2

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?

2 Answers 2

0
+100

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);
    }
}
1
  • 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. Nov 19, 2009 at 19:53
0

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.

1
  • The Migrator stuff will work with any assembly/typing you pass to it - I need to write a blog post on this...
    – user1151
    Mar 12, 2010 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.