【问题标题】:Spring Boot 2.2.1 H2 Failures?Spring Boot 2.2.1 H2 故障?
【发布时间】:2020-03-08 07:53:27
【问题描述】:

我有一个运行良好的 Spring Boot 2.2.0、H2 和 Flyway 项目。 Spring Boot 2.2.1 刚刚发布(所以我得到了一个 Dependabot PR),现在我的迁移失败了(路径/表名/列已清理):

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.internal.command.DbMigrate$FlywayMigrateException: 
Migration V1__Creating_tableName_table.sql failed
------------------------------------------------------
SQL State  : 42001
Error Code : 42001
Message    : Syntax error in SQL statement "CREATE TABLE MY_PROJECT_TABLE (
-- table definition here
) ENGINE=[*]INNODB"; expected "identifier"; SQL statement:
CREATE TABLE my_project_table (
-- table definition here
) ENGINE=InnoDB [42001-200]
Location   : db/migration/V1__Creating_tableName_table.sql (/path/to/file.sql)
Line       : 1
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1803) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]

看起来它不喜欢 ENGINE=INNODB 部分

除了 Spring Boot 的版本没有任何变化(从外观上看,这意味着一些传递依赖,包括 H2),但我在问题或 Spring Boot 的变更日志中没有看到明显的问题,表明问题出在哪里谎言。

我猜这要么与 Spring Boot 将 H2 自动配置为测试数据库(可能是兼容模式?)有关,要么与 H2 的更改有关,但这只是一个猜测,因为我遇到了麻烦在问题/变更日志中找到明显的原因。

【问题讨论】:

  • 表定义中是否有尾随逗号? H2 1.4.200(这是 Boot 2.2.1 中的默认版本)放弃了对它们的支持:github.com/h2database/h2database/pull/2099
  • 是的,我发现了这个问题并查看了,不,我没有看到任何尾随逗号。

标签: spring-boot h2 flyway


【解决方案1】:

除非启用 MySQL 兼容模式,否则您不能在 H2 1.4.200 中使用 ENGINE=InnoDB 子句。

如果要启用它,请将;MODE=MySQL 附加到连接 URL。您可能还想添加;DATABASE_TO_LOWER=TRUE 和可能的;CASE_INSENSITIVE_IDENTIFIERS=TRUE。 MySQL 兼容模式不会自动启用这些标志。

【讨论】:

  • 这是 H2 1.4.200 的新变化?因为我使用的是 Spring Boot 的测试数据库自动配置,所以我实际上并没有在任何地方指定 JDBC URL。我的非测试数据库是 MySQL,这是指定的,但测试数据库是自动配置的,我没有输入。
  • 我可以做这样的事情,但如果有其他方法可以调整自动配置,我不希望这样做:stackoverflow.com/a/43557541/141042
  • 是的,在 1.4.200 中,对这些 MySQL 兼容性子句的支持得到了改进,以接受更多不同的结构,并且还仅限于 MySQL 兼容模式,旧版本的 H2 也错误地接受了其中的一些在常规模式下。你不应该在没有兼容模式的情况下使用供应商特定的功能,它们在其他数据库中没有意义。
  • 当然——有道理,只是想确保这解释了为什么迁移在 Spring Boot 2.2.0 中有效,但在 2.2.1 中无效。我希望 Spring Boot 能够基于非测试数据库设置兼容模式,但至少现在我明白了什么改变了在哪里进行修复。
猜你喜欢
  • 2019-11-30
  • 2015-01-08
  • 2014-09-19
  • 2020-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
相关资源
最近更新 更多