Over a million developers have joined DZone.

Database Migration With Flyway

DZone's Guide to

Database Migration With Flyway

Flyway integrates wonderfully with Maven, but one of its biggest assets is the ability to run both SQL migration scripts and Java migration scripts.

· Database Zone ·
Free Resource

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

Flyway is an extremely convenient database migraton tool. First and foremost it integrates wonderfully with Maven. But one of its biggest assets is the ability to run both SQL migration scripts and Java migration scripts.

Let us start with a simple Maven project.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

        <database.url>{your jdbc url}</database.url>
        <database.user>{your database user}</database.user>
        <databese.password>{your database password}</databese.password>



When you issue through maven

mvn flyway:migrate

Then flyway will will lookup on the db/migration folder of your target to find any migration files. This folder can be changed by altering the content of the locations inside the configuration of the flyway plugin.

For my first migration file I will use sql

The sql file will be located on the db/migration folder which will reside on the maven resources folder.
The name would be V1_1__Create_Persons.sql
Therefore the full path would be src/main/resources/db/migration/V1_1__Create_Persons.sql

LastName varchar(255),
FirstName varchar(255),

The second file would be a java file.

It will be located on the package db.migration folder which will reside on the maven src folder.

The name would be V1_2__Insert_Persons.java

Therefore the full path would be src/main/java/db/migration/V1_2__Insert_Persons.java

package db.migration;

import org.flywaydb.core.api.migration.jdbc.JdbcMigration;

import java.sql.Connection;
import java.sql.Statement;

public class V1_2__Insert_Persons implements JdbcMigration

    public void migrate(Connection connection) throws Exception
        Statement stmt = connection.createStatement();

        stmt.addBatch("INSERT INTO Persons (FirstName,LastName) VALUES ('Emmanouil','Gkatziouras')");

        stmt.addBatch("INSERT INTO Persons (FirstName,LastName) VALUES ('Do not know','this guy')");

        try {
        finally {



Since we provide a java file it is wise to call flyway by

mvn clean compile flyway:migrate

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

sql ,maven ,database migration ,flyway ,database

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}