Jacob first introduced Machine.Migrations over a year ago. Since then, it’s been a solid part of our process and we’re up to nearly 500 migrations with it on one project.
Recently, I finally got around to making some changes I’ve been wanting to make and I wanted to call’em out:
- We split the repostiory out to its own on github. I did this similar to the way I did it for MSpec some time ago. Again, the advantage here is that change logs are localized to the project rather than the entire machine overarching project.
- I added complaints if you have multiple migrations with the same number. This silently caused problems before as one of the migrations would not get applied. Now it just yells and dies like a good app. Of course, this is not as important because of the next change.
-
Next I added support for timestamped migrations. This helps immensely on active projects with multiple developers. You don’t have to deal with communicating migration numbers to the team when you add them, you just generate a new one and merge it when you feel like it. We actually had half of our topic on Campfire dedicated to our current migration number. The other half was who owed how many pushups for breaking the build.
Here’s the rake task we use to generate a new migration:
namespace :new do task :migration do raise "usage: rake new:migration name=\"Your migration name\"" unless ENV.include?('name') name = ENV['name'] filepath = "db/migrate/#{Time.now.strftime('%Y%m%d%H%M%S')}_#{name.gsub(/ /,'_')}.cs" text = File.read("db/migrate/template.cs") File.open(filepath, 'w') { |file| file.puts text.gsub(/\$MigrationName\$/,"#{name.gsub(/ /,'_')}") } end endUsage is simple, just create a template.cs in the directory (we use db/migrate) and then type
rake new:migration name="this is my migration name"
Example template:
using System; using System.Collections.Generic; using System.Text; using Machine.Migrations; public class $MigrationName$ : SimpleMigration { public override void Up() { throw new System.NotImplementedException(); } public override void Down() { throw new System.NotImplementedException(); } } -
Finally, I added a command line runner. As we’ve grown more and more sick of msbuild and more and more fond of rake, we’ve wanted to slowly all but eliminate our dependency on MSBuild. The command line tool is quite simple:
Machine.Migrations Copyright (C) 2007, 2008, 2009 Machine Project c, connection-string Required. The connection string to the database to migrate t, to Applies or unapplies migrations to get to the specified migration u, up Applies migrations to the latest s, scope The scope? d, directory Required. Directory containing the migrations v, compiler-version Version of the compiler to use debug Show Diagnostics r, references Assemblies to reference while building migrations separated by commas t, timeout Default command timeout for migrations ?, help Display this help screen
The only two flags you usually need are -c and -d. Just pass in the connection string and the location of your migrations. Be sure to quote them: migrate.exe -c “my connection string” -d “my migration directory”.
Hopefully these changes help others. Grab the latest here and just msbuild the sln to build it.
I do wonder though, what other active .NET migration projects are people using and how do you like them?