flyway 新老版本兼容异常 Validate failed: Detected resolved migration not applied to database
异常信息
2019-01-11 17:20:52.680 INFO 32204 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2019-01-11 17:20:52.714 ERROR 32204 --- [ main] o.s.boot.SpringApplication : Application startup 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: Detected resolved migration not applied to database: 1.0.020190110
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at com.jfintech.jbs.JeanBourgetApplication.main(JeanBourgetApplication.java:40)
Caused by: org.flywaydb.core.api.FlywayException: Validate failed: Detected resolved migration not applied to database: 1.0.020190110
at org.flywaydb.core.Flyway.doValidate(Flyway.java:1286)
at org.flywaydb.core.Flyway.access$100(Flyway.java:71)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1176)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:1168)
at org.flywaydb.core.Flyway.execute(Flyway.java:1650)
at org.flywaydb.core.Flyway.migrate(Flyway.java:1168)
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 16 common frames omitted
错误原因分析
Validate failed: Detected resolved migration not applied to database: 1.0.020190110
出现这个异常原因sql版本校验没过,正常情况一般是版本命名不规范,修改sql版本即可。于是检查数据库中flyway_schema_history的version并没有发现有何不对
解决方法
1、删除flyway_schema_history表中数据,启动项目错误还是存在
2、放大招删flyway_schema_history 表,启动项目错误依然存在,但此时日志中出现一条警告:
2019-01-14 13:35:03.829 WARN 27816 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Could not find schema history table `jfintech_jean_bourget`.`flyway_schema_history`, but found `jfintech_jean_bourget`.`schema_version` instead. You are seeing this message because Flyway changed its default for flyway.table in version 5.0.0 to flyway_schema_history and you are still relying on the old default (schema_version). Set flyway.table=schema_version in your configuration to fix this. This fallback mechanism will be removed in Flyway 6.0.0.
提示信息已经很明显了,仔细一看数据库种确实有一张schema_version的表,而且里面的version和现在sql文件的命名规则不一致。
原来是之前使用了flyway 5.0.0以前的版本,5.0.0以前的版本默认的版本管理表名是schema_version,5.0.0以后默认表名是flyway_schema_history,6.0.0之前会做兼容,6.0.0之后就会去除此兼容。官方建议在配置中设置flyway.table=schema_version
flyway 兼容源码:
public boolean exists() {
if(!this.tableFallback && this.table.getName().equals("flyway_schema_history")) {
Table fallbackTable = this.table.getSchema().getTable("schema_version");
if(fallbackTable.exists()) {
LOG.warn("Could not find schema history table " + this.table + ", but found " + fallbackTable + " instead. You are seeing this message because Flyway changed its default for flyway.table in version 5.0.0 to flyway_schema_history and you are still relying on the old default (schema_version). Set flyway.table=schema_version in your configuration to fix this. This fallback mechanism will be removed in Flyway 6.0.0.");
this.tableFallback = true;
this.table = fallbackTable;
}
}
3、删除 schema_version 问题解决
使用版本及配置
版本:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.7</version>
</dependency>
配置:
flyway:
locations: classpath:/db
baseline-on-migrate: true
版权声明:本文为zh452647457原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。