Springboot application flyway



The elective course of introduction to business website development has been completed, and the students’ homework has been submitted for some time, and the score is imminent. This evaluation adopts the method of multiple people marking the paper, which is much fairer than the method of one person marking the homework before the operating system. The work status has changed from the original reviewed and not reviewed to not reviewed, being reviewed and reviewed, The field indicating the review is changed from reviewed to status. Because it needs to be operated directly at the data table level, SQL statements are used. At that time, the first idea was to add the status field in the entity class, then write SQL at the warehouse level, judge that the reviewed attribute is 1 (review completed), the status is assigned to 2 (reviewed), and the others are 0 until the teacher mentioned flyway, I knew there was such a magical tool.


Coincidentally, twenty minutes before the teacher mentioned flyway, I saw the blog about flyway written by senior Huang Tingxiang: flyway database management. Maybe this is fate. At that time, it seemed that I wanted to check some things about SQL. I vaguely remember that the senior wrote it. Although I didn’t find it in the end, I found it by mistake, although I didn’t understand it, But I feel very good.
Springboot application flyway
When you enter flyway’s official website, you will first show a paragraph:

Version control of database.
Robust pattern evolution across all environments.
Relaxed, pleasant and plain SQL.

For flyway, some blogs introduce it as follows:

Flyway is an open source database version management tool, which prefers the way of specification over configuration. Flyway can manage and track database changes independently of applications, support automatic database version upgrade, and has a set of default protocols without complex configuration. Migrations can be written in SQL script or Java code. It supports not only command line and Java API, but also build build tool and spring boot, At the same time, it can safely and reliably upgrade the database in the distributed environment, and also support failure recovery.

The first successful run will generate a data table named “flyway_schema_history”, which will be used for subsequent management
Its working principle is as follows:
Springboot application flyway

Configure application flyway

First, introduce dependency in pom.xml file:

    <!-- flyway -->

This part is not completely written, because the dependencies introduced by different spring versions are different, and I haven’t learned about it before. I really don’t know that each spring version corresponds to a stable dependency package. Later, I read Xi Shuo’s blog about flyway: flyway practice. I learned that this kind of thing should happen. For this phenomenon, the senior explained it as follows:

spring-bootProject dependentspring-boot-dependenciesProject, which manages allspring-bootVersion number of the integrated technology.
Search in the fileflyway, that is, find the relevant configuration<flyway.version>5.0.7</flyway.version>
spring-bootIt’s stipulated. If you usespring-boot 2.0.5.RELEASEVersion, it is recommended to useflyway:5.0.7

Springboot application flyway
Such management is not only convenient, but also I think it is to avoid conflict.
When multiple packages are introduced, version conflicts may occur. I believe,spring-bootThese versions must be tested to ensure no conflict before they are recommended.

Then I found the dependent file and found the corresponding version of flyway:
Springboot application flyway
Then add to the dependency:

<!-- flyway -->

Then configure the flyway field of application.yaml:

    enabled: true
    baseline-on-migrate: true
    baseline-version: 1
    check-location: true
Enabled indicates whether flyware is enabled
Baseline on migrate indicates whether to automatically perform benchmark migration when the target schema is found to be non empty and there is a table without metadata during migration. The default is false

Baseline version means to label the version of the existing schema when starting the benchmark migration. The default value is 1

Check location means to check whether the location of the migration script exists. The default is false

Next, import the SQL file, create a new DB file under the resources file, and create a migration file in the DB file. This is the default retrieval path of flyway. Next, export the database, dump the SQL file, and export:
Springboot application flyway
Then rename the SQL file:
Springboot application flyway

This is for version control and subsequent execution. The later named version number shall not be lower than this version. If other operations need to be performed later, just create a new file and write SQL.
Click Run to view the flyway in the database_ schema_ The history form has a record of successful execution of the corresponding version of SQL file
Springboot application flyway

It is worth noting that after flyway is enabled, the DDL auto attribute of Hibernate should be changed to validate, that is, validation. When the field type defined by the entity is different from or does not exist in the SQL table, the program will be terminated to prevent field conflicts from causing major problems in the program.


After that, if you want to modify the field, you should not only modify the entity field, but also write SQL and migrate the version. A good blog really makes the viewers suddenly understand. After reading xishuo’s blog, they really understand a lot. They also successfully applied flyway to the system. Here, I would like to express my gratitude to xishuo.

Author: Zhang Wenda, mengyunzhi development team, Hebei University of Technology