Using FluentMigrator, is there way to find out if the MigrateUp() function will indeed migrate something or whether it's already up to date?
How to check if a migration needs to be ran or did run with fluent migrator?
Asked Answered
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 theApplyMigrationUp
method, which gets called every time a migration gets applied, and track/log the applied migrationsCreate a custom
IAnnouncer
implementation, configure FluentMigrator to use it through theIRunnerContext
and in your announcerSay
method check that themessage
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 aMigrationRunner
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
}
© 2022 - 2024 — McMap. All rights reserved.