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

1

6

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

Countershading answered 27/2, 2014 at 18:6 Comment(0)
C
14

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
    }
Cosenza answered 5/3, 2014 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.