如何检查迁移是否需要运行或使用fluent migrator运行?
-
23-12-2019 - |
题
使用FluentMigrator,有没有办法找出MigrateUp()函数是否确实会迁移某些东西,或者它是否已经是最新的?
解决方案
没有简单的方法来告诉使用公共api是否 MigrateUp
方法会做某事或不做。
但是,有多种"其他"方法依赖于FluentMigrator的内部:
从
MigrationRunner
, ,复盖ApplyMigrationUp
方法,每次应用迁移时都会调用该方法,并跟踪/记录应用的迁移创建自定义
IAnnouncer
实现,配置FluentMigrator通过IRunnerContext
在你的播音员Say
方法检查message
参数包含文本"migrated"
这意味着已经应用了迁移步骤。- 在运行之前查看挂起的迁移
MigrateUp
, ,如果你能得到一个参考MigrationRunner
你可以:
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
}
不隶属于 StackOverflow