Java Flyway 和 Spring Boot 集成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29257440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:40:46  来源:igfitidea点击:

Flyway and Spring Boot integration

javaspringhibernatespring-bootflyway

提问by imalik8088

I trying to integrate Flyway for migrations in a Spring Boot project with Hibernate and Spring JPA. I'm getting the following Exception:

我试图将 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.

My pom.xmlis looking like this:

pom.xml的看起来像这样:

<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
  <version>3.2</version>
</dependency>

I'm using Hibernate and a config java file for postgres (dev stage) and h2 (local). The signatures are looking like this:

我正在使用 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() {
...

I can't find anything about my problem described in this question. Can anybody help?

我找不到有关此问题中描述的问题的任何信息。有人可以帮忙吗?

回答by Patrick Cornelissen

Spring-Boot is capable on it's own to do this. Just add flyway as dependency to your project and spring-boot will pick it up. Flyway migration will start when the service starts up.

Spring-Boot 能够自己做到这一点。只需将 flyway 作为依赖项添加到您的项目中,spring-boot 就会将其选中。Flyway 迁移将在服务启动时开始。

If you already have some tables in the database add:

如果数据库中已经有一些表,请添加:

spring.flyway.baselineOnMigrate = true 

in your property file to keep flyway calm when it discovers that some tables already exist. ;-)

在您的属性文件中,以在发现某些表已存在时保持 flyway 平静。;-)

Flyway should pick up your datasource. If you need for example another user or something like that for flyway, you can set these properties:

Flyway 应该拿起你的数据源。如果您需要例如另一个用户或类似 flyway 的用户,您可以设置这些属性:

spring.flyway.url: jdbc:postgresql://${db.host}/${db.name}
spring.flyway.user: MYUSER
spring.flyway.password: MYPWD

(Of course add your values! You can use SPEL to reference other properties)

(当然添加您的值!您可以使用 SPEL 来引用其他属性)

Update

更新

One word of caution: If you use a clustered database you may encounter problems that multiple instances that are started at the same time try to perform the updates at the same time. This is a problem when the table locks don't work, which happened to me using a clustered mariaDB.

一个警告:如果您使用集群数据库,您可能会遇到同时启动的多个实例尝试同时执行更新的问题。当表锁不起作用时,这是一个问题,这在我使用集群 mariaDB 时发生。

回答by YCF_L

For any one who want to solve it in java code you can use :

对于任何想要在 Java 代码中解决它的人,您可以使用:

fly.setBaselineOnMigrate(true);