Flyway is an open-source database version control tool implemented in Java. It offers various methods such as CLI, Java API, Maven/Gradle Plugin, etc., making it convenient for developers to manage and deploy changes to database table structures and contents in traceable code form. Flyway supports most mainstream relational databases, including MySQL, SQL Server, Oracle Database, PostgreSQL, SQLite, TiDB, MariaDB, etc. Support for MongoDB is still in the preview stage (Mongock is recommended instead). Its counterpart, Liquibase, is more complex to use compared to Flyway, with additional concepts serving as a learning curve.
Following Spring Boot's well-known convention-over-configuration principle, the Spring team provides Flyway Auto Configuration.
The fully qualified class name org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration will perform automatic configuration when the following conditions are detected:
The Flyway class exists (class name: org.flywaydb.core.Flyway)
Bean or properties conditions are met in the FlywayDataSourceCondition class, meaning connection configurations such as DataSource and database URL exist.
The property configuration spring.flyway.enabled=true exists.
For MySQL, besides the required JDBC dependency, you need to include Flyway's own dependencies. Spring Boot has already declared the version number in the POM, so no additional version field is needed here:
There are two ways to use it:
Native SQL
Java API
Flyway will discover version change files or implementation classes based on the default configured folder path classpath:db/migration.
VERSION can be a string version containing dots and underscores, and DESCRIPTION is a simple description text. The separator in the middle is two underscores. Custom prefixes and suffixes can be configured through spring.flyway.
The V{VERSION}__{DESCRIPTION}.sql file needs to be placed in the src/main/resources/db/migration folder, while Java classes need to define the package name as db.migration and inherit the superclass BaseJavaMigration. We need to do is overriding the following method:
According to the official documentation, we can implement database content version changes using Java code in the migrate() method. However, the drawback here is that we cannot perform dependency injection for V{VERSION}__{DESCRIPTION}.java in the Spring environment. We can only attempt to use plain SQL implementations through native JDBC methods, static methods, etc., rather than fully utilizing existing DAO interfaces for business data updates. Therefore, custom configuration of Flyway is required.
Since Flyway's configuration is based on manual configuration like this, it's necessary to exclude the original auto-configuration classes in the Spring Boot startup class to avoid conflicts in auto-configuration loading.
Finally, define a Component Bean to implement BaseJavaMigration:
Now, all migration classes can easily use dependency injection to perform CRUD operations. In practice, Flyway will execute all migrations synchronously after database connection configuration and before HTTP service exposure, ensuring that timing does not affect online services.