Question

Using FluentMigrator, is there way to find out if the MigrateUp() function will indeed migrate something or whether it's already up to date?

Was it helpful?

Solution

There is no easy way to tell using the public api whether the MigrateUp method will do something or not.

However, there are multiple "other" ways around this that depend on the internals of FluentMigrator:

  • Derive from the MigrationRunner, override the ApplyMigrationUp method, which gets called every time a migration gets applied, and track/log the applied migrations

  • Create a custom IAnnouncer implementation, configure FluentMigrator to use it through the IRunnerContext and in your announcer Say method check that the message parameter contains the text "migrated" which means a migration step has been applied.

  • Look at the pending migrations before running MigrateUp, if you can get a reference on a MigrationRunner you can:
    MigrationRunner runner = ... // get a reference to the runner
    if (runner.MigrationLoader.LoadMigrations() // get all the migrations
            .Any(pair => !runner.VersionLoader
                                .VersionInfo.HasAppliedMigration(pair.Key)))
            // check which migrations have been applied
    {
         // there are pending migrations, do your logic here
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top