Databases 11 min read

Using Flyway for Database Version Management and Migration in Spring Boot

This article explains what Flyway is, its migration types and common commands, shows how to integrate Flyway with Spring Boot via Maven dependencies and YAML configuration, provides sample versioned and repeatable migration scripts, and demonstrates the migration process with log output.

Code Ape Tech Column
Code Ape Tech Column
Code Ape Tech Column
Using Flyway for Database Version Management and Migration in Spring Boot

What is Flyway? What problems does it solve?

Flyway is a database migration tool that executes SQL or Java scripts during application deployment, managing versioned migrations automatically.

PS: Its mechanism is essentially the same as Liquibase.

Flyway migrations

Flyway defines three migration types:

Versioned Migrations : executed once per version, used for creating, altering, or dropping tables and data.

Undo Migrations : reverse operations of versioned migrations (available in the paid version).

Repeatable Migrations : can run multiple times, used for views, stored procedures, functions, etc.

The naming convention is V|U|R__Version__Description.sql (e.g., V1.0__Init_DB.sql ).

Common Flyway commands

Migrate : core workflow that scans for pending migrations and applies them.

Clean : removes all objects in the configured schema (useful for dev/test, not for production).

Info : prints detailed status of all migrations.

Validate : checks that applied migrations match the checksum of the scripts.

Undo : rolls back the last migration (paid feature).

Baseline : creates a metadata table for an existing schema and marks a baseline version.

Repair : repairs the metadata table when it becomes inconsistent.

Simple example

Integrating Flyway with a Spring Boot project requires the following Maven dependencies:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-mysql</artifactId>
    <version>8.5.7</version>
</dependency>

The Spring Boot auto‑configuration already includes Flyway support; you can customize it in application.yml :

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test_db_flyway?useSSL=false&autoReconnect=true&characterEncoding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: your_password
  flyway:
    enabled: true
    encoding: UTF-8
    locations: classpath:db/migration
    validate-on-migrate: true

Place migration scripts under src/main/resources/db/migration . Example versioned migrations:

-- V1.0__Init_DB.sql
DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(45) NOT NULL,
  `password` varchar(45) NOT NULL,
  `email` varchar(45) DEFAULT NULL,
  `phone_number` int(11) DEFAULT NULL,
  `description` varchar(255) DEFAULT NULL,
  `create_time` datetime DEFAULT NULL,
  `update_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
-- V1.1__Init_Data.sql
LOCK TABLES `tb_user` WRITE;
INSERT INTO `tb_user` VALUES (1,'pdai','dfasdf','[email protected]',1212121213,'afsdfsaf','2021-09-08 17:09:15','2021-09-08 17:09:15');
UNLOCK TABLES;

Running the Spring Boot application produces logs that show Flyway validating and applying the migrations, creating the flyway_schema_history table, and finally reporting successful migration to version 1.1.

Previous highlights (promotional section omitted)

SQLSpring Bootdatabase migrationVersion ControlFlyway
Code Ape Tech Column
Written by

Code Ape Tech Column

Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.