Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Flyway

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to deploy Flyway". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to deploy Flyway.

1. What is Flyway?

When we are doing development in the company, due to changes in project requirements, or early design defects, leading to the need to modify the database in the later stage, this should be a relatively common thing, if the project has not been online, you may delete the table and recreate it, but if the project is already online, it can not be so simple and rough, we need to upgrade based on the existing data tables through SQL scripts.

At present, there are two main tools for managing the version of the database in Java:

FlywayLiquibase

The two tools have their own advantages, but the core function is the version management of the database, here we mainly look at Flyway. Just as we use Git to manage code versions, Flyway can be used to manage database versions.

All right, let's take a look at how to simplify micro-personnel deployment with Flyway, and then talk about a general principle of Flyway.

two。 Embed into micro personnel

If you are using Flyway in a completely new project, you will have the option of Flyway when creating a new Spring Boot project, as shown below:

After the project is created successfully, there will also be an additional db/migration directory under the resources directory, which is used to store database scripts, as follows:

"attention."

This directory will be available if the Flyway dependency is selected when the project is created. Now that I'm going to add Flyway to the micro-personnel that I've already done, I need to create this directory manually.

First, add flyway dependencies to WeChat:

Org.flywaydb

Flyway-core

Then under the resources directory under the vhr-web module, manually create the db/migration directory, and then create the database script under that directory. The database script is named as follows:

V__.sql

First is the capital letter V, then the version number, if there is a small version can be separated by an underscore, for example, 2 underscore 1, the version number is followed by two underscores, then the script name, and the file suffix is .sql.

For example, I created my first database script here, named V1__vhr.sql, and the content of the script is the micro-personnel database script, which you can get here at https://github.com/lenve/vhr.

After that, you don't need to add additional configuration. You just need to create an empty vhr database in the local MySQL, and then start the micro-personnel project directly. After the project starts successfully, we check the startup log:

From this startup log, we can see the execution information of Flyway and the execution of database scripts, and it is also said that Flyway also creates a flyway_schema_history table that is used to record the update history of the database.

At this point, when we open the local database, we find that we have all the tables that should be in the vhr library. At the same time, the flyway_schema_history table is also found as follows:

With this record, the next time you start the vhr project, the V1__vhr.sql script file will not be executed, because the system knows that the script has been executed, and if you want the V1__vhr.sql script to be executed again, you need to manually delete the corresponding records in the flyway_schema_history table, then the script will be executed when the project starts.

3. Execution details when we define the script, in addition to the script at the beginning of the word V, there is also a script at the beginning of the word R, in which the script at the beginning of the word V is only executed once, while the script at the beginning of the word R is executed as long as the content of the script changes. After using Flyway, if you want to upgrade the database version again, instead of the previous database script, create a new database script directly, and the project detects a new and newer version of the script at startup, and it will be executed automatically, so that it will be much more convenient when working with other colleagues. Because normally, we all pull the code from Git instead of database scripts, so that if someone updates the database, other colleagues may not be able to receive the latest notification, the use of Flyway can effectively avoid this problem. All scripts, once executed, will be recorded in the flyway_schema_history table. If you accidentally make a mistake, you can manually delete the record from the flyway_schema_history table, and then modify the SQL script before restarting (not recommended in the production environment). 4. Other configuration

In Spring Boot, there are many configurations about Flyway. These configurations are all configured in application.properties. Here are a few commonly used configurations:

Spring.flyway.enabled: whether to enable flyway. Default is the directory where spring.flyway.encoding:flyway character encoding spring.flyway.locations:sql scripts are enabled. Default is classpath:db/migration. If there are more than one, separate spring.flyway.clean-disabled: this attribute is critical. It indicates whether to clear the tables under the existing library. If the script executed is V1__xxx.sql, the tables under the existing library will be cleared first, and then the script will be executed. This is quite convenient in the development environment, but it is fatal in the production environment, and it is cleared by default, and the production environment must be set to true. Spring.flyway.table: the name of the configuration database information table. The default is flyway_schema_history. Thank you for reading, the above is the content of "how to deploy Flyway". After the study of this article, I believe you have a deeper understanding of how to deploy Flyway, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report