Using Flyway in Spring Boot with Spring Boot Starter Flyway

When working with Spring Boot applications, the database schema often needs to change along with application changes. Instead of manually applying these changes, you should use database migration tools like Flyway or Liquibase. I’ve already introduced Flyway to implement database migration. To use Flyway in Spring Boot applications, you can use the Spring Boot Starter Flyway! Similar to other Spring Boot Starters, with just this dependency, you can quickly implement database migration for your Spring Boot application. So what is Spring Boot Starter Flyway? What dependencies does it provide? Let’s find out in this article!

Setup project

First, I will create a new Spring Boot project with the Spring Boot Starter Flyway and PostgreSQL JDBC Driver dependency as follows:

Spring Boot Starter Flyway will help us simplify the integration between Flyway and Spring Boot! It can auto-configuration the necessary settings for Flyway and will automatically perform database migration when our Spring Boot application starts.

Main dependencies

After you add the spring-boot-starter-flyway dependency, Maven will load this dependency along with the dependencies that this Starter requires, as follows:

Here are some important dependencies that I can list:

  • spring-boot-flyway
  • spring-boot-jdbc
  • flyway-core

Flyway provides corresponding dependencies to support different database types. For example, in this article, I’m using a PostgreSQL database, so I can list an important dependency for PostgreSQL databases:

  • flyway-database-postgresql

Now let’s look at the role of each dependency!

spring-boot-flyway

spring-boot-flyway is a module that provides Spring Boot’s auto-configuration and integration mechanisms for Flyway. It uses the FlywayAutoConfiguration, FlywayProperties, FlywayMigrationInitializer, and FlywayMigrationStrategy classes to implement these auto-configuration and integration mechanisms.

spring-boot-jdbc

When creating a Flyway object for database migration, we need DataSource information. The spring-boot-jdbc module provides an auto-configuration mechanism for configuring this DataSource information.

flyway-core

The flyway-core module provides core Flyway functionality for managing and executing database migrations.

Some of the module’s main responsibilities include:

  • Searching for migration scripts;
  • Extracting migration version and description information;
  • Checking migration history;
  • Calculating and validating migration checksums;
  • Performing pending migrations;
  • Recording migration results in the flyway_schema_history table;
  • Supporting methods such as migrate(), validate(), info(), and repair().

flyway-database-postgresql

The flyway-database-postgresql module allows the Flyway migration engine in the flyway-core module to work with PostgreSQL databases. It uses the PostgreSQL JDBC driver to interact with those databases.

Project ví dụ

Now, let’s use the example project from the previous tutorial to implement it using Spring Boot Starter Flyway and see how it works.

When you create a new Spring Boot project with Spring Boot Starter Flyway, the db/migration directory will be created with the project by default.

You just need to copy the Versioned Migration script file from the previous example to this directory. Spring Boot also uses the default location for migration script files, pointing them to this db/migration directory.

For the DataSource information, you just need to declare the database information in the application.yaml file as follows:

At this point, you can run this Spring Boot application and check the database; you will see the same results as we did in the previous tutorial.

Summary

With Spring Boot’s auto-configuration mechanism, we only need to define the migration scripts and database information to implement database migration!

Add Comment