它仅基于 7 个基本命令: Migrate、 Clean、 Info、 Validate、 Undo、 Baseline和 Repair。
迁移可以用SQL (支持特定于数据库的语法(如 PL/SQL、T-SQL 等))或Java (用于高级数据转换或处理 LOB)编写。
它有一个命令行客户端。如果您在 JVM 上,我们建议使用Java API 在应用程序启动时迁移数据库。或者,您也可以使用Maven 插件 或Gradle 插件。
如果这还不够,还有 适用于 Spring Boot、Dropwizard、Grails、Play、SBT、Ant、Griffon、Grunt、Ninja 等的插件!
支持的数据库有 Oracle、 SQL Server(包括 Amazon RDS 和 Azure SQL 数据库)、 Azure Synapse(以前的数据仓库)、 DB2、 MySQL(包括 Amazon RDS、Azure 数据库和 Google Cloud SQL)、 Aurora MySQL、 MariaDB、 Percona XtraDB Cluster、 TestContainers , PostgreSQL (包括 Amazon RDS, Azure Database, Google Cloud SQL, TimescaleDB, YugabyteDB & Heroku), Aurora PostgreSQL , Redshift , CockroachDB , SAP HANA , Sybase ASE、 Informix、 H2、 HSQLDB、 Derby、 Snowflake、 SQLite和 Firebird。
Oracle 支持情况:Oracle 12.2 及以上版本。11g 可通过修改源码实现使用。
Spring Boot 官方提供了两款高级别的迁移工具:Flyway 和 Liquibase
spring:
flyway:
# flyway 的 clean 命令会删除指定 schema 下的所有 table, 应该禁掉
clean-disabled: true
# 迁移脚本的位置
locations: classpath:db/migration
#flyway 的 metadata 表名, 缺省为 flyway_schema_history
table: my_flyway_schema_history
#多人协作开发, 很可能先 apply 了自己本地的最新 SQL 代码, 然后发现其他同事早先时候提交的 SQL 代码还没有 apply,
#所以 开发环境应该设置 spring.flyway.outOfOrder=true, 这样 flyway 将能加载漏掉的老版本 SQL 文件;
#生产环境建议为 spring.flyway.outOfOrder=false
out-of-order: true
为了被 Flyway 采用,SQL 迁移必须符合以下命名模式:
V20220504113919__create_student.sql
U20220504132902__create_student.sql
R__add_student.sql
文件名如下组成:
V
用于版本化(可配置)、U
撤消(可配置)和 R
可重复迁移(可配置)__
两个下划线(可配置).sql
(可配置)在 Spring Boot 应用程序启动时,自动进行迁移
2022-05-04 15:11:36.325 INFO 13303 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.7.3 by Redgate
2022-05-04 15:11:36.642 INFO 13303 --- [ main] o.f.c.i.database.base.DatabaseType : Database: jdbc:oracle:thin:@localhost:1521:XE (Oracle 12.1)
2022-05-04 15:11:36.793 INFO 13303 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 3 migrations (execution time 00:00.023s)
2022-05-04 15:11:36.815 INFO 13303 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema "FLYWAY": << Empty Schema >>
2022-05-04 15:11:36.815 WARN 13303 --- [ main] o.f.core.internal.command.DbMigrate : outOfOrder mode is active. Migration of schema "FLYWAY" may not be reproducible.
2022-05-04 15:11:36.830 INFO 13303 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "FLYWAY" to version "20220504113919 - create student"
2022-05-04 15:11:36.873 INFO 13303 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affected
2022-05-04 15:11:36.938 INFO 13303 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "FLYWAY" to version "20220504132902 - create seq"
2022-05-04 15:11:36.944 INFO 13303 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : 0 rows affected
2022-05-04 15:11:36.977 INFO 13303 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "FLYWAY" to version "20220504135829 - select student"
2022-05-04 15:11:36.982 INFO 13303 --- [ main] o.f.c.i.s.DefaultSqlScriptExecutor : +----+------+
| ID | NAME |
+----+------+
| No rows r |
+----+------+
2022-05-04 15:11:37.013 INFO 13303 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 3 migrations to schema "FLYWAY", now at version v20220504135829 (execution time 00:00.208s)
迁移会记录到 flyway_schema_history 表中
installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success |
---|---|---|---|---|---|---|---|---|---|
1 | 20220504113919 | create student | SQL | V20220504113919__create_student.sql | 1002084112 | FLYWAY | 2022-05-04 15:11:36.909950 | 48 | 1 |
2 | 20220504132902 | create seq | SQL | V20220504132902__create_seq.sql | -1063390250 | FLYWAY | 2022-05-04 15:11:36.956655 | 9 | 1 |
3 | 20220504135829 | select student | SQL | V20220504135829__select_student.sql | -1463386058 | FLYWAY | 2022-05-04 15:11:36.995406 | 9 | 1 |
使用 Flyway Migration Creation,可以很方便的在IDEA中创建Versioned Migration和Repeatable Migration
官方网站:https://flywaydb.org
GitHub:https://github.com/flyway/flyway
Q:db/migration 迁移文件越来越多的问题
A:可以在 db/migration 下面自己建立文件夹,存放自己的迁移
Q:多个项目时,因为要集成到项目里面,自动迁移,是需要新建个项目吗?专门用来存放脚本吗?
A:目前有多种方案,采用其中一种即可:
Q:集群部署,同时启动执行,会不会有问题
A:会有问题,配置一台启用 Flyway 就可以了,在启动的命令里面加上-Dspring.flyway.enabled=true
,其他的可以在 Apollo 或者 Nacos 中 配置 spring.flyway.enabled=false
。这样启动的时候,只有一台实例自动化执行 Flyway 的迁移。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有