Flyway H2和MySql升级后不匹配

Posted

技术标签:

【中文标题】Flyway H2和MySql升级后不匹配【英文标题】:Flyway H2 and MySql mismatch after upgrading 【发布时间】:2020-09-11 08:23:37 【问题描述】:

我陷入了深深的泥潭:(

我想将 gradle 从 4 升级到 6。 这导致我升级了 spring,并最终升级了 flyway 和 H2。

现在,不幸的是,我在测试中遇到了飞行路线错误。

这里有一些信息:

 api "org.springframework.boot:spring-boot-starter-json:2.2.2.RELEASE"
api "org.springframework.boot:spring-boot-starter-web:2.2.2.RELEASE"
    api "org.springframework.boot:spring-boot-starter-data-jpa:2.2.2.RELEASE"
testImplementation("org.springframework.boot:spring-boot-starter-test:2.2.2.RELEASE") 
    exclude (group: 'com.h2database', module: 'h2')


api("mysql:mysql-connector-java:5.1.38")
implementation 'org.flywaydb:flyway-core:6.4.2'
testImplementation("com.h2database:h2:1.4.199") 
  force = true


升级前,一切正常。 现在我对另一个版本(虽然我使用的是推荐的)版本和很多错误产生了奇怪的警告。:

 WARN  o.f.c.i.d.b.Database:53 - Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.

ERROR o.f.c.i.c.DbMigrate:57 - Migration of schema "PUBLIC" to version 9 - fixCheckingAccountIndex failed! Please restore backups and roll back database and code!


SQL State  : 42S22
Error Code : 42122
Message    : Column "INDEX" not found; SQL statement:
ALTER TABLE table1 DROP INDEX ACC_INDEX [42122-200]
Location   : db/migration/V9__fixAccountIndex.sql 
Line       : 1
Statement  : ALTER TABLE checking_account DROP INDEX BTA_CHECKING_ACC_INDEX

测试属性:

spring.jpa.hibernate.ddl-auto=none
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE;INIT=create schema if not exists \\"public\"\\; SET SCHEMA public;
spring.datasource.username=root
spring.datasource.password=root

当我正常运行应用程序时,无需测试,一切正常。

有什么想法吗?

感谢和问候,

编辑

我一直在试图理解为什么我会得到 h2 的 200 版。

在我的依赖树上:

gradle -q dependencies | grep h2
+--- com.h2database:h2:1.4.199 (n)
|    |    |         +--- com.h2database:h2:1.4.193 -> 1.4.200
+--- com.h2database:h2:1.4.199 -> 1.4.200
|    |    |         +--- com.h2database:h2:1.4.193 -> 1.4.200
+--- com.h2database:h2:1.4.199 -> 1.4.200
|    |    |         +--- com.h2database:h2:1.4.193 -> 1.4.200
+--- com.h2database:h2:1.4.199 -> 1.4.200
|    |    |         +--- com.h2database:h2:1.4.193 -> 1.4.200
+--- com.h2database:h2:1.4.199 -> 1.4.200

由于某种原因,它使用了较新的版本。

编辑 2020-05-26

这里要求的是升级到 spring 2.3.0 后的错误

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in com...SpringTestConfiguration: Invocation of init method failed; nested exception is org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException: 
Migration V9__IndexFix.sql failed
------------------------------------------------
SQL State  : 42S22
Error Code : 42122
Message    : Column "INDEX" not found; SQL statement:
ALTER TABLE table1 DROP INDEX ACC_INDEX [42122-200]
Location   : db/migration/V9__IndexFix.sql (.../resources/db/migration/V9__IndexFix.sql)
Line       : 1
Statement  : ALTER TABLE table1 DROP INDEX ACC_INDEX

我在这篇文章中抱怨的兼容性警告在升级后消失了

还是这个 h2 错误。在旧版本上它工作。 当前版本:

org.flywaydb:flyway-core:6.4.1(虽然在 gradle 中我放了 6.4.2) com.h2database:h2:1.4.200

【问题讨论】:

spring-boot-starter-test 附带一个 H2 依赖项,版本为 1.4.200。我假设这会覆盖您设置的依赖项。尝试从 spring-boot-starter-test 中排除 H2 依赖项。 我添加了这个:``` testImplementation("org.springframework.boot:spring-boot-starter-test:$ver.spring") exclude (group: 'com.h2database' , module: 'h2') ``` 而且它仍然获取 200 版本。甚至在真正的依赖上添加了 force = true。 我认为有关 H2 的警告具有误导性。这只是一个警告。日志显示:“将架构“PUBLIC”迁移到版本 9”失败。因此迁移到模式的版本 8 是成功的。尝试删除这第 9 个迁移步骤,看看会发生什么。 尝试在 gradle 树上搜索版本 200:om.h2database:h2:1.4.199 -> 1.4.200 到处都能看到,但没有实际的 com.h2database:h2:200 任何地方......为什么我会得到它? gradle -q 依赖 | grep h2 +--- com.h2database:h2:1.4.199 (n) | | | +--- com.h2database:h2:1.4.193 -> 1.4.200 +--- com.h2database:h2:1.4.199 -> 1.4.200 | | | +--- com.h2database:h2:1.4.193 -> 1.4.200 +--- com.h2database:h2:1.4.199 -> 1.4.200 | | | +--- com.h2database:h2:1.4.193 -> 1.4.200 +--- com.h2database:h2:1.4.199 -> 1.4.200 | | | +--- com.h2database:h2:1.4.193 -> 1.4.200 +--- com.h2database:h2:1.4.199 -> 1.4.200 【参考方案1】:

将您的 Spring Boot 依赖项更新到 2.3.0.RELEASE。

2.2.X 在旧的 flyway 版本 (6.0.8) 和不受支持的新 H2 版本 (1.4.200) 之间存在依赖项不匹配。 Flyway 6.1.0 支持 H2 1.4.200。

即使您指定了较新的 Flyway 版本 - 我认为它与指定的 H2 版本一样被忽略。

编辑: 或者,您可以像这样在 Gradle 中强制使用特定版本:

allprojects 
    configurations.all 
        resolutionStrategy 
            dependencySubstitution 
                substitute module('com.h2database:h2') with module('com.h2database:h2:1.4.199')
            
        
    

【讨论】:

感谢我升级到第 3 版。大多数都在工作,但我仍然从 H2 上的 flyway 收到错误。它适用于旧版本此脚本 ALTER TABLE table DROP INDEX CHECKING_ACC_INDEX 在我看来,不允许使用 ALTER TABLE DROP INDEX。但我在工作。它正在使用 MySQL 开发 PROD 现在使用哪个版本的 Flyway 和 H2? org.flywaydb:flyway-core:6.4.1 (虽然在 gradle 中我放了 6.4.2) com.h2database:h2:1.4.200 但我在文档h2database.com/html/commands.html 中看到 ALTER TABLE DROP INDEX 不存在。但同样,这是一个旧脚本,在升级之前可以正常工作。有什么想法吗? 当您将 MySQL 指定为 H2 模式时,该命令将像以前一样工作。错误仍然完全相同吗?警告还在吗?也许您用最新信息更新您的问题。 我按照您的要求进行了编辑。同样的问题。问题不在于 MySQL - 问题在于 H2

以上是关于Flyway H2和MySql升级后不匹配的主要内容,如果未能解决你的问题,请参考以下文章

flyway 在初始模式创建后不创建模式

如何在spring boot中创建H2+flyway测试数据库?

如何最好地处理带有嵌入式数据库的 Flyway 以进行集成测试?

以 # 开头的行不被视为 H2 中 MySQL 的注释

使用 Flyway 设置多个数据库

如何在 h2 数据库中插入特定的 UUID?