Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Hibernate and Flyway boot order

Tags:

I have created Spring application. Pom xml is attached.

It has a config like this (below) and some db/migration/V1__init.sql for Flyway db migration tool.

It has hsqldb in-memory database and it is created after application is started. It is clean after creation.

I want Hibernate to create a schema based on entity classes and then Flyway fills the tables. Now Flyway starts V1__init.sql before tables is created and throw an exception. How can I change this order or what solution can I do?

spring.datasource.testWhileIdle = true spring.datasource.validationQuery = SELECT 1 spring.jpa.show-sql = true spring.jpa.hibernate.ddl-auto = create-drop spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect 

pom.xml:

<parent>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-parent</artifactId>     <version>1.3.3.RELEASE</version>     <relativePath/> </parent>  <properties>     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>     <java.version>1.8</java.version> </properties>  <dependencies>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-web</artifactId>         <version>1.3.2.RELEASE</version>     </dependency>     <dependency>         <groupId>org.hsqldb</groupId>         <artifactId>hsqldb</artifactId>         <scope>runtime</scope>     </dependency>     <dependency>         <groupId>org.hibernate</groupId>         <artifactId>hibernate-core</artifactId>         <version>4.3.11.Final</version>     </dependency>     <dependency>         <groupId>org.springframework</groupId>         <artifactId>spring-orm</artifactId>         <version>4.2.5.RELEASE</version>     </dependency>     <dependency>         <groupId>org.thymeleaf</groupId>         <artifactId>thymeleaf-spring4</artifactId>         <version>2.1.4.RELEASE</version>     </dependency>      <!-- For using 'LEGACYHTML5' mode in Thymeleaf -->     <dependency>         <groupId>net.sourceforge.nekohtml</groupId>         <artifactId>nekohtml</artifactId>         <version>1.9.21</version>     </dependency>     <dependency>         <groupId>xml-apis</groupId>         <artifactId>xml-apis</artifactId>         <version>1.4.01</version>     </dependency>      <dependency>         <groupId>org.flywaydb</groupId>         <artifactId>flyway-core</artifactId>     </dependency>     <dependency>         <groupId>org.springframework.boot</groupId>         <artifactId>spring-boot-starter-data-jpa</artifactId>         <version>1.3.3.RELEASE</version>     </dependency> </dependencies>  <build>     <plugins>         <plugin>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-maven-plugin</artifactId>         </plugin>     </plugins> </build> 
like image 999
user3636486 Avatar asked May 08 '16 08:05

user3636486


People also ask

What is Flyway out of order?

Flyway 2 has flyway. outOfOrder property which is by default false You would have to set it to true to run your missing migration, as per migrate docs: Allows migrations to be run "out of order". If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.

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?

Using Flyway with Spring Boot Flyway is another open-source library used for Database Migration / Version Control for the DB scripts. It allows us to migrate the changes to DB incrementally by versioning and it supports migrations in SQL or Java migration ( for advanced data transformations or dealing with LOB).

Does Flyway lock table?

Flyway doesn't lock the schema. It then acquires a lock on the metadata table using SELECT * FROM metadatatable FOR UPDATE . This lock is released automatically after the migration completes when the transaction is commited or rolled back.


1 Answers

I had the same issue.

I wanted my schema to be created by hibernate because of it's database independence. I already went through the trouble of figuring out a nice schema for my application in my jpa classes, I don't like repeating myself.

But I want some data initialization to be done in a versioned manner which flyway is good at.

Spring boot runs flyway migrations before hibernate. To change it I overrode the spring boot initializer to do nothing. Then I created a second initializer that runs after hibernate is done. All you need to do is add this configuration class:

import org.flywaydb.core.Flyway; import org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn;  @Configuration public class MigrationConfiguration {       /**      * Override default flyway initializer to do nothing      */     @Bean     FlywayMigrationInitializer flywayInitializer(Flyway flyway) {         return new FlywayMigrationInitializer(flyway, (f) ->{} );     }       /**      * Create a second flyway initializer to run after jpa has created the schema      */     @Bean     @DependsOn("entityManagerFactory")     FlywayMigrationInitializer delayedFlywayInitializer(Flyway flyway) {         return new FlywayMigrationInitializer(flyway, null);     }   } 

That code needs java 8, If you have java 7 or earlier, replace (f)->{} with an inner class that implements FlywayMigrationStrategy

Of course you can do this in xml just as easily.

Make sure to add this to your application.properties:

flyway.baselineOnMigrate = true 
like image 160
user3707816 Avatar answered Oct 14 '22 08:10

user3707816