Spring Boot Resources

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=guest
spring.datasource.password=guest123

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: guest123
      username: guest
   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.5.6</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.5.6'
   }
   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')
}

Output

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.


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

 :: Spring Boot ::                (v3.5.6)

2025-09-29T20:21:45.006+05:30  INFO 22704 --- [flywayapp  ] [           main] c.e.flaywayapp.FlaywayappApplication     : Starting FlaywayappApplication using Java 21.0.6 with PID 22704 (D:\Projects\flaywayapp\target\classes started by mahes in D:\Projects\flaywayapp)
2025-09-29T20:21:45.012+05:30  INFO 22704 --- [flywayapp  ] [           main] c.e.flaywayapp.FlaywayappApplication     : No active profile set, falling back to 1 default profile: "default"
2025-09-29T20:21:45.838+05:30  INFO 22704 --- [flywayapp  ] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2025-09-29T20:21:45.883+05:30  INFO 22704 --- [flywayapp  ] [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 27 ms. Found 0 JPA repository interfaces.
2025-09-29T20:21:46.376+05:30  INFO 22704 --- [flywayapp  ] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-09-29T20:21:46.404+05:30  INFO 22704 --- [flywayapp  ] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-09-29T20:21:46.405+05:30  INFO 22704 --- [flywayapp  ] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.46]
2025-09-29T20:21:46.480+05:30  INFO 22704 --- [flywayapp  ] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-09-29T20:21:46.481+05:30  INFO 22704 --- [flywayapp  ] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1395 ms
2025-09-29T20:21:46.842+05:30  INFO 22704 --- [flywayapp  ] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2025-09-29T20:21:47.251+05:30  INFO 22704 --- [flywayapp  ] [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Added connection com.mysql.cj.jdbc.ConnectionImpl@47df5041
2025-09-29T20:21:47.253+05:30  INFO 22704 --- [flywayapp  ] [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2025-09-29T20:21:47.399+05:30  INFO 22704 --- [flywayapp  ] [           main] org.flywaydb.core.FlywayExecutor         : Database: jdbc:mysql://localhost:3306/details (MySQL 8.0)
2025-09-29T20:21:47.502+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Schema history table `details`.`flyway_schema_history` does not exist yet
2025-09-29T20:21:47.506+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.core.internal.command.DbValidate     : Successfully validated 1 migration (execution time 00:00.037s)
2025-09-29T20:21:47.591+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.c.i.s.JdbcTableSchemaHistory         : Creating Schema History table `details`.`flyway_schema_history` with baseline ...
2025-09-29T20:21:47.789+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.core.internal.command.DbBaseline     : Successfully baselined schema with version: 0
2025-09-29T20:21:47.825+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.core.internal.command.DbMigrate      : Current version of schema `details`: 0
2025-09-29T20:21:47.844+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.core.internal.command.DbMigrate      : Migrating schema `details` to version "1 - Initial"
2025-09-29T20:21:47.913+05:30  INFO 22704 --- [flywayapp  ] [           main] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema `details`, now at version v1 (execution time 00:00.034s)
2025-09-29T20:21:48.043+05:30  INFO 22704 --- [flywayapp  ] [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2025-09-29T20:21:48.106+05:30  INFO 22704 --- [flywayapp  ] [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 6.6.29.Final
2025-09-29T20:21:48.168+05:30  INFO 22704 --- [flywayapp  ] [           main] o.h.c.internal.RegionFactoryInitiator    : HHH000026: Second-level cache disabled
2025-09-29T20:21:48.541+05:30  INFO 22704 --- [flywayapp  ] [           main] o.s.o.j.p.SpringPersistenceUnitInfo      : No LoadTimeWeaver setup: ignoring JPA class transformer
2025-09-29T20:21:48.657+05:30  INFO 22704 --- [flywayapp  ] [           main] org.hibernate.orm.connections.pooling    : HHH10001005: Database info:
	Database JDBC URL [Connecting through datasource 'HikariDataSource (HikariPool-1)']
	Database driver: undefined/unknown
	Database version: 8.0.43
	Autocommit mode: undefined/unknown
	Isolation level: undefined/unknown
	Minimum pool size: undefined/unknown
	Maximum pool size: undefined/unknown
2025-09-29T20:21:48.893+05:30  INFO 22704 --- [flywayapp  ] [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2025-09-29T20:21:48.897+05:30  INFO 22704 --- [flywayapp  ] [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2025-09-29T20:21:48.949+05:30  WARN 22704 --- [flywayapp  ] [           main] JpaBaseConfiguration$JpaWebConfiguration : 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
2025-09-29T20:21:49.444+05:30  INFO 22704 --- [flywayapp  ] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-09-29T20:21:49.456+05:30  INFO 22704 --- [flywayapp  ] [           main] c.e.flaywayapp.FlaywayappApplication     : Started FlaywayappApplication in 4.961 seconds (process running for 5.337)

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

mysql> select * from users;
+----+--------------------+
| ID | USERID             |
+----+--------------------+
|  1 | tutorialspoint.com |
+----+--------------------+
1 row in set (0.00 sec)

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

mysql> select * from flyway_schema_history;
+----------------+---------+-----------------------+----------+-----------------------+-------------+--------------+---------------------+----------------+---------+
| installed_rank | version | description           | type     | script                | checksum    | installed_by | installed_on        | execution_time | success |
+----------------+---------+-----------------------+----------+-----------------------+-------------+--------------+---------------------+----------------+---------+
|              1 | 0       | << Flyway Baseline >> | BASELINE | << Flyway Baseline >> |        NULL | guest        | 2025-09-29 20:21:47 |              0 |       1 |
|              2 | 1       | Initial               | SQL      | V1__Initial.sql       | -1994336522 | guest        | 2025-09-29 20:21:47 |             34 |       1 |
+----------------+---------+-----------------------+----------+-----------------------+-------------+--------------+---------------------+----------------+---------+
2 rows in set (0.00 sec)
Advertisements