升级spring-boot-starter-parent至2.3.3后Flyway迁移SQL报语法错误的根因探究
Spring Boot版本升级后Flyway执行建表脚本语法报错问题排查
基础项目背景
- 项目搭建目的为测试、调优Spring Boot内部实现逻辑,通过调整配置、验证运行效果的方式学习其内部运行原理
- 原有配置下,当
spring-boot-starter-parent版本为2.1.1.RELEASE时,配套的Flyway迁移脚本可正常执行,无报错
项目原有配置
Maven核心依赖配置
<?xml version='1.0' encoding='UTF-8'?> <project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <groupId>com.test.springboot</groupId> <artifactId>spring-boot-application</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.6</version> <scope>provided</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>5.2.4</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
可正常运行的Flyway迁移脚本
原脚本报错位置为列定义后的多余尾逗号,未做调整时的内容如下:
CREATE TABLE PERSON( PERSON_ID BIGINT AUTO_INCREMENT PRIMARY KEY, PERSON_NAME VARCHAR(20) NOT NULL, PERSON_SURNAME VARCHAR(20) NOT NULL, PERSON_AGE BIGINT, );
版本升级后的异常表现
将spring-boot-starter-parent版本升级到2.3.3.RELEASE后,项目启动时Flyway初始化失败,抛出如下语法错误:
Exception encountered during context initialization - cancelling refresh attempt: 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__initial_schema.sql failed --------------------------------------- SQL State : 42001 Error Code : 42001 Message : Syntax error in SQL statement "CREATE TABLE PERSON( PERSON_ID BIGINT AUTO_INCREMENT PRIMARY KEY, PERSON_NAME VARCHAR(20) NOT NULL, PERSON_SURNAME VARCHAR(20) NOT NULL, PERSON_AGE BIGINT, )[*]"; expected "identifier"; SQL statement: CREATE TABLE PERSON( PERSON_ID BIGINT AUTO_INCREMENT PRIMARY KEY, PERSON_NAME VARCHAR(20) NOT NULL, PERSON_SURNAME VARCHAR(20) NOT NULL, PERSON_AGE BIGINT, ) [42001-200]
已验证的临时解决方式:移除最后一列定义
PERSON_AGE BIGINT后的多余尾逗号后,脚本可正常运行
问题根因定位
报错本质是升级Spring Boot版本后,两个核心传递依赖的行为变化共同触发了语法校验拦截,并非手动配置的直接改动导致:
- 嵌入式H2数据库版本的语法校验规则收紧
- Spring Boot 2.1.x 版本自动配置的嵌入式H2数据库为1.4.199版本,该版本的SQL解析器对建表语句的尾逗号做了容错处理,不会直接拦截这类非标准语法的脚本
- Spring Boot 2.3.x 版本自动配置的嵌入式H2数据库为1.4.200版本,该版本重构了SQL语法解析逻辑,严格遵循ANSI SQL标准规则,不再对列定义后的多余尾逗号做容错,直接将这类写法识别为语法错误
- Flyway与Spring Boot的依赖版本对齐机制
升级Spring Boot版本后,Maven的依赖仲裁机制会以spring-boot-starter-parent管理的版本为最高优先级,自动覆盖项目中手动指定的不兼容版本:原项目手动指定的flyway-core 5.2.4会被对齐到与Spring Boot 2.3.3匹配的Flyway 6.x版本。该版本Flyway执行迁移脚本时,会将完整SQL直接传递给数据库解析器校验,不做额外的语法预处理过滤,因此H2的语法校验错误会直接向上抛出导致初始化失败
关于单独在SQL客户端测试可正常执行的说明:多数SQL客户端连接的是独立部署的H2实例,其版本、SQL兼容模式参数与Spring Boot自动配置的嵌入式H2实例存在差异,部分客户端默认开启了MySQL兼容模式,会重新对尾逗号做容错处理,因此不会复现同类报错。
内容的提问来源于stack exchange,提问作者Giorgi Tsiklauri
相关产品推荐
相关产品推荐

