36

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?

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85
imalik8088
  • 1,501
  • 5
  • 21
  • 39
  • 5
    Spring Boot already supports flyway the only thing you need is add flyway as a dependency... I suggest a read of the [manual](http://docs.spring.io/spring-boot/docs/current/reference/html/howto-database-initialization.html#howto-execute-flyway-database-migrations-on-startup) and for the [configuration properties](http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html). For me it looks like you are using boot and try very hard to work around it (explicitly configuring everything for instance). – M. Deinum Mar 25 '15 at 18:48
  • 1
    quite strange: spring boot should not start flyway autoconfiguration if you configure flyway for yourself (question is if it is necessary). however error comes from boot autoconfiguration. however try to add `flyway.initOnMigrate= true` to your application.properties and remove your own flyway initialization :) – sodik Mar 31 '15 at 09:19
  • @sodik Actually I've flyway in classpath (3.2.1) and it does not work. What may be the problem? – Opal Jul 22 '15 at 14:52
  • for me the issue was the flyway and spring version missmatch – Tiberiu Popescu Dec 02 '19 at 14:57

2 Answers2

57

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:

spring.flyway.baselineOnMigrate = 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:

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)

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.

Patrick Cornelissen
  • 7,968
  • 6
  • 48
  • 70
  • 11
    Use `flyway.baseline-on-migrate=true` instead. `flyway.initOnMigrate` is deprecated. – Sergei Ledvanov Jan 11 '16 at 13:55
  • @SergeiLedvanov What are you talking about? The documentation says `flyway.baselineOnMigrate`. https://flywaydb.org/documentation/configfiles – Chloe May 10 '18 at 00:03
  • I assume that the config-property-classes are used there and it is able to cope with snake_case, kebab-case and camelCase – Patrick Cornelissen May 10 '18 at 18:51
  • with spring boot you use spring.flyway.baseline-on-migrate = true – Anna Klein Sep 27 '18 at 11:26
  • For spring boot 2.X.X you need to use prefix all flyway properties values with spring in this case spring.flyway.basline-on-migrate – IBEANS Oct 09 '18 at 18:46
  • 1
    Use spring.flyway.baselineOnMigrate = true instead. flyway.baseline-on-migrate=true and flyway.initOnMigrate are deprecated. – cammando Jul 15 '19 at 11:17
7

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

fly.setBaselineOnMigrate(true);

Edit(22-09-2020)

another solution also is :

spring:
  flyway:
    baselineOnMigrate: true
    validateOnMigrate: false
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140