Elegantly manage database migrations with Flyway

Database Migration Pain Points

In the process of developing an application, the database schema often needs to be modified - adding tables, fields, indexes, etc. This will lead to different database schemas in development, testing and production environments, which will bring difficulties to development and deployment.
To solve this problem, we need a mechanism to manage database changes, which is database migration.
The goals are:

  • Track database changes
  • Forward and rollback migrations are possible
  • Keep databases consistent across development, test, and production environments

Introduction to Flyway

Flyway is an open source database migration tool. It can manage SQL scripts to evolve the database schema and keep the data in development, test and production environments in sync.
The main features of Flyway are:

  • SQL scripts can be bound to the database (by file name), so that the current state of the database can be accurately detected
  • Supports forward migration and rollback migration
  • Has "semantic versioning" - the order of migration scripts is determined by filename
  • Both SQL and Java migration scripts are supported
  • It can be integrated in the CI/CD pipeline to ensure the consistency of development, testing and production environments
    Use Flyway for database migration
    Here is a simple example to illustrate how to use Flyway for database migration:
  1. Add Flyway dependency. Take Maven as an example:
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>6.5.3</version> 
</dependency>
  1. Create migration scripts. The file name follows the format of V{version number}__{description}.sql. For example:
V1__Create_user_table.sql
V2__Add_email_column.sql
  1. Configure Flyway in the program
Flyway flyway = Flyway.configure() 
    .dataSource("jdbc:mysql://localhost/mydatabase", "username", "password")
    .locations("classpath:db/migration")    // 迁移脚本目录
    .encoding("UTF-8")                      // 编码
    .schemas("my_schema")                  // schema名字
    .table("schema_version")               // 元数据表名
    .baselineOnMigrate(true)               // 第一次运行时创建baseline
    .validateOnMigrate(false)              // 迁移时不校验SQL
    .load();
flyway.migrate();                        // 执行迁移
  1. Execute migration
    When calling flyway.migrate(), Flyway will execute all unexecuted migration scripts to update the database to the latest version.
    When running for the first time, a schema_version table will be created to store the metadata information of the migration.
  2. Other operations
    Flyway also provides many other APIs:
  • flyway.baseline() : establishing a new baseline for a database derby
  • flyway.repair() : Repair broken migrations
  • flyway.clean() : clean up the database before migration
  • flyway.info() : Print migration information
  • flyway.undo() : Perform a rollback migration

How to use it in springboot

Using Flyway with Spring Boot is also very simple. We only need to add the starter dependency of flyway and configure Flyway in the application.properties file.

  1. add dependencies
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-spring-boot-starter</artifactId>
    <version>6.5.3</version>
</dependency>
  1. Configure Flyway
    to add in application.properties:
spring.flyway.url=jdbc:mysql://localhost/mydatabase
spring.flyway.user=username
spring.flyway.password=password 
spring.flyway.locations=classpath:db/migration    # 迁移脚本目录
  1. Write the migration script
    The name of the migration script is the same as above, and it is placed in the classpath:db/migration directory.
  2. Start the application
    When the Spring Boot application starts, Flyway will automatically execute all unexecuted migration scripts to update the database to the latest version.
    At the same time, Flyway will also perform initialization or repair operations during the application startup phase. We can specify in the configuration file:
spring.flyway.baseline-on-migrate=true   # 第一次启动时创建baseline 
spring.flyway.repair=true                # 启动时检查并修复损坏的迁移 
spring.flyway.clean-on-validation-error=true # 校验 SQL 错误时,自动清理并重试

In this way, the database of the Spring Boot application can be automatically initialized to the latest version when the application starts for the first time, and then kept in sync. This greatly simplifies the difficulty of maintaining a consistent database schema between different environments.
During the development process, database schema changes have also become easier and smoother. We only need to add a new migration script, restart the Spring Boot application, and Flyway will automatically execute the migration to update the database to the latest version.

Guess you like

Origin blog.csdn.net/qq_26824159/article/details/130968278