Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET migrations: Setup and migrate multiple databases at runtime

Brief introduction: I have this ASP.NET Webforms site with the particularity that it doesn't have only 1 database, it has many. Why? Because you can create new "instances" of the site on-the-fly. Every "instance" share the same codebase, but has its own database. These all databases have the same schema (structure) but of course different data. Don't ask 'why don't you put everything in one database and use InstanceId to know which is" because it's a business policy thing.

The application knows which instance is being requested because of the url. There is one extra database to accomplish this (I do know its connection string in design time). This database has only 2 tables and associates urls to 'application instances'. Then, of course, each 'application instance' has its associated connection string.

Current situation: There is nothing being used right now to help us with the job of mantaining every instance database in sync (propagating schema changes to every one). So we are doing it by hand, which of course it's a total mess.

Question: I'd like to use a rails-migration way to handle schema changes, preferably migratordotnet, but could use any other if it's easier to setup.

The problem is that migratordotnet needs the connnection string to be declare in the proj.build file and I don't know them until runtime.

What it would be REALLY useful is some kind of method running on Application_Start that applies the latest migration to every database.

How could this be done with migratordotnet or any similar? Any other suggestion is thanksfully welcomed.

Thank you!

like image 808
empz Avatar asked Jul 15 '10 06:07

empz


3 Answers

Since this is an old question, I assume that you have solved the problem in some manner or another, but I'll post a solution anyway for the benefit of other people stumbling across this question. It is possible to invoke MigratorDotNet from code, rather than having it as an MSBuild target:

public static void MigrateToLastVersion(string provider, string connectionString)
{
    var silentLogger = new Logger(false, new ILogWriter[0]);
    var migrator = new Migrator.Migrator(provider, connectionString, 
                   typeof(YourMigrationAssembly).Assembly, false, silentLogger);
    migrator.MigrateToLastVersion();
}
like image 150
Aasmund Eldhuset Avatar answered Nov 06 '22 05:11

Aasmund Eldhuset


RedGate has a SQL Comparison SDK that could be used. Here is a Case Study that looks promising, but I can't tell you anthing from experience as I haven't used it. Download the trial and kick the tires.

like image 22
Larry Smithmier Avatar answered Nov 06 '22 04:11

Larry Smithmier


You could use Mig# to maintain your migrations in your C# or .NET code: https://github.com/dradovic/MigSharp

like image 20
Dejan Avatar answered Nov 06 '22 05:11

Dejan