This post talks about how we integrated Flyway with Spring Boot and enjoyed the power of database migration seamlessly. If you have worked on Enterprise applications, data migration is really liability for your project. One wrong move, your data get lost during your production patch fixes or release updates. To mitigate such issues happening over manual migrations or even in-house programmatic data migration, we adopted Flyway, an open-source database migration tool that favors simplicity and convention over configuration. Luckily, Spring Boot naturally gels with Flyway.
Flyway : 4.2.0
Spring Boot : 1.5.6.RELEASE
Assumption : Java, MySQL & Maven Used Projects
Flyway Maven Dependency
Flyway has Maven dependency that we need to specify in the pom.xml while we configure spring-boot-starter dependencies. This makes sure Spring Boot look for database migration scripts in the classpath. The default lookout path is src/main/resources/db/migration and the scripts are named as per Flyway convention. You can find more about Flyway from this and this.
Spring Boot in Action
Now that Flyway is in the classpath along with the migration scripts, Spring Boot really wants to see if the migration is necessary while starting to boot the application. So its important that we configure our datasource for connecting, and migrating the scripts. The datasource details can be configured pro-grammatically or in the application.properties file. We can choose any approach as it deems fit. You can find more about Spring Boot Flyway migration from this.
Issue on Database Creation
One of the issues or convention we used to follow in Application Development is that we create database schema even before Flyway data migration. This is just to ensure data source holds the schema to connect and apply the Flyway migrations. But this seemed to cause annoying application failures while booting since we may have missed to create the database in any development environment. As a result, it took quite a lot of time to understand the cause. Flyway, out of the box, provides support to create schema while migration itself.
Spring Boot Flyway Configuration
Flyway uses Spring Boot primary datasource by default unless it has its own datasource defined. Flyway also does not need any schema information in the datasource url. Hence we decided to create two different datasources, one for Flyway that does not contain schema information in the url, another one for Spring Boot primary datasource that contains the schema name. If you want to try this in programmatic way, you can define a @Bean with @FlywayDataSource that Flyway can use for data migration. Also you must need to define another @Bean with @Primary. Makinus applications used to configure through application.properties, hence we did the following.
#Flyway
flyway.url = jdbc:mysql://localhost:3306/
flyway.user = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
flyway.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
flyway.schemas = TEST
flyway.locations = classpath:db/migration
#Primary Datasource props
spring.datasource.url = jdbc:mysql://localhost:3306/TEST
spring.datasource.username = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
spring.datasource.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.type = com.zaxxer.hikari.HikariDataSource
This solved the problem of creating database schema even before deploying the Spring Boot application.
Flyway : 4.2.0
Spring Boot : 1.5.6.RELEASE
Assumption : Java, MySQL & Maven Used Projects
Flyway Maven Dependency
Flyway has Maven dependency that we need to specify in the pom.xml while we configure spring-boot-starter dependencies. This makes sure Spring Boot look for database migration scripts in the classpath. The default lookout path is src/main/resources/db/migration and the scripts are named as per Flyway convention. You can find more about Flyway from this and this.
Spring Boot in Action
Now that Flyway is in the classpath along with the migration scripts, Spring Boot really wants to see if the migration is necessary while starting to boot the application. So its important that we configure our datasource for connecting, and migrating the scripts. The datasource details can be configured pro-grammatically or in the application.properties file. We can choose any approach as it deems fit. You can find more about Spring Boot Flyway migration from this.
Issue on Database Creation
One of the issues or convention we used to follow in Application Development is that we create database schema even before Flyway data migration. This is just to ensure data source holds the schema to connect and apply the Flyway migrations. But this seemed to cause annoying application failures while booting since we may have missed to create the database in any development environment. As a result, it took quite a lot of time to understand the cause. Flyway, out of the box, provides support to create schema while migration itself.
Spring Boot Flyway Configuration
Flyway uses Spring Boot primary datasource by default unless it has its own datasource defined. Flyway also does not need any schema information in the datasource url. Hence we decided to create two different datasources, one for Flyway that does not contain schema information in the url, another one for Spring Boot primary datasource that contains the schema name. If you want to try this in programmatic way, you can define a @Bean with @FlywayDataSource that Flyway can use for data migration. Also you must need to define another @Bean with @Primary. Makinus applications used to configure through application.properties, hence we did the following.
#Flyway
flyway.url = jdbc:mysql://localhost:3306/
flyway.user = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
flyway.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
flyway.schemas = TEST
flyway.locations = classpath:db/migration
#Primary Datasource props
spring.datasource.url = jdbc:mysql://localhost:3306/TEST
spring.datasource.username = ENC(pcs/pbUbvfWuVCPcFr2QaA==)
spring.datasource.password = ENC(dS+A9wuvtE1KkgVv1nyjUg==)
spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.type = com.zaxxer.hikari.HikariDataSource
This solved the problem of creating database schema even before deploying the Spring Boot application.
Amazing post.Thanks for your details and explanations..I want more information from your side.Thank you
ReplyDeleteGuest posting sites
Technology
Excellent post! This post was delivered a lot of information. Thanks for the effort to share this with us. Keep updating.
ReplyDeletePrimavera Training in Chennai
Primavera Coaching in Chennai
Corporate Training in Chennai
Pega Training in Chennai
Spark Training in Chennai
Embedded System Course Chennai
Linux Training in Chennai
Graphic Design Courses in Chennai
Social Media Marketing Courses in Chennai
Soft Skills Training in Chennai
JMeter Training in Chennai
Primavera Training in Anna Nagar
I read your post and got it quite informative. IoT app development
ReplyDelete