参考https://blog.csdn.net/qq_34845394/article/details/90025456
https://stackoverflow.com/questions/64173564/failed-to-ugrade-a-spring-boot-app-to-flyway-7-0-0
1.1、maven坐标由于flyway高于6.5.7的版本与 Spring Boot 2.3.4 不兼容,我们使用6.5.7这个版本
引入maven坐标
# 数据源配置,注意对齐spring: flyway: # 是否启用flyway enabled: true # 迁移sql脚本文件存放路径,默认classpath:db/migration # locations: classpath:db/migration # 当迁移发现数据库非空且存在没有元数据的表时,自动执行基准迁移,新建schema_version表 baseline-on-migrate: true
1.3、脚本名称约定sql脚本名称约定格式:V + 版本号 + 双下划线(__) + 描述 + 结束符(.sql)
1.4、控制原理主要是通过对比flyway_schema_history表中 version 与 checksum.来判断文件是否存在与被修改过.来执行版本控制的.
2、常见错误与注意事项常见错误一:
sql脚本有错误,执行失败修改后重新执行报错:
nested exception is 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: Detected failed migration to version 20220207.3 (test delete)
需要手动修复,找到对应的约束进行删除:
DELETE FROM SPDCLOUD."flyway_schema_history" WHERe "installed_rank"=xxx;
常见错误二:
已经执行过的脚本被改动,导致校验失败
nested exception is 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 checksum mismatch for migration version 20220207.1-> Applied to database : -335589342-> Resolved locally : -725714856
还原该脚本可以解决
注意
1.特别要注意脚本文件的正确性,一定要保证脚本的正确,否则脚本执行到一半发生错误就需要手动修复(常见错误一)
2.脚本文件如果已经执行过,便不要在进行任何更改(否则无法通过校验,常见错误二)
3.如果使用了flyway进行版本管理,就不要单独执行任何sql,以保证flyway的脚本完整性