Flyway和Spring Boot集成 [英] Flyway and Spring Boot integration

查看:1848
本文介绍了Flyway和Spring Boot集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Hibernate和Spring JPA的Spring Boot项目中集成Flyway进行迁移。我收到以下异常:

  org.springframework.beans.factory.BeanCreationException:创建名为'flyway'的bean时出错在类路径资源中定义[org / springframework / boot / autoconfigure / flyway / FlywayAutoConfiguration $ FlywayConfiguration.class]:调用init方法失败;嵌套异常是org.flywaydb.core.api.FlywayException:发现没有元数据表的非空模式PUBLIC!使用init()或将initOnMigrate设置为true来初始化元数据表。 

我的pom.xml看起来像这样:

 <依赖性> 
< groupId> org.flywaydb< / groupId>
< artifactId> flyway-core< / artifactId>
< version> 3.2< / version>
< /依赖关系>

我为postgres(dev stage)和h2(local)使用Hibernate和配置java文件, 。签名看起来像这样:

  @Bean(initMethod =migrate)
public Flyway flyway(){
Flyway fly =新的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();
返航;

@Bean(name =sbaEntityManagerFactory)@DependsOn(flyway)
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
...

我无法找到任何有关此问题中描述的问题的信息。
任何人都可以提供帮助吗?

解决方案

Spring-Boot可以自行完成这项工作。
只需将flyway作为依赖项添加到您的项目中,spring-boot就会启动它。
当服务启动时,Flyway迁移将开始。



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

  flyway.baseline-on-migrate = true 



<在你的财产档案中,当它发现某些表已经存在时,让它保持冷静。 ; - )

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

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

$ b

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

更新



请注意:如果您使用群集数据库,则可能会遇到以下问题:同时启动的多个实例尝试在同时。当表锁不起作用时,这是一个问题,发生在使用集群mariaDB的情况下。


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

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.xml is looking like this:

<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:

  @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?

解决方案

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.

If you already have some tables in the database add:

flyway.baseline-on-migrate=true

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

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.url: jdbc:postgresql://${db.host}/${db.name}
flyway.user: MYUSER
flyway.password: MYPWD

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

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.

这篇关于Flyway和Spring Boot集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆