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:
<!-- flyway --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>4.2.0</version> <dependency>
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:
<bean id="MigrationSqlite" class="com.xxxxxx.flyway.MigrationSqlite" init-method="migrate"> <property name="dataSource" ref="dataSource"></property> </bean>
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).
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" depends-on="MigrationSqlite"> <property name="dataSource" ref="dataSource" /> </bean>
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!

Java's Nashorn engine enables JavaScript scripting within Java apps. Key steps include setting up Nashorn, managing scripts, and optimizing performance. Main issues involve security, memory management, and future compatibility due to Nashorn's deprec

Java enums represent fixed sets of values, offering type safety, readability, and additional functionality through custom methods and constructors. They enhance code organization and can be used in switch statements for efficient value handling.

The article discusses the Java Virtual Machine (JVM), detailing its role in running Java programs across different platforms. It explains the JVM's internal processes, key components, memory management, garbage collection, and performance optimizatio

The article discusses various Java garbage collection algorithms (Serial, Parallel, CMS, G1, ZGC), their performance impacts, and suitability for applications with large heaps.

Java's try-with-resources simplifies resource management by automatically closing resources like file streams or database connections, improving code readability and maintainability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
