Question

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?

Was it helpful?

Solution

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);
    }
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top