2024-03-13
分类:开源工具
阅读(265) 评论(0)
一、flyway介绍
----------
我们都知道,Git/ SVN 是代码界的版本控制工具,那么,Flyway 就是一款数据库界的版本控制工具,它可以记录数据库的变化记录。 可能很多公司都是通过人工去维护、同步数据库脚本,但经常会遇到疏忽而遗漏的情况,举个简单的例子:
> 我们在开发环境对某个表新增了一个字段,而提交测试时却忘了提交该 SQL 脚本,导致出现 bug 而测试中断,从而影响开发、测试的工作效率。
有了 Flyway,我们可以按版本约定,统一管理所有的 SQL 脚本变更,在所有环境自动同步数据库,而无需人为手工控制,再也不用担心因数据库不同步而导致的各种环境问题。
二、mysql环境准备
-----------
```
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
version: '3'services: mysql: image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7 # 原镜像`mysql:5.7` container_name: mysql_3306 # 容器名为'mysql_3306' restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器 volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录 - "./mysql/my.cnf:/etc/mysql/my.cnf" - "./mysql/init-file.sql:/etc/mysql/init-file.sql" - "./mysql/data:/var/lib/mysql"# - "./mysql/conf.d:/etc/mysql/conf.d" - "./mysql/log/mysql/error.log:/var/log/mysql/error.log" - "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # 可执行初始化sql脚本的目录 -- tips:`/var/lib/mysql`目录下无数据的时候才会执行(即第一次启动的时候才会执行) environment: # 设置环境变量,相当于docker run命令中的-e TZ: Asia/Shanghai LANG: en_US.UTF-8 MYSQL_ROOT_PASSWORD: root # 设置root用户密码 MYSQL_DATABASE: demo # 初始化的数据库名称 ports: # 映射端口 - "3306:3306"
```
启动
```
*
docker-compose -f docker-compose.yml -p mysql5.7 up -d
```
三、代码工程
------
pom.xml
```
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
springboot-demo com.et 1.0-SNAPSHOT 4.0.0
flyway
8 8
org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-autoconfigure org.springframework.boot spring-boot-starter-test test
org.flywaydb flyway-core org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime org.projectlombok lombok true
```
application.yaml
```
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
server: port: 8088spring: # flyway 配置 flyway: # 开启 flyway enabled: true # 是否禁用数据库清理 clean-disabled: true # SQL 迁移的编码 encoding: UTF-8 # 迁移脚本的位置,默认 db/migration. locations: classpath:db/migration # SQL 迁移的文件名前缀。 sql-migration-prefix: V # SQL 迁移的文件名分隔符。 sql-migration-separator: __ # SQL 迁移的文件名后缀。 sql-migration-suffixes: .sql # 是否在执行迁移时自动调用验证。 validate-on-migrate: true # 迁移时发现目标 schema 非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认 false. baseline-on-migrate: true # JDBC 驱动程序的完全限定名称。 默认根据 URL 自动检测。 driver-class-name: # 要迁移的数据库的 DBC url。 如果未设置,则使用主要配置的数据源。 url: jdbc:mysql://localhost:3306/zq?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT # 要迁移的数据库的登录密码。 password: root # 要迁移的数据库的登录用户。 user: root
# MySQL 配置 datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/zq username: root password: root
```
四、测试
----
测试一: 创建表 sql文件,在resource文件下面,创建db/migration,
### V1__user_version.sql
```
*
*
*
*
*
*
*
DROP TABLE IF EXISTS user ;CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `name` varchar(20) NOT NULL COMMENT '姓名', `age` int(5) DEFAULT NULL COMMENT '年龄', PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
```
启动应用spring boot应用,查看结果
```
*
*
*
*
*
*
*
*
*
*
2024-03-05 10:27:54.267 INFO 20728 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate2024-03-05 10:27:54.862 INFO 20728 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/zq (MySQL 5.7)2024-03-05 10:27:54.901 INFO 20728 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 1 migration (execution time 00:00.015s)2024-03-05 10:27:55.051 INFO 20728 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `zq`.`flyway_schema_history` ...2024-03-05 10:27:55.151 INFO 20728 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `zq`: << Empty Schema >>2024-03-05 10:27:55.159 INFO 20728 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `zq` to version 1 - user version2024-03-05 10:27:55.166 WARN 20728 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : DB: Unknown table 'zq.user' (SQL State: 42S02 - Error Code: 1051)2024-03-05 10:27:55.217 INFO 20728 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema `zq` (execution time 00:00.075s)2024-03-05 10:27:55.290 INFO 20728 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''2024-03-05 10:27:55.293 INFO 20728 --- [ main] com.et.flyway.DemoApplication : Started DemoApplication in 2.896 seconds (JVM running for 3.283)
```
### V1_1__alter_table_user.sql
```
*
ALTER TABLE `user` ADD COLUMN `address` VARCHAR(20) DEFAULT NULL;
```
启动应用spring boot应用,第二次并不会执行2个脚本,它会从上次的版本号开始往后执行。如下图所示
```
*
*
*
*
*
*
*
*
*
2024-03-05 10:31:22.738 INFO 29244 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'2024-03-05 10:31:22.931 INFO 29244 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate2024-03-05 10:31:23.479 INFO 29244 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/zq (MySQL 5.7)2024-03-05 10:31:23.522 INFO 29244 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.022s)2024-03-05 10:31:23.537 INFO 29244 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `zq`: 12024-03-05 10:31:23.547 INFO 29244 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `zq` to version 1.1 - alter table user2024-03-05 10:31:23.617 INFO 29244 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 1 migration to schema `zq` (execution time 00:00.089s)2024-03-05 10:31:23.684 INFO 29244 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''2024-03-05 10:31:23.687 INFO 29244 --- [ main] com.et.flyway.DemoApplication : Started DemoApplication in 2.702 seconds (JVM running for 3.027)
```
五、引用
----
* https://flywaydb.org/
* http://www.liuhaihua.cn/archives/710280.html
众生皆苦,唯有自渡!