为什么我的 FlywayMigrationStrategy 调用 afterMigrate.sql 两次?

Why does my FlywayMigrationStrategy call afterMigrate.sql twice?

我在 Spring-boot 项目上工作,使用 Flyway 进行数据库迁移。 在开发配置文件中工作时,我想用虚拟数据填充数据库。 为了获得完全相同的初始数据值,我覆盖了 FlywayMigrationStrategy Bean,以便它在迁移开始之前执行 flyway.clean():

@Bean
@Profile("dev")
public FlywayMigrationStrategy cleanMigrateStrategy() {
    FlywayMigrationStrategy strategy = new FlywayMigrationStrategy() {
        @Override
        public void migrate(Flyway flyway) {
            flyway.clean();
            flyway.migrate();
        }
    };

    return strategy;
}

我的迁移文件夹包含几个版本化的迁移脚本和一个 afterMigrate 回调脚本,它将数据添加到创建的表中。

现在的问题是,afterMigrate.sql 脚本被调用了两次,您可以从以下日志中看到:

2017-07-03 13:12:42.332  INFO 23222 --- [           main] o.f.core.internal.command.DbClean        : Successfully cleaned schema "PUBLIC" (execution time 00:00.031s)
2017-07-03 13:12:42.397  INFO 23222 --- [           main] o.f.core.internal.command.DbValidate     : Successfully validated 4 migrations (execution time 00:00.044s)
2017-07-03 13:12:42.413  INFO 23222 --- [           main] o.f.c.i.metadatatable.MetaDataTableImpl  : Creating Metadata table: "PUBLIC"."schema_version"
2017-07-03 13:12:42.428  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Current version of schema "PUBLIC": << Empty Schema >>
2017-07-03 13:12:42.430  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 1 - create users
2017-07-03 13:12:42.449  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 2 - create address
2017-07-03 13:12:42.464  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 3 - create patient case
2017-07-03 13:12:42.475  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Migrating schema "PUBLIC" to version 4 - state machine
2017-07-03 13:12:42.498  INFO 23222 --- [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 4 migrations to schema "PUBLIC" (execution time 00:00.086s).
2017-07-03 13:12:42.499  INFO 23222 --- [           main] o.f.c.i.c.SqlScriptFlywayCallback        : Executing SQL callback: afterMigrate
2017-07-03 13:12:42.502  INFO 23222 --- [           main] o.f.c.i.c.SqlScriptFlywayCallback        : Executing SQL callback: afterMigrate
2017-07-03 13:12:42.917  INFO 23222 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup

如果我删除 flyway.clean() 函数调用,它只会被调用一次。

有人能告诉我为什么在我调用 flyway.clean() 和 flyway.migrate() 时会调用两次以及如何防止第二次调用吗?

这是一个已知问题,将在 Flyway 5.0 中修复。参见 https://github.com/flyway/flyway/issues/1653