Use flyway database version management in the SpringBoot

This article outlines

  • What is flyway
  • What can help us solve the problem
  • Use flyway under springboot environment
  • flyway works

First, what is the flyway

Flyway is an open source version of the database management tool, and urged "greater than the configured agreement", simple, focused and powerful. SQL can be used for data synchronization, or based on the syntax of a specific database (e.g., PL / SQL, T-SQL, etc.) or Java code (for advanced data conversion or processing LOB) write mode. And the database supports a very wide:

Second, to help us solve any problems

Well, we first explain what is the version of the database management?

  • We all know that git is to help software project code version management, to facilitate collaborative development programmer
  • So FlyWay is the database version management tool, the goal is to ensure that multi-state environment database consistency programmers to develop collaborative

Here is a simple example:

  • Developers often use the same database or self-built library development, database development libraries often called.
  • Testers in order to protect the validity of test data, usually a self-built test libraries, this database is called the test library.
  • Sales staff in order to ensure the effect of the presentation of data, usually require a separate database, which is called the presentation library.
  • Official production library on line, supplying users, this database is called the production library.

So the question becomes: How do we ensure a consistent state of the database schema? Some developers have changed the development of a library, a new field, how they can effectively synchronized to the test library, to demonstrate how to effectively synchronize the library and the library after production testing through? In the absence Flyway, this action is usually made on-line programmers themselves to execute SQL to complete, or more formal version of the company has specialized management personnel to operate. This is often there are several problems:

  • Increase in the cost of communication in-house development team, a member of such a modification of a database field, other people may not know.
  • Increase in the cost of communication between the development team and the test team and other teams
  • Unable to complete automation of continuous integration, continuous integration process code can be automated code package, deployed by git, maven, docker, k8s and other tools. But the change of state of the database has not been effectively changed automatically, automated continuous integration process can not be achieved.

This is the purpose of our study Flyway: Flyway can automatically help us to effectively state between each publication database synchronization, whether you are added or deleted a field, or the addition of a new table, he can automation following the release of the project also released.

Third, using the flyway under springboot environment

First, we add Flyway rely project is SpringBoot


<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>6.0.7</version>
</dependency>

Then we want to ensure there are spring.datasource database data source configuration. And add the following configuration in the application.properties


spring:
  flyway:
    enabled: true    #启用flyway
    encoding: utf-8   #字符编码
    locations: ["classpath:db/migration"]  #版本控制文件存放目录

Then in resources / db / migration directory, add the script the following format:


V1.1__create_table.sql
V1.2__update_person_table.sql
V1.3__create_new_table.sql
V1.4__add_person_comment.sql
V2.1__modify_person_data.sql

Later followed by a capital V version number of the database scripts (incremental form, can not be repeated), then two underscores after the contents of the script is described. Such as: V1.1__create_table.sql script is used to create a database table structure, content DDL create table and the like. The second script is a person in the database tables have been updated. In short script inside the contents of the database that you want to operate on behalf of spring.datasource carried out, the table can be created, deleted, modified, or may be modified to create deletion of data.

Of course, I personally do not recommend the DML-SQL script is written, that is, do not write insert, update, delete this script. So as not to pay attention to the case, lead to misuse production data. If your company does not have strict vetting procedures in the management of the production line, in a production environment, they simply do not use the flyway, is on the one hand facilitate the development, production safety is more important!

Finally, start SpringBoot project, not carried out within the scope of the target database SQL script is executed. And script execution information stored in the database table data schema_version_history inside.

Four, FlyWay works

  • First, the project will start flyway to db / migration following scan file, get the file name, version number and parse
  • Then go inside to find schema_version_history table corresponding version execution information, if your file is larger than the version number of the database record version, the script is executed. Otherwise ignored.

    Look forward to your attention

  • Blogger recently wrote a book: "hand touch hand to teach you to learn SpringBoot series chapter 97 section -16"
  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

Guess you like

Origin www.cnblogs.com/zimug/p/11796288.html