Search code examples
c#.netfluent-migrator

Run FluentMigrator for one specific migration only


We are using FluentMigrator with C# to run our database changes and we don't use any tag as we have simple solution, so all our migrations are without any tags.

I know it seems not common, but I hope there is a solution in their API.

Is there a programmatic way to run one specific migration by specifying its version number (No tags)?

I am not talking about

migrationRunner.MigrateUp(version);

Because that will run all migrations up to that version.

I want to apply one that specific migration only and not run the previous versions if they are not applied.

So if the last applied version is 26, and I want to run version 30. So Is there a way to run only 30 without running 27,28,29 ?

As I said, all our migrations have no tags, so it is difficult to do it with tags.


Solution

  • It turns out there is a way in their API to do it.
    I had to read their code.

    If you ever want to do this crazy thing, you can do this in the code:

    private static void ApplySpecificMigration(IServiceProvider serviceProvider, long version)
    {
        var runner = serviceProvider.GetRequiredService<IMigrationRunner>();
    
        
        var targetMigration = runner.MigrationLoader.LoadMigrations()
            .FirstOrDefault(migration => migration.Key == version);
    
        ((MigrationRunner) migrationRunner).ApplyMigrationUp(migration.Value, true);
    }