Search code examples
unit-testingfluent-migrator

How can I unit test my FluentMigrator migrations?


The general advice is I should always test my database migrations, but how to do it seems to be a well kept secret ;)

My chosen framework is FluentMigration.

What I think I want to do is:

  1. Migrate database to N-1.
  2. Save some data.
  3. Migrate database to N.
  4. Read data and verify it's not lost.
  5. Verify other relevant changes

But I can't figure out how to run the migrations from my unit tests.


Solution

  • To kick off the migration in your integration tests just shell out to the migrate.exe command using Process.Start

    For example

    var migrator = System.Diagnostics.Process.Start("migrator.exe", "/connection \"Data Source=db\\db.sqlite;Version=3;\" /db sqlite /target your.migrations.dll");
    migrator.WaitForExit();
    

    If you're using MSTest you'll have to make sure that migrator.exe is included as a deployment item, or that you specify a path to where the .exe lives when you start the process.