BeanCreationException: Error creating bean with name 'flywayInitializer' BeanCreationException: Error creating bean with name 'flywayInitializer' docker docker

BeanCreationException: Error creating bean with name 'flywayInitializer'


It looks like the test container with the database has started successfully, so no issue there, you're getting an empty database.

Then you try running the flyway and this fails.Flyway in spring boot works during the initialization of the spring application context,so the actual migration runs while the application context gets initialized, so the migration failure looks like a spring failure.

The reason, however, is logged: the migration file has an invalid content:

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))

This GENERATED BY is unsupported.

Why? Probably your docker image includes the version of RDBMS that doesn't support this syntax. So it differs from the DB that you use in a local environment without docker.

In any case it's not about docker, spring or flyway but about the DB and the migration code.

In terms of resolution, I suggest running the docker image of the DB directly (without java, testcontainers and flyway).When it runs, just run this migration "manually" in pgadmin or something. You're expected to see the same error.


Thank you @M. Deinum and Mark Bramnik!

I found out that the issue is with Postgres version. For some reason by default docker image is created with old version 9.6.12, but sql script GENERATED BY DEFAULT was added to Postgres with version 10.

Solution 1 (Update the sql script to older version):

CREATE TABLE revinfo(    rev      INTEGER PRIMARY KEY NOT NULL,    revtstmp BIGINT);

Solution 2:Changed docker image version to 11.2 by creating CustomPostgreSQLContainer file in the project.

import org.testcontainers.containers.PostgreSQLContainer;public class CustomPostgreSQLContainer extends PostgreSQLContainer<CustomPostgreSQLContainer> {    private static final String IMAGE_VERSION = "postgres:11.2";    private static CustomPostgreSQLContainer container;    CustomPostgreSQLContainer() {        super(IMAGE_VERSION);    }    public static CustomPostgreSQLContainer getInstance() {        if (container == null) {            container = new CustomPostgreSQLContainer();        }        return container;    }    @Override    public void start() {        super.start();        System.setProperty("spring.datasource.url", container.getJdbcUrl());        System.setProperty("spring.datasource.username", container.getUsername());        System.setProperty("spring.datasource.password", container.getPassword());    }    @Override    public void stop() {        //do nothing, JVM handles shut down    }}

And updating BaseIntTest file:

@Testcontainers@SpringBootTestpublic class BaseIntTest {    @Container    private static final PostgreSQLContainer<?> container = CustomPostgreSQLContainer.getInstance();

And last removing two lines from test application.properties file:

spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriverspring.datasource.url=jdbc:tc:postgresql://localhost:5433/test


The problem in my case was because of using spring.datasource.url=jdbc:postgres://localhost:5432/todoListDbinstead ofspring.datasource.url=jdbc:postgresql://localhost:5432/todoListDb

(postgres instead of PostgreSQL) in my application.properties file

So try to use the right URL for your database and verify if there is a typo in your url