FluentMigrator Rising From The Ashes

  Well more like it was peacefully sleeping until I came along and started pounding it on the chest. Now it’s hunched over and coughing while muttering something about me under it’s breath. But anyway..

  I wanted to build schema management into one of the projects I’m working on, and found FluentMigrator partly by reference and partly by accident.  The point is to build up and change database schema in code with a fluent syntax. Perfect, only I wanted my app to automatically update if it has a newer version than the database. So I went about adding that to the code, and all of a sudden this fork appeared for me in github while angelic voices sung magically in the background. Right before I pulled the sword from the stone! Seriously though, I’m working on adding functionality to an excellent project started by Justin Etheredge and Nate Kohari. You can find my github fork here.

  My focus will be on my own needs, but feel free to make requests along the way. Even better, feel free to help! Hopefully, Justin will accept my changes into the master tree and maybe we can put together a release.

To give you a bit of a preview, here is a sample migration to create a couple tables and foreign keys:

   1:      [Migration(1)]
   2:      public class TestMigration : Migration
   3:      {
   4:          public override void Up()
   5:          {
   6:              Create.Table("Users")
   7:                  .WithColumn("UserId").AsInt32().Identity().PrimaryKey()
   8:                  .WithColumn("GroupId").AsInt32().NotNullable()
   9:                  .WithColumn("UserName").AsString(32).NotNullable()
  10:                  .WithColumn("Password").AsString(32).NotNullable();
  11:   
  12:              Create.Table("Groups")
  13:                  .WithColumn("GroupId").AsInt32().Identity().PrimaryKey()
  14:                  .WithColumn("Name").AsString(32).NotNullable();
  15:   
  16:              Create.Column("Foo").OnTable("Users").AsInt16().Indexed();
  17:   
  18:              Create.ForeignKey("FK_Foo").FromTable("Users").ForeignColumn("GroupId").ToTable("Groups").PrimaryColumn("GroupId");
  19:              
  20:              Create.Table("Foo")
  21:                  .WithColumn("Fizz").AsString(32);
  22:   
  23:              Rename.Table("Foo").To("Bar");
  24:              Rename.Column("Fizz").OnTable("Bar").To("Buzz");
  25:   
  26:              Insert.IntoTable("Users").Row(new { UserName = "Data1", Password = "Data2" });
  27:          }
  28:   
  29:          public override void Down()
  30:          {
  31:              Delete.ForeignKey("FK_Foo").OnTable("Users");
  32:              
  33:              Delete.Table("Bar");
  34:              Delete.Column("Foo").FromTable("Users");
  35:              Delete.Table("Users");
  36:              Delete.Table("Groups");
  37:          }
  38:      }

 

I’m actually not so much of a fan of the class attribute to specify the version. I’d prefer to have a convention using the class name, and a convention to specify where the migration classes live within an assembly. We’ll work with this for now and see what can be done later. So go check it out.

-j

 
August 19, 2009 07:00 by josh
E-mail | Permalink | Comments (4) | Comment RSSRSS comment feed

Comments

August 19. 2009 07:23

trackback

FluentMigrator Rising From The Ashes

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com

August 19. 2009 07:37

Tuna Toksoz

Nice to see it on development! I also like the idea of convention, however it is really not a big deal.
Perhaps something like

TestMigrationv01_00_00

but not really a big issue.

Tuna Toksoz

August 20. 2009 04:20

Duckie

Nice!

I dont like conventions in naming, imho it is too error-prone. It is impossible to make errors in attaching a attribute to a class. Also, it makes it possible to use whatever namingsystem i want for my migrations.

No reason to get attribute-phobia Smile

Duckie

August 20. 2009 06:39

Justin Etheredge

Looks great. I actually don't mind the attribute approach. That is what they are there for, annotating types. Anyways, thanks again for all of the help.

P.S. My last name has no "I" in it Smile

Justin Etheredge

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading




about josh

another programmer blogging about his misadventures in writing code.

Contact

contact us for website & software consulting

Decide

decide on pragmatic solutions

Develop

develop your product together

Succeed

achieve your goals with our services