使用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
    }
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top