【问题标题】:Why does my FlywayMigrationStrategy call afterMigrate.sql twice?为什么我的 FlywayMigrationStrategy 调用 afterMigrate.sql 两次?
【发布时间】:2017-12-06 15:27:02
【问题描述】:

我从事 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;
}

我的 Migration 文件夹包含几个版本化的迁移脚本和一个 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() 时它被调用了两次,以及如何防止第二次调用?

【问题讨论】:

    标签: spring spring-boot flyway


    【解决方案1】:

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

    【讨论】:

    • 感谢您的信息。我希望这个错误将很快得到修复。
    • 这个问题现在在 5.0.1 版本中得到修复。感谢 Alex 和所有贡献者。
    • 嗯,我不认为问题出在 FlywayDB 但in my case 这部分在部署后没有执行。
    猜你喜欢
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多