views:

170

answers:

2

Hey all,

I've been playing with SubSonic's SimpleRepository, and it's awesome. However, I would like to know if there's an easy way to apply pending changes to a production system.

Previously, I used SubSonic 2's "Migrations" via a batch file; when I was ready to move to production, I'd just run all the pending migrations against the production server, and I'd be ready to go. Nice and simple.

The SimpleRepostitory takes more of a "run them when you need them" approach to migrations, but I don't want to leave that option on when in production.

Is there a way to get a list of pending changes? I know I could use something like SqlDiff, but since I had a working solution before, it'd be a shame to lose it...

Anyone?

A: 

If you use reflector you can easily find how SimpleRepository manages Migrations.

I haven't actually tried this code, but you could simply do the Migrations the same way the SimpleRepository does:

  • Create a new Migrator, passing in your Assembly to the constructor.
  • Create a BatchQuery if you want to use transactions
  • Iterate through the types you want to migrate, getting the SQL string by calling the MigrateFromModel method on the migrator object
  • if you want to use the BatchQuery, create a new QueryCommand and pass in the object to the BatchQuery's QueueForTransaction method.

Here is the method in Reflector: (following the logic on how MigrateFromModel figures out what needs to change is left as an exercise for the reader :) )

private void Migrate<T>() where T: class, new()
{
    Type item = typeof(T);
    if (!this.migrated.Contains(item))
    {
        BatchQuery query = new BatchQuery(this._provider);
        Migrator migrator = new Migrator(Assembly.GetExecutingAssembly());
        foreach (string str in migrator.MigrateFromModel(item, this._provider))
        {
            query.QueueForTransaction(new QueryCommand(str, this._provider));
        }
        query.ExecuteTransaction();
        this.migrated.Add(item);
    }
}
John Weldon
Sweet, I'll give this a try...
jvenema
I'm not going to have a chance to test this before the bounty expires, so I'm giving it to you anyway. Thanks for the reply.
jvenema
Thanks :) -- let me know how it works out.
John Weldon
FYI, it works like a champ...
jvenema
A: 

Any chance you can package this up and share it back to the project? :)

I've been using a much more "poor man's" migration solution but it doesn't have rollback or other features.

bonder
The Migrator stuff will work with any assembly/typing you pass to it - I need to write a blog post on this...
Rob Conery