网络知识 娱乐 SpringBoot数据库管理 - 用flyway对数据库管理和迁移

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

上文介绍了Liquibase,以及和SpringBoot的集成。除了Liquibase之外,还有一个组件Flyway也是经常被使用到的类似的数据库版本管理中间件。本文主要介绍Flyway, 以及SpringBoot集成Flyway。@pdai

知识准备

需要了解Flyway和要解决的问题,以及一些基础概念,比如变迁(migrations),常用命令(commands)等。

什么是Flyway? 要解决什么问题?

Flyway是一款数据库迁移(migration)工具。简单点说,就是在你部署应用的时候,帮你执行数据库脚本的工具。Flyway支持SQL和Java两种类型的脚本,你可以将脚本打包到应用程序中,在应用程序启动时,由Flyway来管理这些脚本的执行,这些脚本被Flyway称之为migration。

PS: 本质上和liquibase机制一致

按照verion的顺序(和数据库中的更新记录对比,找到未更新的),更新如下

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

更新记录如下

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Flyway中的变迁(migrations)

对于Flyway,对数据库的所有更改都称为变迁(migrations),等同于liquibase中的changeset。

在Flyway中变迁(migrations)定义的更细,包含如下三种:

  1. 版本变迁(Versioned Migrations): 每个版本执行一次,包含有版本、描述和校验和;常用于创建,修改,删除表;插入,修改数据等
  2. 撤销变迁(Undo Migrations): 版本变迁(Versioned Migrations)的反操作。
  3. 可重复变迁(Repeatable Migrations): 可以执行多次,包含描述和校验和(没有版本);主要用于视图,存储过程,函数等

这三种类型对应的格式如下:

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

  1. 前缀: V 代表版本变迁(Versioned Migrations), U 代表撤销变迁(Undo Migrations), R 代表可重复变迁(Repeatable Migrations)
  2. 版本号: 唯一的版本号,比如V1.0.1
  3. 分隔符: __ (两个下划线)
  4. 描述信息: 描述信息
  5. 后缀: .sql

(PS:撤销变迁(Undo Migrations)在收费版本中)

Flyway中常用命令

Flyway中的常用commands有哪些?什么含义?

Migrate: 是Flyway工作流的核心。它将扫描文件系统或类路径以查找可用的Migrate。它将把它们与已应用于数据库的Migrate进行比较。如果发现任何差异则迁移数据。

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Clean: 清除掉对应数据库Schema中所有的对象,包括表结构,视图,存储过程等,clean操作在dev 和 test阶段很好用;(PS:不能用在product环境)

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Info: 用于打印所有的Migrations的详细和状态信息,也是通过MetaData和Migrations完成的,可以快速定位当前的数据库版本;

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Validate: 验证以及apply的Migrations是否有变更,默认开启的;原理是对比MetaData表与本地Migrations的checkNum值,如果值相同则验证通过,否则失败。

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Undo: Migrate的反操作, 即回滚操作,这是收费功能

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

BaseLine:对已经存在数据库Schema结构的数据库一种解决方案。实现在非空数据库新建MetaData表,并把Migrations应用到该数据库;也可以应用到已有表结构的数据库中也可以实现添加Metadata表。

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Repair:repair操作能够修复metaData表,该操作在metadata出现错误时很有用

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

简单示例

这里主要介绍基于SpringBoot集成flyway来管理数据库的变更。

POM依赖

Maven 包的依赖,主要包含mysql驱动, JDBC(这里spring-boot-starter-data-jpa包含了jdbc包,当然直接引入jdbc包也行),以及flyway包。

<dependency>n <groupId>mysql</groupId>n <artifactId>mysql-connector-java</artifactId>n <version>8.0.28</version>n</dependency>n<dependency>n <groupId>com.github.wenhao</groupId>n <artifactId>jpa-spec</artifactId>n <version>3.1.0</version>n</dependency>n<dependency>n <groupId>org.springframework.boot</groupId>n <artifactId>spring-boot-starter-data-jpa</artifactId>n</dependency>nn<dependency>n <groupId>org.flywaydb</groupId>n <artifactId>flyway-mysql</artifactId>n <version>8.5.7</version>n</dependency>n

yml配置

SpringBoot AutoConfig默认已经包含了对flyway的配置,在spring.flyway配置下

spring:n datasource:n url: jdbc:mysql://localhost:3306/test_db_flyway?useSSL=false&autoReconnect=true&characterEncoding=utf8n driver-class-name: com.mysql.cj.jdbc.Drivern username: rootn password: bfXa4Pt2lUUScy8jakXfn flyway:n enabled: truen encoding: UTF-8n # 可以支持多个location, 用','隔开n locations: classpath:db/migrationn # migrate是否校验n validate-on-migrate: truen

在开发时,更多的配置可以从如下SpringBoot AutoConfig中找到。

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

Migrate配置

这里我们准备两个Versioned Migration

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

  • V1.0__Init_DB.sql

DROP TABLE IF EXISTS `tb_user`;n/*!40101 SET @saved_cs_client = @@character_set_client */;n/*!40101 SET character_set_client = utf8 */;nCREATE TABLE `tb_user` (n `id` int(11) NOT NULL AUTO_INCREMENT,n `user_name` varchar(45) NOT NULL,n `password` varchar(45) NOT NULL,n `email` varchar(45) DEFAULT NULL,n `phone_number` int(11) DEFAULT NULL,n `description` varchar(255) DEFAULT NULL,n `create_time` datetime DEFAULT NULL,n `update_time` datetime DEFAULT NULL,n PRIMARY KEY (`id`)n) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;n/*!40101 SET character_set_client = @saved_cs_client */;n

  • V1.1__Init_Data.sql

LOCK TABLES `tb_user` WRITE;n/*!40000 ALTER TABLE `tb_user` DISABLE KEYS */;nINSERT INTO `tb_user` VALUES (1,'pdai','dfasdf','suzhou.daipeng@gmail.com',1212121213,'afsdfsaf','2021-09-08 17:09:15','2021-09-08 17:09:15');n/*!40000 ALTER TABLE `tb_user` ENABLE KEYS */;nUNLOCK TABLES;n

测试

启动springBootApplication, 我们可以看到如下log

2022-04-13 07:56:56.122 INFO 86030 --- [ main] o.f.c.i.database.base.DatabaseType : Database: jdbc:mysql://localhost:3306/test_db_flyway (MySQL 8.0)n2022-04-13 07:56:56.220 INFO 86030 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.074s)n2022-04-13 07:56:56.245 INFO 86030 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `test_db_flyway`.`flyway_schema_history` ...n2022-04-13 07:56:56.270 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.282 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.292 INFO 86030 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `test_db_flyway`: << Empty Schema >>n2022-04-13 07:56:56.297 INFO 86030 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `test_db_flyway` to version "1.0 - Init DB"n2022-04-13 07:56:56.309 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Unknown table 'test_db_flyway.tb_user' (SQL State: 42S02 - Error Code: 1051)n2022-04-13 07:56:56.309 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.309 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.310 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. (SQL State: HY000 - Error Code: 3719)n2022-04-13 07:56:56.310 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.317 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)n2022-04-13 07:56:56.317 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Integer display width is deprecated and will be removed in a future release. (SQL State: HY000 - Error Code: 1681)n2022-04-13 07:56:56.317 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous. (SQL State: HY000 - Error Code: 3719)n2022-04-13 07:56:56.317 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.318 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.333 INFO 86030 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `test_db_flyway` to version "1.1 - Init Data"n2022-04-13 07:56:56.334 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.335 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Table storage engine for 'tb_user' doesn't have this option (SQL State: HY000 - Error Code: 1031)n2022-04-13 07:56:56.335 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.335 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 1 rows affectedn2022-04-13 07:56:56.336 WARN 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Table storage engine for 'tb_user' doesn't have this option (SQL State: HY000 - Error Code: 1031)n2022-04-13 07:56:56.337 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.337 INFO 86030 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affectedn2022-04-13 07:56:56.346 INFO 86030 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 2 migrations to schema `test_db_flyway`, now at version v1.1 (execution time 00:00.058s)n

生成的flyway更新的记录,test_db_flyway.flyway_schema_history

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

已经user表结构和数据

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

进一步理解

通过几个问题,进一步理解。

MySQL的支持性问题

从Flyway对MySQL支持性,可以看出官方期望通过MySQL使用的大量基数获取更多的付费用户。

首先,如果你只是引入flyway-core:8.5.7的包时,会报如下错误

Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 8.0n at org.flywaydb.core.internal.database.DatabaseTypeRegister.getDatabaseTypeForConnection(DatabaseTypeRegister.java:106) ~[flyway-core-8.5.7.jar:na]n at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:76) ~[flyway-core-8.5.7.jar:na]n at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:147) ~[flyway-core-8.5.7.jar:na]n at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.5.7.jar:na]n at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.5.3.jar:2.5.3]n at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.9.jar:5.3.9]n at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.9.jar:5.3.9]n ... 18 common frames omittedn

因为找不到内置的Datebase type

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

所以你应该引入的包是flyway-mysql:8.5.7,而有意思的是这个包中包含的flyway-core版本是7.7.3

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

然后我们看下官网对MySQL的支持性,这骚操作,5.7版本还需要使用企业版。就是为了收费,折腾...。

SpringBoot数据库管理 - 用flyway对数据库管理和迁移

示例源码

https://github.com/realpdai/tech-pdai-spring-demos

更多内容

告别碎片化学习,无套路一站式体系化学习后端开发: Java 全栈知识体系(https://pdai.tech)