Flyway:如何在不获取 "FlywayException: Validate failed" 的情况下替换已弃用的 SpringJdbcMigration?

Flyway: How to replace deprecated SpringJdbcMigration without getting "FlywayException: Validate failed"?

将springboot 2.0.3升级到2.1.1也带来了一个新的flyway版本:5.2.3而不是5.0.7。

在 5.2.3 中,SpringJdbcMigration 已弃用,并将在 flyway 6 中删除。我主要使用 sql 脚本,但我在一个项目中也有一个 java 迁移 class (有 4 个 sql 迁移,最后一个 4.2 是一个 java 迁移 - 它只是一些快速而肮脏的 hack 来更改旧数据,我不再需要它了)。

所以我改变了 class:

class V4_2__Update_foo implements SpringJdbcMigration {
    public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
    ...
    }
}

class V4_2__Update_foo extends BaseJavaMigration {
    public void migrate(Context context) throws Exception {
    JdbcTemplate jdbcTemplate = 
      new JdbcTemplate(new SingleConnectionDataSource(context.getConnection(), true));
    ......
    }
}

这是唯一的变化,其他都是一样的。结果是

Application run failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]:
Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException:
Validate failed: Migration type mismatch for migration version 4.2
....
Caused by: org.flywaydb.core.api.FlywayException:
Validate failed: Migration type mismatch for migration version 4.2
-> Applied to database : SPRING_JDBC
-> Resolved locally    : JDBC
at org.flywaydb.core.Flyway.doValidate(Flyway.java:1479)

我不想永久禁用验证,但我也不知道如何解决这个问题。我尝试使用谷歌搜索,但没有发现任何关于 "type mismatch" 的信息。 在我的开发机器上,我尝试了 "flyway repair",但它只说

Repair of failed migration in Schema History table "PUBLIC"."schema_version" not necessary. No failed migration detected.
Successfully repaired schema history table "PUBLIC"."schema_version"

迁移4.2的类型在运行修复后仍然是"SPRING_JDBC"。 后来我完全删除了 java class 这让我收到警告

Schema "PUBLIC" has a version (4.2) that is newer than the latest available migration (4) !

但至少应用程序再次 运行。不过,我不太愿意在生产中这样做。还有其他想法吗?

这绝对是一个错误。不幸的是,这不会在 5.2.x 中修复。 Flyway 6.0(将于 2019 年第一季度发布)将自动更正您的架构历史记录 table 并修复此问题。

或者,如果您真的不想等待,您可以手动查找架构历史记录 table 以使此消息消失。

问题:

[schema_version table 与 SPRING_JDBC][1] [1]: https://i.stack.imgur.com/GI9So.png

解决方案: 我通过使用以下查询手动更新 schema_version table 来解决此问题:update schema_version set type='SQL', checksum=662979041 where script='V001_026__initial_reconciliation_config_env_NonProd.sql';

但是,要创建以上 UPDATE 语句,您将需要您之前使用 spring/java class 迁移的实际脚本的校验和,其中没有校验和。

如何获得校验和? 在你的本地,删除本地的schema_version table。成功启动您的应用程序,您现在应该使用正确的校验和创建新的 schema_version table 并输入 SQL.

checksum是检查文件的完整性,即一旦得到checksum就不要修改文件。

然后在非生产环境中手动执行上面的 UPDATE 语句。

下一步,删除 java class,将该脚本移动到 db/non-prod 文件夹中,并在 application.yaml 中配置该文件夹,例如

spring: 飞路: 位置:“class路径:db/migration,class路径:db/env/migration/V001/NON-PROD”