RikMigrations Tutorial

 

Let's get running with a basic example of using RikMigrations. (To find out more or download RikMigrations go here)

The first step is to create the migrations classes. There's two things you must do to make a class a migration:

  1. Implement the IMigrations interface
  2. Add a MigrationAttribute to the assembly.

You can name your class whatever you want and put it in whatever namespace. Let's see it so far: 

using RikMigrations;
[assembly: Migration(typeof(BlogMigration1), 1)]
namespace Blog.Migrations
{
  public class BlogMigration1 : IMigration
  {
    #region IMigration Members
    public void Up(DbProvider db)
    {
      throw new Exception("The method is not implemented.");
    }
    public void Down(DbProvider db)
    {
      throw new Exception("The method is not implemented.");
    }
    #endregion
  }
}

Now that we have the stubs, let's add the actual code to create the database. The DbProvider class provides our entry point to the database. First "Up", let's add a table:

Table t = db.AddTable("Blog");
t.Save();

Now that won't actually work yet because we have to have some columns for the Save method to work. So let's add some basic columns:

Table t = db.AddTable("Blog");
t.AddColumn("ID", typeof(int));
t.AddColumn("Name", typeof(string), 64);
t.Save();

This will set up an integer ID column and a string column to save the name in. Notice that we can specify a length for string columns.

Of course, there's probably some extra info we want to specify on these columns. The ID column is probably our Primary Key, and we probably want it to be an identity column so it will generate its values automatically.

RikMigrations uses a fluid interface for specifying the columns properties. This means you can just list the properties after the AddColumn call. Each method returns the column so you can call the next method:

t.AddColumn("ID", typeof(int)).PrimaryKey().AutoGenerate();

That should be enough for the Up method. The "Down" column just needs to do the reverse of this:

db.DropTable("Blog");

Running the Migration

Manually

Migrations can be run from the command line. You will need to build the assembly the migrations are stored in first. Once that's done just use RikMigrations.exe to update your database:

RikMigrations migrate TestApp.exe -s:(local)\SQLEXPRESS -db:TestDB 

Programatically

You can also run your migrations easily from within your code using the MigrationManager. The following line will run all of the migrations in your assembly up to their maximum version:

MigrationManager.UpgradeMax(typeof(Blog.Migrations.BlogMigration1).Assembly);

This line could be dropped in your global.asax to automatically keep your database up to date whenever you deploy a new version of a website. By default the RikMigrations will use the first connection string from you configuration, but you can use DbProvider.DefaultConnectionString to supply an alternative.

Back to RikMigrations Homepage

About Us

RikWare is a software development company based in Brisbane, Australia. Led by director, Richard Mason, RikWare focusses on solving challenging business problems promptly and effectively.

RikWare is also the owner of Aussie Bushwalking, a free, wiki-like collaboration site for bushwalkers and the creator of RikMigrations, a migration framework for .NET.

Aussie Bushwalking

Want to discover some new bushwalking ideas? Share a few of your own? Aussie Bushwalking is like Wikipedia... but for bushwalkers. Head on over. Anybody can see the walks and it's free to sign up and share your own.

 

RikMigrations

RikMigrations is a database migration library for .NET based on the migrations facility provided by Ruby on Rails. It allows simple upgrading and downgrading of databases using .NET code (such as VB or C#) either from the command line or built into your application. Unlike many other migration frameworks, RikMigrations is a stand-alone library which does not rely on a large surrounding framework.