

Table of Contents
1. Overview
In this article, We will learn about Spring boot Flyway Java Based Migration Example. Flyway migration tool provides SQL and Java based migration. In this article, we learn about spring boot SQL based migration example.
- In SQL based migration we can write migration using
.SQL
file..SQL
will be executed in a sequential manner by Flyway tool. If we require some business logic to migrate existing data then? Here Java based migration will help us. - In Java based migration will need to create
.Java
file which implementsSpringJdbcMigration
interface.
Can we configure JAVA and SQL based migration in the same application? :
Yes. We can configure Java and SQL based migration in same application but we need to take create about file version name.
Steps to configure Java based migration:
- Create directory
src/main/java/db/migration
directory. In short/db/migration
must be available in our classpath. - Create Java inside
src/main/java/db/migration
directory which must be implemented
for spring application, For non-spring application interface isSpringJdbcMigration
JdbcMigration
:
src/main/java/db/migration/V4__Another_user
package db.migration; import org.flywaydb.core.api.migration.spring.SpringJdbcMigration; import org.springframework.jdbc.core.JdbcTemplate; /** * Created by JavaDeveloperZone on 12-05-2018. */ public class V4__Another_user implements SpringJdbcMigration{ @Override public void migrate(JdbcTemplate jdbcTemplate) throws Exception { jdbcTemplate.execute("INSERT INTO employee\n" + "(employeeId, employeeName, employeeRole)\n" + "VALUES (14, 'Zone', 'ADMN');"); } }
Here are rules for the name of a class file. It also available in flyway official document.

Java File Name Rules
NOTE: We can configure same with Spring or Spring MVC Application without Spring boot also. We have used here spring boot to take advantages of configuration.
2. Example

Spring boot Flyway Java Based Migration Example
2.1 pom.xml
We have added flyway-core
dependency for flyway.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-boot-example</groupId> <artifactId>Spring-boot-flaway-database-migration-example</artifactId> <version>1.0-SNAPSHOT</version> <description>Spring boot Flyway Java Based Migration Example</description> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <!--It contains database base related classes--> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>5.0.6</version> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.2 application.properties
flyway.*
are flyway related properties. flyway.url, flyway.user, flyway.password
are optional. If we have a separate user for database migration then we can specify using those properties. flyway automatically find the default data source when we will not specify flyway.url, flyway.user, flyway.password.
spring.datasource.url=jdbc:mysql://localhost/demo_database_1 spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver debug=true flyway.baseline-on-migrate=true #used if database has some already table flyway.enabled=true flyway.url=jdbc:mysql://localhost/demo_database_1 flyway.user=root flyway.password=
2.3 db/migration/V4__Another_user.java
Here we have implements SpringJdbcMigration
interface and override migrate() method, Flyway will automatically call migrate() method where we can write our business logic for migration. JdbcTemplate
will be passed so we can execute the query using that.
package db.migration; import org.flywaydb.core.api.migration.spring.SpringJdbcMigration; import org.springframework.jdbc.core.JdbcTemplate; /** * Created by JavaDeveloperZone on 12-05-2018. */ public class V4__Another_user implements SpringJdbcMigration{ @Override public void migrate(JdbcTemplate jdbcTemplate) throws Exception { jdbcTemplate.execute("INSERT INTO employee\n" + "(employeeId, employeeName, employeeRole)\n" + "VALUES (14, 'Zone', 'ADMN');"); } }
2.4 resources/db/migration/V1__Create_Employee_Table.sql
CREATE TABLE `employee` ( `employeeId` int(11) NOT NULL AUTO_INCREMENT, `employeeName` varchar(255) DEFAULT NULL, `employeeRole` varchar(255) DEFAULT NULL, PRIMARY KEY (`employeeId`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
2.5 resources/db/migration/V2__Employee_Insert.sql
INSERT INTO employee (employeeId, employeeName, employeeRole) VALUES (12, 'Zone', 'ADMN');
2.6 resources/db/migration/V3__Employee_Insert.sql
INSERT INTO employee (employeeId, employeeName, employeeRole) VALUES (12, 'Zone', 'ADMN');
2.7 Demo
Let’s run spring boot application:
mvn spring-boot:run
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.8.RELEASE) 2018-05-12 16:49:54.895 INFO 115180 --- [ main] o.f.core.internal.util.VersionPrinter : Flyway Community Edition 5.0.6 by Boxfuse 2018-05-12 16:49:55.161 INFO 115180 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost/demo_database_1 (MySQL 5.5) 2018-05-12 16:49:55.239 INFO 115180 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.032s) 2018-05-12 16:49:55.255 INFO 115180 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `demo_database_1`: 3 2018-05-12 16:49:55.255 INFO 115180 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `demo_database_1` to version 4 - Another user
Output:

Spring boot Flyway Java Based Migration Example – Output
3. Concussion
In this article, We have learned about how we can execute Java code with Flyway migration. Flyway Java Migration Example in spring boot application.
4. References
5. Source Code
spring-boot-Flyway-database-Java-migration-example (47 KB)
2 comments. Leave new
1st of all nice article on fundamentals for java based flyway migration. I have one query that if I want to execute one java based migration on some spring application property, is it possible to make that migration conditional using that property ?
You can try this:
public class V4__Another_user implements: SpringJdbcMigration{
@Override
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
if(value==”your value”{
jdbcTemplate.execute(“INSERT INTO employee\n” +
“(employeeId, employeeName, employeeRole)\n” +
“VALUES (14, ‘Zone’, ‘ADMN’);”);
}
}
}
NOTE: If you want to autowire any field then use @Component as class:
You can refer this article for read properties from application.properties :
https://javadeveloperzone.com/spring-boot/spring-boot-read-application-properties/