Hanalei, Hawaii

I crossed Time and Space today on my way into my office - the coffee was strong, the tunes loud - and I ended up tripping my way into an alternate universe where the EF team just posted about their new migration tool. I have to admit - this Fantasy Migration Tool is pretty compelling. Wanna see?

The following is not real. It’s a small dream from a small mind about a huge tool.

Versioning! Hurrah!

The thing I was most impressed with is the ditching of Automatic Migrations. They never work - you always have to write code or a command to get around a limitation. Rails solved this by asking you to be explicit with what you want to happen.

So I was happy to see EF adopt explicit directives! Check this out:

[Migrations]
public class Migrations{
  DbContext _db;
  public Migrations(){
    _db = new DbContext();
  }

  [Migration("001")]
  public void Create_Meats_And_Cheeses(){
    _db.Migration.CreateTable("Meats")
        .Column(name:"FatContent",dbtype: DbType.Decimal, nullable:false)
        .Column(name:"Edible", dbtype: DbType.Boolean, nullable:true, default_value:false)
 
    _db.Migration.CreateTable("Cheeses")
        .Column(name:"Name",dbtype: DbType.String, nullable:false)
        .Column(name:"IsSnotty", dbtype: DbType.Boolean, nullable:true, default_value:true)

    _db.Migration.CreateIndex(table:"Cheeses", column:"Name", indexType: IndexType.Unique);

    _db.Migration.ExecuteScript("INSERT INTO Cheeses(Name, IsSnotty) VALUES ('Osau Iraty', true);")

    
  }
}

It’s a bit noisy - but I love the clarity! It’s also a really great option that they threw in the ability to execute ad-hoc queries in there so you can insert data as a primer - very useful.

I’m not a fan of the attributes - but it makes it abundantly clear what’s going on here.

Execution is incredibly simple too:

PM> db_migrate
1 migration found
Current db version: 0
---------------------
  CREATE TABLE [Meats] ...
  CREATE TABLE [Cheeses] ...
  CREATE INDEX [IX_Cheese_Name]...
  ...
  Finished (0.0034 seconds)

Nice. You can also specify the version if you like:

PM> db_migrate "001"
1 migration found
Current db version: 0
---------------------
  CREATE TABLE [Meats] ...
  CREATE TABLE [Cheeses] ...
  CREATE INDEX [IX_Cheese_Name]...
  ...
  Finished (0.0034 seconds)

And if you decide you screwed up and need to redo something - just rollback and do it again:

PM> db_rollback
  DROP TABLE [Meats]
  DROP TABLE [Cheeses]

The interesting thing about this latter part is that traditionally, with Rails, you had to specify an “up” and a “down” explicitly. 99% of the time “down” can be inferred - and indeed that’s what EF is doing here. It sees that in the “001” migration there are two calls to “CreateTable” - so logically they should do the reverse of that, which is “DropTable”.

Nicely done EF team!

But Wait! There’s More!

When you run the migrations all your EdmMetaData is redone - but how do you know? Check out what I now have after running my migration:

It updated it automatically for me! Unbelievably nice touch.

Cross Platform

This just blew me away: I decided to see if this would work with PostGres. Why not? A guy can dream right?

And it did. I simply repointed my connection string to my local PostGres, reset my provider in my connection string and BAM - a full DB sitting right there for me.

OK this is pretty much where I snapped back to our plane of existence - the psychic weight of my own delusions crashing in the walls of my extreme caffeine hallucinations.

But you know something? It’s Alpha still. Anything can happen right? And usually that “anything” amounts some amusing efforts from the EF team (and endless blog fodder as they stumble their way over well-trodden paths) - but there’s always room for some positive energy and a little love from the people that make fun of them constantly.

So here ya go EF team: I present to you my little Migration Fantasy for your current efforts. Something that’s been underway for about 5 years now (starting with RikMigrations, then SubSonic, then Migrator.net,  then FluentMigrations… you’ve got a lot of resources out there to use!

I’ll talk to LCS for you guys.

Blog comments powered by Disqus

My name is Rob Conery and I am the owner/smooth operator of Tekpub, creator of
This Developer's Life, and an avid Ruby/Rails/.NET developer.

Find Something