Spring Boot Flyway achieve integrated database version control?

Spring Boot Flyway achieve integrated database version control?

Today to introduce a more user-friendly database version control tools Flyway. In the process of building the micro through Spring Boot services, the services while the micro-split will be split according to the general boundaries of the system function of its dependency database. In this case, the micro version of the database management service for R & D project management, it would be a more difficult problem.

In normal code management process, from product development process research and development, the general experience functional development, R & D testing, integration testing, pre-release testing many areas, such as on-line. And for the same product features that it may also involve changes to multiple micro service code and database structure.

These changes require that we publish every environment in more than a process, we need to rely on pre-set well in advance of the database structure changes. Suppose that we developed need to publish to the test environment, we need to change the script we will advance the pre-release testing environment, also need to advance in the pre-release environment in a test environment to execute the script execution, issued after the completion of the test required to test environment. In the past, this process relies on manual execution, if you want to keep all versions of the database environment consistency, largely need to rely on people, the environment is less good, but if the environment more, then, over time it is easy to we do not maintain state appeared. Only one day when an error in the test environment, and will suddenly find that some services database change script has not been executed, thereby to fill a vacancy.

So there is not a more intelligent way, when the micro service starts, it can detect changes in database versions, which can automatically execute database scripts to change, in order to ensure that the database version in addition to most of the production environment You can automatically do the same?

The answer is a number, there are some solutions available in the market today to introduce the use of relatively little broad Flyway.

Flyway Overview

Flyway version control is a database management tool, similar to Git version control functionality of the code. Flyway to support the market almost all popular databases such as Mysql, Oracle, PostgreSQL and so on. By Flyway management, we can easily across multiple schema and related business data management database environment change information. For example, the development of a new feature to create a new table, simply scripts in the specified directory of the project in accordance with the specifications of naming format, then the application can automatically detect the current version of the database environment through Flyway, thereby automatically help us to complete the appropriate environmental structure synchronization, eliminating the need to manually perform as before.

In addition to changing the structure of the database schema, data changes can also be synchronized in this way, for example, we have added a dictionary data dictionary table, similarly can be synchronized to manage data in this way changing the record.

Spring Boot integration Flyway

Use Flyway project in Spring Boot is very convenient and simple. First, we need to introduce plug-in dependencies and dependencies Flyway, as follows:

<!--引入flyway的依赖-->
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-core</artifactId>
    <version>5.0.3</version>
</dependency>
 <!--flyway插件依赖-->
 <plugin>
     <groupId>org.flywaydb</groupId>
     <artifactId>flyway-maven-plugin</artifactId>
     <version>5.0.3</version>
 </plugin>

So far, we have completed the integration of the Flyway Spring Boot project is not very easy to use them! After completing Flyway integration, our database script needs to be how to manage Flyway automatically recognized and properly enforced?

We need to project in resourcesthe establishment of the db/migrationfolder, and by V1.0__init.sql(is __instead _) like this naming naming database scripts every time we need to change. For example, we create a new project, then we can put the database initialization script into this project here, such as: V1.0__init_database.sql.

In this way, at this time if you connect to a new database, start Spring Boot project Flyway automatically to scan db/migrationscripts directory is not performed, so help you complete database synchronization script. With the development features, consider a new database changes need to be performed, then we need to build a new script file, such as: V1.1__add_dictdata.sql this way, next time you start the project when the script is also automatically executed. About naming convention script data in addition to the version number of this V1.1, the back part it can play a meaningful, relatively standardized according to the type of name can be changed.

Having said that, is not it a bit puzzled, Flyway to do in the end is how can we manage the database version of it? In fact, if we first integrated Flyway, to start the project in the corresponding Flyway creates a database named " flyway_schema_history table", this table will record the implementation of all versions of the script, such as:

Spring Boot Flyway achieve integrated database version control?

That, in fact Flyway control over database script version is entirely dependent on the maintenance of such an information table. Assume a script has been successfully performed, if we perform artificial delete records of this table, what will happen? The answer is, Flyway will perform again, and as executed, if there is to rebuild the table SQL script, then it may result in data loss, so use Flyway for the maintenance of this table is critical, make sure to remember!

In addition, in most cases, before we use Flyway, the database may have already done some script, if it again this time to manage, and you want to achieve the effect of the script that was performed before are no longer performed automatically, then, at this time we may need to manually in flyway_schema_history inserted into the script performs a version recorded corresponds to the table, thereby temporarily bypassing the lower Flyway.

postscript

Flyway is a more automated database version control tool, with good will to help us improve the development of R & D efficiency, on the other hand, even the best tools that people how to use, if not a full set of norms, too automated tools It may bring disaster, such as the reconstruction of important data from being lost cause! Therefore, in most cases Flyway for test and development environments version of the database maintenance is very convenient, as for the production of thing, it is recommended that agreement through a set of procedures performed manually manage safer!

Guess you like

Origin blog.51cto.com/14570694/2451985