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

问题描述 投票:0回答:5

我正在尝试使用 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找到的示例迁移的 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-boot flyway
5个回答
93
投票

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

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

spring
,如下所示。

spring.flyway.baseline-on-migrate = true

spring.flyway.baselineOnMigrate = true


3
投票

也许你可以尝试

 mvn flyway:clean && mvn flyway:migrate


1
投票

请检查数据库的搜索路径,如果公共模式(flyway 在其上创建日志表)不是首先,它可能无法找到日志表,并且可能会抱怨找不到模式历史记录...

请注意,如果您正在基线,则需要从脚本文件夹中删除旧脚本,否则它将重新尝试。


0
投票

当回收站中仍有东西时,也会出现相同的错误消息。清空架构后,回收站中仍然有碎片,并且还收到错误:

Found non-empty schema(s) "public" without schema history table! Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

清除回收站后,错误消失。这是在具有 Flyway 9.5.1 的 Oracle 19c 数据库上。


0
投票

我需要清理本地 Postgres 数据库。因此,我删除了数据库中的所有表和视图,然后尝试运行应用程序。但后来我得到了这个问题中描述的 FlyWay 错误。

唯一的区别是数据库清理后我有 40 多个 FlyWay 迁移需要处理。

不幸的是,接受的答案中的属性并没有帮助“按原样”,因为 FlyWay 试图创建编号为#1 的基线迁移,但我已经有了自己的编号为#1 的迁移。经过一番小调查,我找到了治疗方法:

spring.flyway.baseline-on-migrate = true
spring.flyway.baseline-version = 0

现在应用程序启动时没有错误,并且所有迁移均已成功处理。

© www.soinside.com 2019 - 2024. All rights reserved.