RikWare
...

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");