Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway and Spring Boot integration

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?

like image 779
imalik8088 Avatar asked Mar 25 '15 13:03

imalik8088


People also ask

How do you implement a Flyway in a spring boot?

Configuring Flyway Database gradle file. In application properties, we need to configure the database properties for creating a DataSource and also flyway properties we need to configure in application properties. For properties file users, add the below properties in the application. properties file.

What is Flyway used for in spring boot?

Spring Boot integrates with Flyway, one of the most extensively used database migration tools, to make database migrations easier. Flyway is a tool that allows you to version control incremental changes to your database such that you can easily and completely migrate to a new version.

Why Flyway is required?

Flyway is an open-source tool, licensed under Apache License 2.0, that helps you implement automated and version-based database migrations. It allows you to define the required update operations in an SQL script or as Java code.


2 Answers

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.

like image 111
Patrick Cornelissen Avatar answered Oct 04 '22 12:10

Patrick Cornelissen


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 
like image 20
YCF_L Avatar answered Oct 04 '22 14:10

YCF_L