Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a migration needs to be ran or did run with fluent migrator?

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

like image 299
OnResolve Avatar asked Feb 27 '14 18:02

OnResolve


1 Answers

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
    }
like image 53
nemesv Avatar answered Jan 04 '23 17:01

nemesv