首页 > 解决方案 > Flyway:找到没有模式历史表的非空模式“公共”!使用 baseline() - 在空数据库上

问题描述

我正在尝试使用 kotlin Spring boot、jpa 和 postgreSQL 配置 flyway。我的 gradle 依赖项是:

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

我的 application.properties 文件是:

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

使用 jpa 和 hibernate 创建表和条目按预期工作。但是,空数据库上的示例迁移会导致:

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.

我的目录结构是 spring initializr 生成的默认目录结构,我的迁移位于:demo/src/main/kotlin/db/migration

我只有一个迁移,它是此处找到的示例迁移的 kotlinized 版本,我对其进行了调整以使其看起来像这样:

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

我在这里想念什么?当数据库完全为空(干净的 docker 映像)时,为什么 Flyway 一直抱怨在没有模式历史记录表的情况下找到非空模式“公共”?

标签: spring-bootflyway

解决方案


假设您使用的是 spring-boot 版本 2。

在 Spring Boot 2 中,前缀是“spring.flyway”,所以尝试添加spring如下前缀。

spring.flyway.baseline-on-migrate = true

或者

spring.flyway.baselineOnMigrate = true


推荐阅读