Question

I am using FluentMigrator to manage my database changes, I execute my migrations like this:

const string connectionString = @"Data Source=localhost, 1433;Initial Catalog=testdb;Integrated Security=SSPI;";
            Announcer announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
            announcer.ShowSql = true;

            Assembly assembly = Assembly.GetAssembly(typeof (MigrationMarker));
            IRunnerContext migrationContext = new RunnerContext(announcer);

            var options = new ProcessorOptions
                              {
                                  PreviewOnly = false, // set to true to see the SQL
                                  Timeout = 60
                              };

            var factory = new SqlServer2008ProcessorFactory();
            IMigrationProcessor processor = factory.Create(connectionString, announcer, options);
            var runner = new MigrationRunner(assembly, migrationContext, processor);

            runner.MigrateUp(true);

What I can't figure out though, is how to execute a migration for a specific profile?

So given that my migrator has an attribute like this:

[Profile("DevMigration")]
public class DevMigration : FluentMigrator.Migration
{

I have tried a few variations of:

runner.ProfileLoader.FindProfilesIn(assembly, "DevMigrator");
runner.ApplyProfiles();

But I'm not getting any closer, does anyone know how I can execute a profile migration using the runner?

Was it helpful?

Solution

Try setting the profiles on the migration context before being passed to the migration runner like this:

IRunnerContext migrationContext = new RunnerContext(announcer);
migrationContext.Profile = "DevMigrator"

The profile loader method FindProfilesIn only returns the migrations with the profile. The constructor of the RunnerContext loads the ProfileLoader which by default loads the migrations for the specified profile in the context (I think this defaults to null therefore having no profile migrations).

You shouldn't need to manually call the ApplyProfiles method as this is called in the MigrateUp(bool) method.

OTHER TIPS

For the people reading it much later and using in-process migrator as it's shown in the docs here. The way to specify Profile name in this case is adding another Configure call on ServiceCollection like this:

            .Configure<RunnerOptions>(cfg =>
            {
                cfg.Profile = "DevMigration";
            })
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top