Spring Boot - Flyway Database



Flyway is a version control application to evolve your Database schema easily and reliably across all your instances. To learn more about Flyway, you can use the link − www.flywaydb.org

Many software projects use relational databases. This requires the handling of database migrations, also often called schema migrations.

In this chapter, you are going to learn in detail about how to configure Flyway database in your Spring Boot application.

Configuring Flyway Database

First, download the Spring Boot project from Spring Initializer page www.start.spring.io and choose the following dependencies −

  • Spring Web
  • Flyway Migration
  • MySQL Driver
  • Spring Data JPA
Creating Flyway Project

Maven users can add the following dependencies in pom.xml file.

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.flywaydb</groupId>
   <artifactId>flyway-core</artifactId>
</dependency>
<dependency>
   <groupId>org.flywaydb</groupId>
   <artifactId>flyway-mysql</artifactId>
</dependency>
<dependency>
   <groupId>com.mysql</groupId>
   <artifactId>mysql-connector-j</artifactId>
   <scope>runtime</scope>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

Gradle users can add the following dependencies in build.gradle file.

compile('org.flywaydb:flyway-core')
compile('org.flywaydb:flyway-mysql')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-test')
compile('mysql:mysql-connector-j')

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.

spring.application.name = flywayapp  

spring.datasource.url = jdbc:mysql://localhost:3306/details
spring.datasource.username = root
spring.datasource.password = Admin@123

spring.flyway.enabled=true
spring.flyway.validate-on-migrate=true
spring.flyway.baseline-on-migrate = true
spring.flyway.baseline-version = 0

YAML users can add the following properties in application.yml file.

spring:
   application:
      name: flywayapp  
   datasource: 
      url: jdbc:mysql://localhost:3306/details
      password: Admin@123
      username: root
   flyway:
      enabled: true
      validate-on-migrate: true
      baseline-on-migrate: true
      baseline-version: 0

Now, create a SQL file under the src/main/resources/db/migration directory. Name the SQL file as "V1__Initial.sql" considering the naming convention as V<version-number>__<Name>.sql

CREATE TABLE USERS (ID INT AUTO_INCREMENT PRIMARY KEY, USERID VARCHAR(45));
INSERT INTO USERS (ID, USERID) VALUES (1, 'tutorialspoint.com');

The main Spring Boot application class file code is given below −

FlywayappApplication.java

package com.tutorialspoint.flywayapp;

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

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

The complete build configuration file is given below.

Maven pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>3.3.4</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.tutorialspoint</groupId>
   <artifactId>flywayapp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>flywayapp</name>
   <description>Demo project for Spring Boot</description>
   <url/>
   <licenses>
      <license/>
   </licenses>
   <developers>
      <developer/>
   </developers>
   <scm>
      <connection/>
      <developerConnection/>
      <tag/>
      <url/>
   </scm>
   <properties>
      <java.version>21</java.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-jpa</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.flywaydb</groupId>
         <artifactId>flyway-core</artifactId>
      </dependency>
      <dependency>
         <groupId>org.flywaydb</groupId>
         <artifactId>flyway-mysql</artifactId>
      </dependency>
      <dependency>
         <groupId>com.mysql</groupId>
         <artifactId>mysql-connector-j</artifactId>
         <scope>runtime</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>

Gradle build.gradle

buildscript {
   ext {
      springBootVersion = '3.3.4'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.tutorialspoint'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 21

repositories {
   mavenCentral()
}
dependencies {
   compile('org.flywaydb:flyway-core')
   compile('org.flywaydb:flyway-mysql')
   compile('org.springframework.boot:spring-boot-starter-data-jpa')
   compile('org.springframework.boot:spring-boot-starter-web')
   compile('mysql:mysql-connector-j')
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

Compilation and Execution

You can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands given below −

For Maven, you can use the command shown here −

mvn clean install

After "BUILD SUCCESS", you can find the JAR file under the target directory.

For Gradle, you can use the command shown here −

gradle clean build

After "BUILD SUCCESSFUL", you can find the JAR file under the build/libs directory.

Now, run the JAR file by using the following command −

 java jar <JARFILE> 

Now, Tomcat started on the port 8080 and in the console window you can see the flyway database logs as shown here.


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

[32m :: Spring Boot :: [39m              [2m (v3.3.4)[0;39m

[2m2024-09-20T13:17:03.838+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication      [0;39m [2m:[0;39m Starting FlywayappApplication using Java 21.0.3 with PID 9760 (E:\Dev\flywayapp\target\classes started by Tutorialspoint in E:\Dev\flywayapp)
[2m2024-09-20T13:17:03.841+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication      [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[2m2024-09-20T13:17:04.545+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[2m2024-09-20T13:17:04.568+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 14 ms. Found 0 JPA repository interfaces.
[2m2024-09-20T13:17:05.144+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port 8080 (http)
[2m2024-09-20T13:17:05.161+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[2m2024-09-20T13:17:05.161+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.apache.catalina.core.StandardEngine   [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/10.1.30]
[2m2024-09-20T13:17:05.235+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[2m2024-09-20T13:17:05.235+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 1339 ms
[2m2024-09-20T13:17:05.481+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[2m2024-09-20T13:17:05.892+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.pool.HikariPool       [0;39m [2m:[0;39m HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@6c5ca0b6
[2m2024-09-20T13:17:05.894+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[2m2024-09-20T13:17:05.952+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36morg.flywaydb.core.FlywayExecutor        [0;39m [2m:[0;39m Database: jdbc:mysql://localhost:3306/details (MySQL 8.0)
[2m2024-09-20T13:17:05.993+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.c.i.s.JdbcTableSchemaHistory        [0;39m [2m:[0;39m Schema history table `details`.`flyway_schema_history` does not exist yet
[2m2024-09-20T13:17:05.996+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbValidate    [0;39m [2m:[0;39m Successfully validated 1 migration (execution time 00:00.018s)
[2m2024-09-20T13:17:06.026+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.c.i.s.JdbcTableSchemaHistory        [0;39m [2m:[0;39m Creating Schema History table `details`.`flyway_schema_history` with baseline ...
[2m2024-09-20T13:17:06.282+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbBaseline    [0;39m [2m:[0;39m Successfully baselined schema with version: 0
[2m2024-09-20T13:17:06.321+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate     [0;39m [2m:[0;39m Current version of schema `details`: 0
[2m2024-09-20T13:17:06.329+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate     [0;39m [2m:[0;39m Migrating schema `details` to version "1 - Initial"
[2m2024-09-20T13:17:06.455+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.f.core.internal.command.DbMigrate     [0;39m [2m:[0;39m Successfully applied 1 migration to schema `details`, now at version v1 (execution time 00:00.073s)
[2m2024-09-20T13:17:06.586+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[2m2024-09-20T13:17:06.655+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 6.5.3.Final
[2m2024-09-20T13:17:06.690+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.h.c.internal.RegionFactoryInitiator   [0;39m [2m:[0;39m HHH000026: Second-level cache disabled
[2m2024-09-20T13:17:07.025+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.s.o.j.p.SpringPersistenceUnitInfo     [0;39m [2m:[0;39m No LoadTimeWeaver setup: ignoring JPA class transformer
[2m2024-09-20T13:17:07.446+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m:[0;39m HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
[2m2024-09-20T13:17:07.452+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[2m2024-09-20T13:17:07.514+05:30[0;39m [33m WARN[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
[2m2024-09-20T13:17:07.978+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8080 (http) with context path '/'
[2m2024-09-20T13:17:07.988+05:30[0;39m [32m INFO[0;39m [35m9760[0;39m [2m---[0;39m [2m[flywayapp  ] [           main][0;39m [2m[0;39m[36mc.t.flywayapp.FlywayappApplication      [0;39m [2m:[0;39m Started FlywayappApplication in 4.595 seconds (process running for 5.424)

You can now go to the database and check the Users table created in details schema with the details as per the script provided in V1__Initial.sql

Users table data

You can also track the migrations in flyway metadata table flyway_schema_history as shown below:

flyway_schema_history table
Advertisements