Flyway:找到没有架构历史记录 table 的非空架构 "public"!使用 baseline() - 在空数据库上
Flyway: Found non-empty schema(s) "public" without schema history table! Use baseline() - on Empty database
我正在尝试使用 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 创建 tables 和条目按预期工作。
但是,空数据库上的示例迁移会导致:
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 版本here,我将其改编成如下行:
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() }
}
}
我在这里错过了什么?为什么 Flyway 一直抱怨在没有模式历史 table 的情况下找到非空模式 "public",当数据库完全为空时(干净 docker 图像)?
假设您使用的是 spring-引导版本 2.
在 spring boot 2 中前缀是 "spring.flyway" 所以尝试添加前缀 spring
如下所示。
spring.flyway.baseline-on-migrate = true
或
spring.flyway.baselineOnMigrate = true
也许你可以试试 mvn flyway:clean && mvn flyway:migrate
我正在尝试使用 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 创建 tables 和条目按预期工作。 但是,空数据库上的示例迁移会导致:
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 版本here,我将其改编成如下行:
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() }
}
}
我在这里错过了什么?为什么 Flyway 一直抱怨在没有模式历史 table 的情况下找到非空模式 "public",当数据库完全为空时(干净 docker 图像)?
假设您使用的是 spring-引导版本 2.
在 spring boot 2 中前缀是 "spring.flyway" 所以尝试添加前缀 spring
如下所示。
spring.flyway.baseline-on-migrate = true
或
spring.flyway.baselineOnMigrate = true
也许你可以试试 mvn flyway:clean && mvn flyway:migrate