6

使用 FluentMigrator,有没有办法找出 MigrateUp() 函数是否确实会迁移某些东西,或者它是否已经是最新的?

4

1 回答 1

13

使用公共 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
    }
于 2014-03-05T06:04:19.810 回答