spring-bootflyway

Flyway: Found non-empty schema(s) "public" without schema history table! Use baseline() - on Empty database


I am trying to configure flyway with kotlin Spring boot, jpa and postgreSQL. My gradle dependencies are:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-data-jpa')
    implementation('org.springframework.boot:spring-boot-starter-web')
    implementation('com.fasterxml.jackson.module:jackson-module-kotlin')
    implementation('org.flywaydb:flyway-core')
    implementation('com.google.code.gson:gson')
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    runtimeOnly('org.postgresql:postgresql')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
}

My application.properties file is:

spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://${JDBC_DATABASE_URL}/jpaTestDatabase
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}

flyway.baseline-on-migrate=true
flyway.locations=classpath:src/main/kotlin/db/migration

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=validate
spring.session.store-type=none

Creating tables and entries using jpa and hibernate works as expected. However a sample migration on an empty database results in:

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.api.FlywayException: 
Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

my directory structure is the default one generated by spring initializr and my migrations are in: demo/src/main/kotlin/db/migration

I only have a single migration which is the kotlinized version of the example migration found here which I adapted to look line this:

class V1__Sample : BaseJavaMigration() {
  override fun migrate(context: Context?) {
    val statement = context?.connection?.prepareStatement(
      """
        CREATE TABLE article (
          id bigserial primary key,
          name varchar(20) NOT NULL,
          desc text NOT NULL
        );
      """
    )
    statement.use { it?.execute() }
  }
}

What am I missing here? Why does Flyway keep complaining about finding non-empty schema(s) "public" without schema history table, when the database is completelly empty (clean docker image)?


Solution

  • Assuming that you are using spring-boot version 2.

    In spring boot 2 the prefix is "spring.flyway" so try adding prefix spring like below.

    spring.flyway.baseline-on-migrate = true

    OR

    spring.flyway.baselineOnMigrate = true