挺帶勁!這款開(kāi)源數(shù)據(jù)庫(kù)遷移工具超牛
遷移數(shù)據(jù)庫(kù)可以說(shuō)是日常工作中的家常便飯,不過(guò),如果一不小心把數(shù)據(jù)庫(kù)搞炸了,那就麻煩大了(搞不好就得背鍋了)。隨著數(shù)據(jù)量的逐漸增加、表結(jié)構(gòu)的改動(dòng),數(shù)據(jù)庫(kù)各類(lèi)遷移、升級(jí)是勢(shì)在必行。所以,有一款好的遷移工具是可以大大提高工作效率,還能很大程度上避免手動(dòng)操作帶來(lái)的失誤或數(shù)據(jù)損壞、丟失的風(fēng)險(xiǎn)。
今天,民工哥就給大家介紹一款這樣的數(shù)據(jù)庫(kù)遷移神器。
Flyway簡(jiǎn)介
flyway 是一個(gè)敏捷工具,用于數(shù)據(jù)庫(kù)的移植。采用 Java 開(kāi)發(fā),支持所有兼容 JDBC 的數(shù)據(jù)庫(kù)。主要用于在你的應(yīng)用版本不斷升級(jí)的同時(shí),升級(jí)你的數(shù)據(jù)庫(kù)結(jié)構(gòu)和里面的數(shù)據(jù)。
Flyway 特點(diǎn)
-
簡(jiǎn)單:使用和學(xué)習(xí)簡(jiǎn)單,通過(guò)不同版本的SQL腳本實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移。
-
專(zhuān)業(yè):專(zhuān)注于數(shù)據(jù)庫(kù)遷移功能,你無(wú)需擔(dān)心有任何問(wèn)題。
-
功能強(qiáng)大:支持多種數(shù)據(jù)庫(kù),擁有大量的第三方工具,支持CI/DI。
工作平臺(tái)
Windows,macOS,Linux,Docker,Java 和 Android
支持的構(gòu)建工具
Maven 和 Gradle
支持的數(shù)據(jù)庫(kù)
Oracle、SQL Server、DB2、MySQL、Aurora MySQL、MariaDB、Percona XtraDB群集、PostgreSQL、Aurora PostgreSQL、Redshift、CockroachDB、SAP HANA、Sybase ASE、Informix、H2、HSQLDB、Derby、SQLite、Firebird
Flyway 工作原理
一開(kāi)始你向Flyway指向一個(gè)空數(shù)據(jù)庫(kù)時(shí):
它就會(huì)去查找schema歷史表,如果此時(shí)數(shù)據(jù)庫(kù)是空的,F(xiàn)lyWAY會(huì)自己創(chuàng)建一張歷史表,然后現(xiàn)在就有了一個(gè)僅包含空表flyway_schema_history(默認(rèn))的數(shù)據(jù)庫(kù)。
flyway_schema_history 這個(gè)表用來(lái)跟蹤數(shù)據(jù)庫(kù)的狀態(tài)。
數(shù)據(jù)庫(kù)的遷移是按版本號(hào)來(lái)順序執(zhí)行的:
每次遷移被執(zhí)行后 schema_history表會(huì)依此更新記錄
后面如果再次遷移的話(huà),F(xiàn)lyway會(huì)再次掃描應(yīng)用的文件系統(tǒng)和類(lèi)路徑、歷史表檢查,如果版本號(hào)低于或等于當(dāng)前版本號(hào),則忽略遷移操作。
增量遷移
仍然按版本號(hào)進(jìn)行:
schema_history歷史表會(huì)依此更新記錄
整個(gè)過(guò)程就是這樣的!每次需要修改數(shù)據(jù)庫(kù)時(shí),無(wú)論是結(jié)構(gòu) (DDL) 還是參考數(shù)據(jù) (DML),只需 創(chuàng)建一個(gè)版本號(hào)高于當(dāng)前版本號(hào) 的新遷移。下次 Flyway 啟動(dòng)時(shí),它會(huì)找到它并相應(yīng)地升級(jí)數(shù)據(jù)庫(kù)。
參考地址:https://flywaydb.org/documentation/getstarted/how
下載
下載地址:https://flywaydb.org/download
有免費(fèi)與收費(fèi)版本可供選擇,我們當(dāng)然選擇最好用的免費(fèi)版本了,哈哈哈哈。。。。
Linux系統(tǒng)
- wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/7.11.0/flyway-commandline-7.11.0-linux-x64.tar.gz | tar xvz && ln -s `pwd`/flyway-7.11.0/flyway /usr/local/bin
Docker
- sh -c 'echo "docker run --rm flyway/flyway:7.11.0 $*" > /usr/local/bin/flyway && chmod +x /usr/local/bin/flyway'
目錄結(jié)構(gòu)
- [root@centos7 ~]# tree -L 1 ./flyway-7.11.0
- ./flyway-7.11.0
- ├── conf #配置文件
- ├── drivers #JDBD驅(qū)動(dòng)程序
- ├── flyway #macOS/Linux 可執(zhí)行文件
- ├── flyway.cmd #Windows 可執(zhí)行文件
- ├── jars #基于 Java 的遷移(作為 jars)
- ├── jre
- ├── lib
- ├── licenses
- ├── README.txt
- └── sql #SQL 遷移
- 7 directories, 3 files
使用
使用之前需要了解的一些概念
-
版本:對(duì)數(shù)據(jù)庫(kù)的每一次變更可稱(chēng)為一個(gè)版本
-
遷移:Flyway把數(shù)據(jù)庫(kù)結(jié)構(gòu)從一個(gè)版本更新到另一個(gè)版本叫做遷移
-
可用的遷移:Flyway的文件系統(tǒng)識(shí)別出來(lái)的遷移版本
-
已經(jīng)應(yīng)用的遷移:Flyway已經(jīng)對(duì)數(shù)據(jù)庫(kù)執(zhí)行過(guò)的遷移
命令行使用
- > flyway [options] command
flyway主要基于6種基本命令
- migrate #遷移數(shù)據(jù)庫(kù)
- clean #清除所有配置
- info #打印遷移相關(guān)的詳細(xì)信息與狀態(tài)信息
- validate #驗(yàn)證遷移的可用性
- baseline #為現(xiàn)有數(shù)據(jù)庫(kù)創(chuàng)建基準(zhǔn)版本,高于基準(zhǔn)版本的才會(huì)被執(zhí)行遷移動(dòng)作
- repair #修改 schema history 表
修改配置文件
- [root@centos7 ~]# cd ./flyway-7.11.0/conf/
- [root@centos7 conf]# ll
- total 24
- -rw-r--r-- 1 root root 22943 Jul 1 2021 flyway.conf
- [root@centos7 conf]# vim flyway.conf
- flyway.url=jdbc:mysql://localhost:3306/mingongge?useUnicode=true
- flyway.user=root
- flyway.password=123456
準(zhǔn)備一個(gè)測(cè)試用的sql腳本
這里還得提一下命令規(guī)范的問(wèn)題,否則后續(xù)的sql腳本無(wú)法執(zhí)行。
-
Prefix(前綴):V 用于數(shù)據(jù)庫(kù)遷移的版本號(hào),U 用于數(shù)據(jù)庫(kù)回滾的版本號(hào),R 表示可重復(fù)執(zhí)行的數(shù)據(jù)庫(kù)遷移
-
Version(版本號(hào)):Flyway會(huì)按照版本號(hào)的大小順序來(lái)執(zhí)行數(shù)據(jù)庫(kù)遷移腳本
-
Separator(分隔符):使用 雙下劃線(xiàn) 分隔符
-
Description(描述):用于描述遷移腳本的說(shuō)明性文字
-
Suffix(后綴):.sql文件
在sql目錄下面添加一個(gè)測(cè)試sql腳本,這里是一個(gè)簡(jiǎn)單的創(chuàng)建表的語(yǔ)句。
- [root@centos7 sql]# pwd
- /root/flyway-7.11.0/sql
- [root@centos7 sql]# cat V1.0__Create_test_table.sql
- CREATE TABLE `test_table`
- (
- `id` bigint(10) NOT NULL,
- `username` varchar(64) DEFAULT NULL,
- `password` varchar(64) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE = InnoDB
- DEFAULT CHARSET = utf8 COMMENT ='測(cè)試表';
開(kāi)始遷移
- [root@centos7 ~]# flyway migrate
- Flyway Teams Edition 7.11.0 by Redgate
- Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7)
- ----------------------------------------
- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams
- ----------------------------------------
- Successfully validated 0 migrations (execution time 00:00.028s)
- WARNING: No migrations found. Are your locations set up correctly?
- ERROR: Found non-empty schema(s) `mingongge` but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
最后的提示很明白,需要?jiǎng)?chuàng)建schema history table,那么好吧,創(chuàng)建一個(gè)唄。
- [root@centos7 ~]# flyway baseline
- Flyway Teams Edition 7.11.0 by Redgate
- Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7)
- ----------------------------------------
- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams
- ----------------------------------------
- Creating Schema History table `mingongge`.`flyway_schema_history` with baseline ...
- Successfully baselined schema with version: 1
再次執(zhí)行
- [root@centos7 sql]# flyway migrate
- Flyway Teams Edition 7.11.0 by Redgate
- Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7)
- ----------------------------------------
- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams
- ----------------------------------------
- Successfully validated 1 migration (execution time 00:00.225s)
- Migrating schema `mingongge` to version "1.0 - Create test table"
- Successfully applied 1 migration to schema `mingongge`, now at version v1.0 (execution time 00:00.356s)
增加數(shù)據(jù)進(jìn)行遷移測(cè)試。還是在sql目錄下創(chuàng)建腳本V1.0.1__add_data.sql
- [root@centos7 ~]# cat flyway-7.11.0/sql/V1.0.1__add_data.sql
- INSERT INTO test_table (id,username, PASSWORD)
- VALUES ('001','test1', '123456');
- INSERT INTO test_table (id,username, PASSWORD)
- VALUES ('002','test2', '123456');
這個(gè)時(shí)候查看狀態(tài)信息,可以發(fā)現(xiàn)如下:
執(zhí)行遷移動(dòng)作
- [root@centos7 ~]# flyway migrate
- Flyway Teams Edition 7.11.0 by Redgate
- Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7)
- ----------------------------------------
- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams
- ----------------------------------------
- Successfully validated 2 migrations (execution time 00:00.042s)
- Current version of schema `mingongge`: 1.0
- Migrating schema `mingongge` to version "1.0.1 - add data"
- Successfully applied 1 migration to schema `mingongge`, now at version v1.0.1 (execution time 00:00.170s)
查看狀態(tài)信息
回滾
先創(chuàng)建一個(gè)回滾腳本,注意命名規(guī)范,這個(gè)會(huì)讓很多人踩坑的。。。。切記?。。?!
- [root@centos7 sql]# cat U1.0.1__delete_data.sql
- DELETE FROM test_table;
注意 :這里的回滾,一定是回滾到已存在的版本號(hào),所以命名時(shí)注意版本號(hào)這塊,如果這里寫(xiě)成1.0,就會(huì)出現(xiàn)報(bào)錯(cuò):
- ERROR: Unable to undo migration to version 1.0.1 as no corresponding undo migration has been found.
執(zhí)行回滾
- [root@centos7 ~]# flyway undo
- Flyway Teams Edition 7.11.0 by Redgate
- Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7)
- ----------------------------------------
- Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams
- ----------------------------------------
- Current version of schema `mingongge`: 1.0.1
- Undoing migration of schema `mingongge` to version 1.0.1 - delete data
- Successfully undid 1 migration to schema `mingongge`, now at version v1.0 (execution time 00:00.256s)
查看狀態(tài)
從上圖中也可以看出V1.0.1這個(gè)版本的腳本現(xiàn)處于Pending狀態(tài)。
其它集成
Flyway也提供了Maven插件,也可以在Maven中使用這個(gè)工具,添加maven依賴(lài),在pom.xml文件中配置連接數(shù)據(jù)的信息。
- <!--引入flyway插件-->
- <plugin>
- <groupId>org.flywaydb</groupId>
- <artifactId>flyway-maven-plugin</artifactId>
- <version>7.11.0</version>
- <configuration>
- <url>jdbc:mysql://localhost:3306/mingongge</url>
- <user>root</user>
- <password>root</password>
- </configuration>
- <dependencies>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.15</version>
- </dependency>
- </dependencies>
- </plugin>
然寫(xiě)同上面的方法一樣寫(xiě)腳本,放在resources/db/migration目錄下。打開(kāi)idea控制臺(tái),輸入mvn flyway:migrate,出現(xiàn)BUILD SUCCESS即執(zhí)行成功。
詳細(xì)的配置及用法請(qǐng)參閱官方手冊(cè):https://flywaydb.org/documentation/usage/maven/
更多更詳細(xì)的使用指南大家有興趣的可以參閱官方文檔:https://flywaydb.org/documentation/