当前位置:   article > 正文

spring boot集成flyway快速入门demo

spring boot集成flyway快速入门demo

一、flyway介绍

我们都知道,Git/ SVN 是代码界的版本控制工具,那么,Flyway 就是一款数据库界的版本控制工具,它可以记录数据库的变化记录。 可能很多公司都是通过人工去维护、同步数据库脚本,但经常会遇到疏忽而遗漏的情况,举个简单的例子:

我们在开发环境对某个表新增了一个字段,而提交测试时却忘了提交该 SQL 脚本,导致出现 bug 而测试中断,从而影响开发、测试的工作效率。

有了 Flyway,我们可以按版本约定,统一管理所有的 SQL 脚本变更,在所有环境自动同步数据库,而无需人为手工控制,再也不用担心因数据库不同步而导致的各种环境问题。

二、mysql环境准备

 
 
  1. version: '3'
  2. services:
  3. mysql:
  4. image: registry.cn-hangzhou.aliyuncs.com/zhengqing/mysql:5.7 # 原镜像`mysql:5.7`
  5. container_name: mysql_3306 # 容器名为'mysql_3306'
  6. restart: unless-stopped # 指定容器退出后的重启策略为始终重启,但是不考虑在Docker守护进程启动时就已经停止了的容器
  7. volumes: # 数据卷挂载路径设置,将本机目录映射到容器目录
  8. - "./mysql/my.cnf:/etc/mysql/my.cnf"
  9. - "./mysql/init-file.sql:/etc/mysql/init-file.sql"
  10. - "./mysql/data:/var/lib/mysql"
  11. # - "./mysql/conf.d:/etc/mysql/conf.d"
  12. - "./mysql/log/mysql/error.log:/var/log/mysql/error.log"
  13. - "./mysql/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d" # 可执行初始化sql脚本的目录 -- tips:`/var/lib/mysql`目录下无数据的时候才会执行(即第一次启动的时候才会执行)
  14. environment: # 设置环境变量,相当于docker run命令中的-e
  15. TZ: Asia/Shanghai
  16. LANG: en_US.UTF-8
  17. MYSQL_ROOT_PASSWORD: root # 设置root用户密码
  18. MYSQL_DATABASE: demo # 初始化的数据库名称
  19. ports: # 映射端口
  20. - "3306:3306"

启动

 
 
docker-compose -f docker-compose.yml -p mysql5.7 up -d

三、代码工程

pom.xml

 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springboot-demo</artifactId>
  7. <groupId>com.et</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11. <artifactId>flyway</artifactId>
  12. <properties>
  13. <maven.compiler.source>8</maven.compiler.source>
  14. <maven.compiler.target>8</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-web</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-autoconfigure</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-test</artifactId>
  28. <scope>test</scope>
  29. </dependency>
  30. <!-- flyway-core 依赖 V:7.7.3 -->
  31. <dependency>
  32. <groupId>org.flywaydb</groupId>
  33. <artifactId>flyway-core</artifactId>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-jdbc</artifactId>
  38. </dependency>
  39. <dependency>
  40. <groupId>mysql</groupId>
  41. <artifactId>mysql-connector-java</artifactId>
  42. <scope>runtime</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.projectlombok</groupId>
  46. <artifactId>lombok</artifactId>
  47. <optional>true</optional>
  48. </dependency>
  49. </dependencies>
  50. </project>

application.yaml

 
 
  1. server:
  2. port: 8088
  3. spring:
  4. # flyway 配置
  5. flyway:
  6. # 开启 flyway
  7. enabled: true
  8. # 是否禁用数据库清理
  9. clean-disabled: true
  10. # SQL 迁移的编码
  11. encoding: UTF-8
  12. # 迁移脚本的位置,默认 db/migration.
  13. locations: classpath:db/migration
  14. # SQL 迁移的文件名前缀。
  15. sql-migration-prefix: V
  16. # SQL 迁移的文件名分隔符。
  17. sql-migration-separator: __
  18. # SQL 迁移的文件名后缀。
  19. sql-migration-suffixes: .sql
  20. # 是否在执行迁移时自动调用验证。
  21. validate-on-migrate: true
  22. # 迁移时发现目标 schema 非空,而且带有没有元数据的表时,是否自动执行基准迁移,默认 false.
  23. baseline-on-migrate: true
  24. # JDBC 驱动程序的完全限定名称。 默认根据 URL 自动检测。
  25. driver-class-name:
  26. # 要迁移的数据库的 DBC url。 如果未设置,则使用主要配置的数据源。
  27. url: jdbc:mysql://localhost:3306/zq?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
  28. # 要迁移的数据库的登录密码。
  29. password: root
  30. # 要迁移的数据库的登录用户。
  31. user: root
  32. # MySQL 配置
  33. datasource:
  34. driver-class-name: com.mysql.cj.jdbc.Driver
  35. url: jdbc:mysql://localhost:3306/zq
  36. username: root
  37. password: root

四、测试

测试一: 创建表 sql文件,在resource文件下面,创建db/migration,

V1__user_version.sql

 
 
  1. DROP TABLE IF EXISTS user ;
  2. CREATE TABLE `user` (
  3. `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  4. `name` varchar(20) NOT NULL COMMENT '姓名',
  5. `age` int(5) DEFAULT NULL COMMENT '年龄',
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

启动应用spring boot应用,查看结果

 
 
  1. 2024-03-05 10:27:54.267 INFO 20728 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate
  2. 2024-03-05 10:27:54.862 INFO 20728 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/zq (MySQL 5.7)
  3. 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)
  4. 2024-03-05 10:27:55.051 INFO 20728 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `zq`.`flyway_schema_history` ...
  5. 2024-03-05 10:27:55.151 INFO 20728 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `zq`: << Empty Schema >>
  6. 2024-03-05 10:27:55.159 INFO 20728 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `zq` to version 1 - user version
  7. 2024-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)
  8. 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)
  9. 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 ''
  10. 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个脚本,它会从上次的版本号开始往后执行。如下图所示

 
 
  1. 2024-03-05 10:31:22.738 INFO 29244 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  2. 2024-03-05 10:31:22.931 INFO 29244 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate
  3. 2024-03-05 10:31:23.479 INFO 29244 --- [ main] o.f.c.internal.database.DatabaseFactory : Database: jdbc:mysql://localhost:3306/zq (MySQL 5.7)
  4. 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)
  5. 2024-03-05 10:31:23.537 INFO 29244 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `zq`: 1
  6. 2024-03-05 10:31:23.547 INFO 29244 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `zq` to version 1.1 - alter table user
  7. 2024-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)
  8. 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 ''
  9. 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 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/218534
推荐阅读
相关标签
  

闽ICP备14008679号