创建名为“flywayInitializer”的 bean 时出错

我正在尝试在 Docker 容器中运行我的项目测试。所有测试在本地运行时都运行良好。当我尝试将测试移动到 docker 容器时,错误开始发生。这是错误消息:


java.lang.IllegalStateException: Failed to load ApplicationContext

[...]

Caused by: 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_user.sql failed

-------------------------------------

SQL State  : 42601

Error Code : 0

Message    : ERROR: syntax error at or near "GENERATED"

  Position: 45

Location   : db/migration/V1__initial_user.sql (/Users/villemossip/Desktop/GRP/GRP-SAS/application/build/resources/main/db/migration/V1__initial_user.sql)

Line       : 36

Statement  : CREATE TABLE revinfo

(

    rev      INTEGER GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ),

    revtstmp BIGINT,

    PRIMARY KEY (rev)

)


胡子哥哥
浏览 276回答 3
3回答

慕沐林林

看起来带有数据库的测试容器已成功启动,因此没有问题,您将得到一个空数据库。然后你尝试运行飞行路线,但失败了。Spring Boot中的Flyway在Spring应用程序上下文初始化期间工作,因此实际迁移在应用程序上下文初始化时运行,因此迁移失败看起来像Spring失败。但是,记录了原因:迁移文件包含无效内容:Migration V1__initial_user.sql failed-------------------------------------SQL State  : 42601Error Code : 0Message    : ERROR: syntax error at or near "GENERATED" Position: 45Location   : db/migration/V1__initial_user.sql (/Users/villemossip/Desktop/GRP/GRP- SAS/application/build/resources/main/db/migration/V1__initial_user.sql)Line       : 36Statement  : CREATE TABLE revinfo(   rev      INTEGER GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ),   revtstmp BIGINT,   PRIMARY KEY (rev))这GENERATED BY是不受支持的。为什么?您的 docker 映像可能包含不支持此语法的 RDBMS 版本。所以它和你在没有docker的本地环境中使用的DB不同。无论如何,这不是关于 docker、spring 或 Flyway 的问题,而是关于数据库和迁移代码的问题。在分辨率方面,我建议直接运行DB的docker镜像(不带java、testcontainers和flyway)。当它运行时,只需在 pgadmin 或其他东西中“手动”运行此迁移即可。您预计会看到相同的错误。

天涯尽头无女友

就我而言,我在 application.properties 文件中添加了spring.flyway.baselineOnMigrate = true我还必须从版本 1.1 而不是 1 开始,否则 Flyway 会抛出错误(flyway 8.0.5)

慕森卡

我发现问题出在 Postgres 版本上。由于某种原因,默认情况下,docker 镜像是使用旧版本 9.6.12 创建的,但 SQL 脚本GENERATED BY DEFAULT已添加到版本 10 的 Postgres 中。解决方案1(将sql脚本更新到旧版本):CREATE TABLE revinfo(&nbsp; &nbsp; rev&nbsp; &nbsp; &nbsp; INTEGER PRIMARY KEY NOT NULL,&nbsp; &nbsp; revtstmp BIGINT);解决方案2: 通过在项目中创建CustomPostgreSQLContainer文件将docker镜像版本更改为11.2。import org.testcontainers.containers.PostgreSQLContainer;public class CustomPostgreSQLContainer extends PostgreSQLContainer<CustomPostgreSQLContainer> {&nbsp; &nbsp; private static final String IMAGE_VERSION = "postgres:11.2";&nbsp; &nbsp; private static CustomPostgreSQLContainer container;&nbsp; &nbsp; CustomPostgreSQLContainer() {&nbsp; &nbsp; &nbsp; &nbsp; super(IMAGE_VERSION);&nbsp; &nbsp; }&nbsp; &nbsp; public static CustomPostgreSQLContainer getInstance() {&nbsp; &nbsp; &nbsp; &nbsp; if (container == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; container = new CustomPostgreSQLContainer();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return container;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void start() {&nbsp; &nbsp; &nbsp; &nbsp; super.start();&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("spring.datasource.url", container.getJdbcUrl());&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("spring.datasource.username", container.getUsername());&nbsp; &nbsp; &nbsp; &nbsp; System.setProperty("spring.datasource.password", container.getPassword());&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void stop() {&nbsp; &nbsp; &nbsp; &nbsp; //do nothing, JVM handles shut down&nbsp; &nbsp; }}并更新 BaseIntTest 文件:@Testcontainers@SpringBootTestpublic class BaseIntTest {&nbsp; &nbsp; @Container&nbsp; &nbsp; private static final PostgreSQLContainer<?> container = CustomPostgreSQLContainer.getInstance();最后从测试 application.properties 文件中删除两行:spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriverspring.datasource.url=jdbc:tc:postgresql://localhost:5433/test
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java