如何检查是否需要运行迁移或是否使用流畅的迁移器运行迁移?

OnR*_*lve 5 fluent-migrator

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

nem*_*esv 12

没有简单的方法来告诉使用公共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
    }
Run Code Online (Sandbox Code Playgroud)