[英]Flyway and Spring Boot integration
我試圖將 Flyway 集成到 Spring Boot 項目中,以使用 Hibernate 和 Spring JPA 進行遷移。 我收到以下異常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' 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 "PUBLIC" without metadata table! Use init() or set initOnMigrate to true to initialize the metadata table.
我的pom.xml
看起來像這樣:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>3.2</version>
</dependency>
我正在使用 Hibernate 和一個用於 postgres(開發階段)和 h2(本地)的配置 java 文件。 簽名看起來像這樣:
@Bean(initMethod = "migrate")
public Flyway flyway() {
Flyway fly = new Flyway();
fly.clean();
fly.init();
//flyway.setInitOnMigrate(true);
fly.setSchemas("SBA_DIALOG");
//flyway.setLocations("filesystem:src/main/resources/db/migration");
fly.setDataSource(this.dataSource());
fly.migrate();
return fly;
}
@Bean(name = "sbaEntityManagerFactory") @DependsOn("flyway")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
...
我找不到有關此問題中描述的問題的任何信息。 有人可以幫忙嗎?
Spring-Boot 能夠自己做到這一點。 只需將 flyway 作為依賴項添加到您的項目中,spring-boot 就會將其選中。 Flyway 遷移將在服務啟動時開始。
如果數據庫中已經有一些表,請添加:
spring.flyway.baselineOnMigrate = true
在您的屬性文件中,以在發現某些表已存在時保持 flyway 平靜。 ;-)
Flyway 應該拿起你的數據源。 如果您需要例如另一個用戶或類似 flyway 的用戶,您可以設置這些屬性:
spring.flyway.url: jdbc:postgresql://${db.host}/${db.name}
spring.flyway.user: MYUSER
spring.flyway.password: MYPWD
(當然添加您的值!您可以使用 SPEL 來引用其他屬性)
更新
一個警告:如果您使用集群數據庫,您可能會遇到同時啟動的多個實例嘗試同時執行更新的問題。 當表鎖不起作用時,這是一個問題,這在我使用集群 mariaDB 時發生。
對於任何想要在 Java 代碼中解決它的人,您可以使用:
fly.setBaselineOnMigrate(true);
另一個解決方案也是:
spring:
flyway:
baselineOnMigrate: true
validateOnMigrate: false
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.