spring-boot - 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-boot 版本 2。
在 Spring Boot 2 中,前缀是“spring.flyway”,所以尝试添加spring
如下前缀。
spring.flyway.baseline-on-migrate = true
或者
spring.flyway.baselineOnMigrate = true
推荐阅读
- assembly - 在汇编中,如何在节的开头插入数据(或代码或符号)?
- azure-devops - 如何在 Azure DevOps 中限制对管道的访问
- md5 - 为什么解密 MD5 校验和如此容易
- php - PHP mysqli_fetch_assoc() 函数在同一个 PHP 文件工作时返回 NULL
- javascript - 我如何正确更改 React 中元素的样式,而不会随着我更新状态而不断更改?
- elm - Elm:属性“onerror”改为添加“data-onerror”属性
- php - 如何将相同质量的html转换为jpg
- c - 检查矩阵中的行数是否等于c中的给定行
- python - 在 3D 数组中输入
- mysql - MySQL:所有用户一个数据库或每个用户多个数据库