一、概念
数据库版本控制管理工具,通过集成Flyway可以实现启动项目时自动执行项目迭代升级。
Flyway已经支持数据库包括: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 Phoeni
二、集成
1、依赖
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>8.2.0</version>
</dependency>
2、配置
##是否启动,默认开启
spring.flyway.enabled = true
##脚本存放路径
spring.flyway.locations = classpath:db.migration
##当flyway第一次运行时,会在我们对应的数据库中新建一个记录脚本运行情况的
spring.flyway.table=flyway_schema_history
## 用户名
spring.flyway.user=root
## 密码
spring.flyway.password=root
## 数据库驱动
spring.flyway.driver-class-name=com.mysql.cj.jdbc.Driver
#设置flyway配置基线
spring.flyway.baseline-on-migrate=true
#迁移配置
spring.flyway.sql-migration-prefix=V
spring.flyway.sql-migration-separator=__
spring.flyway.sql-migration-suffixes=.sql
#编码
spring.flyway.encoding=UTF-8
#是否允许执行未按顺序的迁移脚本
spring.flyway.out-of-order=true
#日志
logging.level.org.flywaydb=debug
3、创建脚本

4、启动项目,即可看到

5、遇到的几个问题
1)org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 8.0
一开始想用flyway最新的版本9.xx,启动后报错,查询后新版本不支持mysql
flyway-core 8.2.1及以后的版本确实是不再支持MySQL
解决方式:
- 降版本,使用8.2.0
- 添加依赖,官方Flyway 8.2.1版本发布说明(https://flywaydb.org/documentation/learnmore/releaseNotes#8.2.1)
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
</dependency>
2)Found non-empty schema(s) "PUBLIC" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history tabl解决方法:
#设置flyway配置基线
spring.flyway.baseline-on-migrate=true