【问题标题】:Flyway: Found non-empty schema(s) "public" without schema history table! Use baseline() - on Empty databaseFlyway:找到没有模式历史表的非空模式“公共”!使用 baseline() - 在空数据库上
【发布时间】:2019-04-09 20:37:15
【问题描述】:

我正在尝试使用 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

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

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-boot flyway


    【解决方案1】:

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

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

    spring.flyway.baseline-on-migrate = true

    spring.flyway.baselineOnMigrate = true

    【讨论】:

    【解决方案2】:

    也许你可以试试 mvn flyway:clean && mvn flyway:migrate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-23
      • 2022-01-24
      • 2019-06-29
      • 2014-04-10
      • 2022-08-22
      • 2018-12-12
      • 2020-12-21
      • 2013-04-20
      相关资源
      最近更新 更多