1. Problems encountered in database management during development:
Nowadays, development is generally done by teams, so there will be problems with project synchronization. Code synchronization can be managed through SVN tools, but what about database synchronization? Ideally, when developing a new project, you will first clarify the business, design the database tables, and then hand over the database to specialized personnel for maintenance, so there will be no database synchronization problems. But what is the actual situation? The requirements keep changing from the beginning to the end of the project. Many companies do not have dedicated database maintenance personnel. Everyone is operating and modifying the database. It would be fine if the team communicates in a timely manner and everyone updates the code every time. Database, if communication is not timely, haha (you can make up your own mind). . . In this way, the problem of database out of synchronization is highlighted.
2. A brief introduction to Flyway:
1. Concept:
Flyway is independent Database version management tool for database application, management and tracking database changes. In layman's terms, Flyway can manage different people's SQL scripts just like SVN manages the codes of different people, thereby achieving database synchronization.
2. Supported database types:
Oracle, SQL Server, SQL Azure, DB2, DB2 z/OS, MySQL (including Amazon RDS), MariaDB, Google Cloud SQL, PostgreSQL (including Amazon RDS and Heroku), Redshift, Vertica, H2, Hsql, Derby, SQLite, SAP HANA, solidDB, Sybase ASE and Phoenix.
3. Naming convention for sql scripts:
V version number (the numbers in the version number are separated by "." or "_") Double underscore (used to separate the version number and Description) File description suffix name, for example: V2017.9.30__Update.sql.
Note: The version numbers cannot be the same!
4. The default location for Flyway to read sql scripts:
The db/migration directory under the source folder of the project.
5. Commands:
There are 6 basic commands in total: migrate, clean, info, validate, baseline, repair.
3. Advantages of Flyway:
1. Not only supports sql scripts, but also supports Java code to directly operate the database (flyway-core-x.x.x. jar);
2. Has Maven plug-in;
3. Supports command line;
4. Combined with Spring box, it is very convenient to automatically check and Upgrade database functionality.
4. Use of Flyway command line tool:
1. Unzip and download the flyway-commandlin version and unzip it locally. The structure diagram is as follows:
2. Place the sql script in Flyway's default db/migration directory. If placed in another location, you need to modify flyway.locations in the conf/flyway.conf file.
3. Modify flyway.url, flyway.user, flyway.password in the conf/flyway.conf file according to your own situation.
4. Execute the migrate command on the command line.
5. Use in conjunction with Maven projects:
1. Introduce dependency coordinates:
org.flywaydb flyway-core 4.2.0
2. Create in the src/main/resources directory The path to store the sql version file is dataBase/sqlite (you can also write the default path db/migration), and place the sql file below.
3. Add the java class of flyway:
package com.xxxxxx.flyway; import javax.sql.DataSource; import org.flywaydb.core.Flyway; public class MigrationSqlite { private DataSource dataSource; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } public void migrate() { //初始化flyway类 Flyway flyway = new Flyway(); //设置加载数据库的相关配置信息 flyway.setDataSource(dataSource); //设置存放flyway metadata数据的表名,默认"schema_version",可不写 flyway.setTable("SCHMA_VERSION"); //设置flyway扫描sql升级脚本、java升级脚本的目录路径或包路径,默认"db/migration",可不写 flyway.setLocations("dataBase/sqlite"); //设置sql脚本文件的编码,默认"UTF-8",可不写 flyway.setEncoding("UTF-8"); flyway.migrate(); } }
4. Instantiate the java class of step 3 in spring:
From the above bean definition, we can see that we have injected a data source into the flywayMigration bean instance. All operations of Flyway will be performed against this data source; at the same time, we specify Spring through the init-method attribute when instantiating the bean. Afterwards, the bean's migrate method is actively executed, and within this method Flyway updates the database. So far, we have reached the point where Spring instantiates the context when the application starts, and when Spring instantiates the flywayMigration bean, Flyway automatically updates the database.
5. Handle conflicts when Flyway updates the database and code logic operates the database (I haven’t encountered it yet, I found it online, keep it for emergencies):
If Flyway is still there Before updating the database, other logic of the application has begun to use the database for other operations before completing the update operation, which will cause the application to generate many bugs or even not run at all. To solve this problem, we can use Spring's bean dependency principle to make key database operation beans depend on the flywayMigration bean, so that no other database-related operations can be performed until flywayMigration is instantiated (the database update operation is completed).
In this way, the database will be automatically updated every time the project is started, so you don’t have to worry about the database being out of sync.
Recommended tutorial: "Java Tutorial"
The above is the detailed content of Detailed instructions for using Flyway. For more information, please follow other related articles on the PHP Chinese website!