The most annoying problem in everyone's work is the problem when going online. The online time is usually at night. If there is a problem, and then go to the location to fix it, it will go directly to the next day after work. At the same time, the most encountered is SQL changes , sometimes forget to submit SQL, or submit SQL incorrectly, which will bring serious bugs to the online system. At this time, the leader will talk to you, and it will also affect that part of the function of the partner. , then it is completely inside and outside not human.
Therefore, in order to prevent everyone from entering the pit
I have arranged this tool for you
Flyway is such a tool. By combining Flyway and SpringBoot, the database table structure can be automatically upgraded when the application starts. It is very convenient and recommended to everyone!
Hello everyone, let me introduce myself first. I'm on the code. You can call me Brother Code. I am also the most ordinary student who graduated from an ordinary undergraduate degree. I believe that most programmers or those who want to work in the programmer industry are I am a child of an ordinary family, so I also rely on my own efforts, from graduation to join a traditional company, to changing jobs without fail, and now working in a giant company in the Internet industry, I hope that through my sharing, I can help everyone
I have prepared 16 technical columns for everyone to lead you to learn together
"Billion-level traffic distributed system combat"
"Battery Factory Interview Must-Ask Series"
"Zero Foundation takes you to learn java tutorial column"
"Take you to learn springCloud column"
"Take you to learn SpringCloud source code column"
"Take you to learn distributed system column"
"Take you to learn cloud native column"
"Take you to learn springboot source code"
"Take you to learn netty principles and practical column"
"Take you to learn Elasticsearch column"
"Take you to learn mysql column"
"Take you to learn JVM principle column"
"Take you to learn Redis principle column"
content
Introduction to Flyway
Flyway is a database migration tool that makes database migration easier. It can version control the database like Git, and supports command-line tools, Maven plug-ins, third-party tools (such as SpringBoot) and other usage methods.
Flyway has the following features:
- Simple: Simple to use and learn, database migration is achieved through different versions of SQL scripts.
- Professional: Focus on the database migration function, you don't need to worry about any problems.
- Powerful function: supports multiple databases, has a large number of third-party tools, and supports CI/DI.
Related concepts
working principle
When using Flyway, we need to write SQL scripts for database migration. For example V1__Initial_Setup.sql
, three kinds of tables are initialized in , V2__First_Changes.sql
and two new tables are added in . Flyway creates flyway_schema_history
tables to store the execution of these SQL scripts, thus versioning the database. When we use Flyway for database migration, Flyway will flyway_schema_history
decide which SQL scripts need to be executed according to the records in the table to realize database migration.
script naming convention
When creating Flyway's SQL scripts, some naming conventions need to be followed. These naming conventions determine the order and method of Flyway's execution of scripts. You can refer to the following diagram first.
In order to be executed correctly by Flyway, the SQL migration script needs to follow the following specifications:
- Prefix (prefix):
V
indicates database migration with version number,U
indicates rollback of some database versions,R
indicates repeatable database migration; - Version (version number): Flyway will execute the database migration script in the order of the version number;
- Separator (separator): use double underscore separator when naming;
- Description: a specific operation description used to describe the migration script;
- Suffix (suffix): Indicates a
.sql
file.
Related commands
- migrate: The database migration command will directly upgrade the database table to the latest version according to the set SQL script.
- clean: delete all tables in the database, do not use it in the production environment.
- info: Prints all detailed and status information about database migrations.
- validate: Validate that database migrations are available.
- undo: Roll back the database migration.
- Baseline: Create a
flyway_schema_history
table based on the existing database. Only database migrations larger than the baseline version will be applied. - repair: Repair
flyway_schema_history
the table.
command line tool
There are many ways to use Flyway to achieve data migration. Let's first experience the use of Flyway through the method of command line tools.
- First, you need to download Flyway's command-line tool, just download the community version, download address: Download + pricing - Flyway
- After the download is complete, decompress it. After the decompression is complete, the directory structure is as follows;
- Modify the configuration file
/conf/flyway.conf
of Flyway and modify the database configuration;
flyway.url=jdbc:mysql://localhost:3306/flyway?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
flyway.user=root
flyway.password=root
Copy to clipboardErrorCopied
- Add the SQL execution script in the
/sql
directory, and addums_admin
the execution script to create the table hereV1.0.1__Create_ums_admin_table.sql
;
CREATE TABLE `ums_admin`
(
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`username` varchar(64) DEFAULT NULL,
`password` varchar(64) DEFAULT NULL,
`icon` varchar(500) DEFAULT NULL COMMENT '头像',
`email` varchar(100) DEFAULT NULL COMMENT '邮箱',
`nick_name` varchar(200) DEFAULT NULL COMMENT '昵称',
`note` varchar(500) DEFAULT NULL COMMENT '备注信息',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`login_time` datetime DEFAULT NULL COMMENT '最后登录时间',
`status` int(1) DEFAULT '1' COMMENT '帐号启用状态:0->禁用;1->启用',
PRIMARY KEY (`id`)
) ENGINE = InnoDB
AUTO_INCREMENT = 8
DEFAULT CHARSET = utf8 COMMENT ='后台用户表';
Copy to clipboardErrorCopied
- Use the
flyway migrate
command to migrate data. At this time, we will find that we need to use theflyway baseline
command to create a table to save the migration recordsflyway_schema_history
;
- Use the
flyway baseline
command first, then use theflyway migrate
command, the command line will output the successful execution information;
\sql
Add the SQL execution script in the directory,ums_admin
add some data to the table, the execution script isV1.0.2__Add_ums_admin.sql
;
INSERT INTO ums_admin (username, PASSWORD, email, nick_name, STATUS)
VALUES ('test', '123456', '[email protected]', '测试账号', 1);
INSERT INTO ums_admin (username, PASSWORD, email, nick_name, STATUS)
VALUES ('macro', '123456', '[email protected]', '普通账号', 1);
INSERT INTO ums_admin (username, PASSWORD, email, nick_name, STATUS)
VALUES ('andy', '123456', '[email protected]', '普通账号', 1);
Copy to clipboardErrorCopied
- We can use the
flyway info
command to viewflyway_schema_history
the data migration records in the table, and we can find that1.0.2
the version update is still in thePending
state, after using theflyway migrate
command it becomesSuccess
;
- We can create reproducible SQL scripts, which can usually be used to create views, stored procedures, functions, etc., such as
ums_admin
creating a view based on a table, and executing the script asR__Ums_admin_view.sql
;
CREATE
OR REPLACE VIEW ums_admin_view AS
SELECT username,
PASSWORD,
email
FROM ums_admin;
Copy to clipboardErrorCopied
- The
flyway migrate
command can be executed repeatedly (when the script at the beginning of R is changed), the script will beV
executed after all the scripts at the beginning are executed;
- Flyway's rollback mechanism needs to rely on SQL scripts, which are created here
U1.0.1__Create_ums_admin_table.sql
andU1.0.2__Add_ums_admin.sql
two rollback scripts;
# U1.0.1__Create_ums_admin_table.sql
DROP TABLE ums_admin
Copy to clipboardErrorCopied
# U1.0.2__Add_ums_admin.sql
DELETE FROM ums_admin;
Copy to clipboardErrorCopied
- You can use the
flyway undo
command to perform rollback. Unfortunately, the community version does not support rollback. It seems that you still have to make a backup through the tool before the database is upgraded!
Maven plugin
Flyway also provides a Maven plug-in, and the functions supported by the plug-in are basically the same as those of the command-line tool.
- To use Flyway through a plug-in in a Maven project, you first need to add the Flyway plug-in in pom.xml and configure the database connection information;
<!--Flyway的Maven插件-->
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>7.3.2</version>
<configuration>
<url>jdbc:mysql://localhost:3306/flyway?serverTimezone=Asia/Shanghai</url>
<user>root</user>
<password>root</password>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</plugin>
Copy to clipboardErrorCopied
- Create a directory under the resource directory
db\migration
, and put the SQL script used for database upgrade into it;
- Flyway's Maven plugin supports the following commands;
- Double-click
flyway:info
the command to use, and the output is as follows. This method is basically no different from the command-line tool.
[INFO] --- flyway-maven-plugin:7.3.2:info (default-cli) @ mall-tiny-flyway ---
[INFO] Flyway Community Edition 7.3.2 by Redgate
[INFO] Database: jdbc:mysql://localhost:3306/flyway (MySQL 5.7)
[INFO] Schema version: 1.0.2
[INFO]
[INFO] +------------+---------+------------------------+----------+---------------------+----------+
| Category | Version | Description | Type | Installed On | State |
+------------+---------+------------------------+----------+---------------------+----------+
| | 1 | << Flyway Baseline >> | BASELINE | 2020-12-24 11:17:35 | Baseline |
| Versioned | 1.0.1 | Create ums admin table | SQL | 2020-12-24 11:17:42 | Success |
| Versioned | 1.0.2 | Add ums admin | SQL | 2020-12-24 11:33:40 | Success |
| Repeatable | | Ums admin view | SQL | 2020-12-24 11:33:40 | Success |
+------------+---------+------------------------+----------+---------------------+----------+
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.076 s
[INFO] Finished at: 2020-12-24T14:28:16+08:00
[INFO] Final Memory: 28M/286M
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
Copy to clipboardErrorCopied
Use with SpringBoot
Since SpringBoot officially supports Flyway, it is very easy to use Flyway in combination with SpringBoot!
- First add Flyway-related dependencies in pom.xml, note that there is no need to add the version number of Flyway:
<!--Flyway相关依赖-->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
Copy to clipboardErrorCopied
- Modify the configuration file to configure
application.yml
the data source and Flyway;
spring:
datasource:
url: jdbc:mysql://localhost:3306/flyway?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
flyway:
# 启用Flyway功能
enabled: true
# 禁用Flyway的clean命令,使用clean命令会删除schema下的所有表
clean-disabled: true
# 设置Flyway的SQL脚本路径
locations: classpath:db/migration
# 设置版本信息控制表名称,默认flyway_schema_history
table: flyway_schema_history
# 在执行migrate命令时需要有flyway_schema_history表,通过baseline命令可以生成该表
baseline-on-migrate: true
# 指定baseline版本号,低于该版本的SQL脚本在migrate是不会执行
baseline-version: 1
# 设置字符编码
encoding: UTF-8
# 不允许不按顺序迁移
out-of-order: false
# 设置Flyway管控的schema,不设置的话为datasourcel.url中指定的schema
schemas: flyway
# 执行migrate时开启校验
validate-on-migrate: true
Copy to clipboardErrorCopied
- Finally, run the SpringBoot application directly to automatically create the corresponding database, and the console will output the following information.
2020-12-24 14:38:15.659 INFO 10716 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.4.1 by Redgate
2020-12-24 14:38:15.898 INFO 10716 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/flyway (MySQL 5.7)
2020-12-24 14:38:15.972 INFO 10716 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 3 migrations (execution time 00:00.047s)
2020-12-24 14:38:15.988 INFO 10716 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `flyway`.`flyway_schema_history` with baseline ...
2020-12-24 14:38:16.106 INFO 10716 --- [ main] o.f.core.internal.command.DbBaseline : Successfully baselined schema with version: 1
2020-12-24 14:38:16.122 INFO 10716 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `flyway`: 1
2020-12-24 14:38:16.134 INFO 10716 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `flyway` to version 1.0.1 - Create ums admin table
2020-12-24 14:38:16.248 INFO 10716 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `flyway` to version 1.0.2 - Add ums admin
2020-12-24 14:38:16.281 INFO 10716 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `flyway` with repeatable migration Ums admin view
2020-12-24 14:38:16.314 INFO 10716 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 3 migrations to schema `flyway` (execution time 00:00.206s)
Copy to clipboardErrorCopied
Summarize
Compared with manually upgrading the database table structure, it is more advantageous to use Flyway to automatically upgrade. Using Flyway, we can upgrade the database at the same time when we upgrade the application. Since the community version currently does not support database rollback, it is necessary to make a backup before upgrading.
References
官方文档:Documentation - Flyway by Redgate • Database Migrations Made Easy.
Project source code address
https://github.com/macrozheng/mall-learning/tree/master/mall-tiny-flyway
Everyone must remember to like, subscribe, and follow
Prevent it from being found next time
Your support is the driving force for me to continue to create! ! !