Flyway:找到没有模式历史记录表的非空模式“ public”!使用基线()-在空数据库上

Pau*_*ulB 8 flyway spring-boot

我正在尝试使用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')
}
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

使用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.
Run Code Online (Sandbox Code Playgroud)

我的目录结构是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() }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Ali*_*ien 14

假设您正在使用spring-boot版本2。

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

spring.flyway.baseline-on-migrate = true

要么

spring.flyway.baselineOnMigrate = true

  • +1。这也可以使用环境变量“FLYWAY_BASELINE_ON_MIGRATE=t​​rue”来完成。请参阅 https://flywaydb.org/documentation/configuration/parameters/baselineOnMigrate。 (7认同)