Spring boot flyway Example

Spring Boot example with Flyway for DB migration. DB migration can be done using flyway db under the hood by providing some configuration details and migration script.

Technology Used :

Dependency

<?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>com.jbt</groupId>
    <artifactId>spring.boot.flyway</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.17</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

Flyway Configuration

flyway:
  enabled: true
  locations: classpath:migration
  schemas: [jbt_db]

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/
    password: Password
    username: root

Migration Script

insert into PERSON (id,first_name, last_name) values (2,'Vishal', 'Tubro');
create table PERSON (
    FIRST_NAME varchar(100) not null,
    LAST_NAME varchar(100) not null,
    id INT PRIMARY KEY
);

insert into PERSON (id,first_name, last_name) values (1,'MBA', 'TCs');

Project Structure

Spring Boot Application

package com.jbt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class,args);
    }
}

When you execute Spring boot application. It will search for migration script in location defined in application.yml(locations: classpath:migration). If it finds any, it will execute all script which are not yet executed. To identify scripts status flyway uses table “SCHEMA_VERSION” in current schema. If it is not there and schema is blank, flyway will create one.  If you run Spring boot Application without schema or blank schema, flyway will execute migration script and you will get to see below details in console.

 o.f.core.internal.util.VersionPrinter    : Flyway 4.2.0 by Boxfuse
 o.f.c.i.dbsupport.DbSupportFactory       : Database: jdbc:mysql://localhost:3306/ (MySQL 5.7)
 o.f.core.internal.command.DbSchemas      : Creating schema `jbt_db` ...
 o.f.c.i.metadatatable.MetaDataTableImpl  : Creating Metadata table: `jbt_db`.`schema_version`
 o.f.core.internal.command.DbMigrate      : Current version of schema `jbt_db`: null
 o.f.core.internal.command.DbMigrate      : Migrating schema `jbt_db` to version 1 - Create person table
 o.f.core.internal.command.DbMigrate      : Migrating schema `jbt_db` to version 1.1 - Insert Another Record
 o.f.core.internal.command.DbMigrate      : Successfully applied 2 migrations to schema `jbt_db` (execution time 00:00.600s).
 j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
 o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [

If you check mysql DB. You will find new schema(jbt_db) and new tables (person & schema_version).

 SCHEMA_VERSION table will contain details of all the migration done till now.

Source Code

Download complete Spring Boot Flyway Example Source Code

2 Comments

  1. what if I have multiple schemas? like for example,

    flyway:
    enabled: true
    locations: classpath:migration
    schemas: [jbt_db]

    where can I add the second schema?

  2. what if I have multiple schemas? like for example,

    flyway:
    enabled: true
    locations: classpath:migration
    schemas: [jbt_db]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.