RikMigrations - Upgrading

The basic tutorial shows you how to create your initial migration, but how do you use RikMigrations to perform subsequent upgrades?

Migration Versioning

Each migration consists of 2 elements - the migration class and the attribute describing the migration. So what changes between your first migration and subsequent migrations?

The migration class is simply a class which implements IMigration. There is no difference between the first and second migration classes other than the operations they perform.

Two aspects of the attribute change - the type indicating which migration it is describing and the version number. Here's a short example of a couple of migrations:

using RikMigrations;
[assembly: Migration(typeof(BlogMigration1), 1)]
[assembly: Migration(typeof(BlogMigration2), 2)]
namespace Blog.Migrations
{
  public class BlogMigration1 : IMigration
  {
    #region IMigration Members
    public void Up(DbProvider db)
{
      Table t = db.AddTable("Blog");
t.AddColumn("ID", typeof(int)).PrimaryKey().AutoGenerate();
t.AddColumn("Name", typeof(string), 64);
t.Save();
}
    public void Down(DbProvider db)
{
db.DropTable("Blog");
}
    #endregion
  }
  public class BlogMigration2 : IMigration
  {
    #region IMigration Members
    public void Up(DbProvider db)
{
      Table t = db.AlterTable("Blog");
t.AddColumn("Description", typeof(string), int.MaxValue);
t.Save();
}
    public void Down(DbProvider db)
{
      Table t = db.AlterTable("Blog");
t.DropColumn("Description");
t.Save();
}
    #endregion
  }
}

Migration Commands

A reference to an existing table can be obtained using the "db.AlterTable" method:

Table t = db.AlterTable("Blog");

 

This object can be used to add or remove columns and constraints:

t.AddConstraint("U_Name", ConstraintType.Unique, "Name");
t.DropColumn("Description");

 

You can also drop tables via the DbProvider:

db.DropTable("Post");

Here's a quick reference list of methods until a proper reference manual is created:

db.AddTable("Post");
db.DropTable("Post");
t.AddColumn("ID", typeof(int));
t.AddConstraint("PK_BlogPosts", ConstraintType.PrimaryKey, "BlogID", "PostID");
t.DropColumn("ID");
t.DropConstraint("PK_BlogPosts");

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.