Spring-boot集成flyway
1. 介绍
Flyway 是一款开源的数据库版本管理工具。它可以很方便的在命令行中使用,或者在Java应用程序中引入,用于管理我们的数据库版本。
2. pom引用
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>6.1.0</version>
</dependency>
3.配置说明
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://#
username: #
password: #
flyway:
# 是否启用flyway
enabled: true
# 编码格式,默认UTF-8
encoding: UTF-8
# 迁移sql脚本文件存放路径,默认db/migration
locations: classpath:db/migration
# 迁移sql脚本文件名称的前缀,默认V
sql-migration-prefix: V
# 迁移sql脚本文件名称的分隔符,默认2个下划线__
sql-migration-separator: __
# 迁移sql脚本文件名称的后缀
sql-migration-suffixes: .sql
# 迁移时是否进行校验,默认true
validate-on-migrate: true
# 当迁移发现数据库非空且存在没有元数据的表时,自动执行基准迁移,新建schema_version表
baseline-on-migrate: true
说明:
- sql-migration-prefix、sql-migration-separator、sql-migration-suffixes参数,分别定义了脚本的命名规范,前缀、分隔符、后缀
- locations参数规定了脚本目录
4.使用说明
SQL脚本: V3__add_test.sql
#测试flyway
CREATE TABLE `database`.`test_ai` (
`id` int NOT NULL,
`test` varchar(255) NULL COMMENT '测试',
PRIMARY KEY (`id`)
);
注:脚本名称描述:V3 版本号为3;__分隔符;add/update/… 类别;test_ai 表名/功能模块;.sql 后缀
flyway的执行日志
字段说明
字段名 | 字段说明 |
---|---|
version | 版本号 |
description | 版本描述 |
script | 文件名 【脚本文件名称】 |
checksum | 唯一码 【与脚本文件匹配,因此执行后的脚本不能编辑,否则项目启动异常】 |
execution_time | 执行时间 |
success | 执行结果 1 成功 |