自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

挺帶勁!這款開(kāi)源數(shù)據(jù)庫(kù)遷移工具超牛

運(yùn)維 數(shù)據(jù)庫(kù)運(yùn)維
有一款好的遷移工具是可以大大提高工作效率,還能很大程度上避免手動(dòng)操作帶來(lái)的失誤或數(shù)據(jù)損壞、丟失的風(fēng)險(xiǎn)。

 [[409779]]

遷移數(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)

  1. 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

  1. sh -c 'echo "docker run --rm flyway/flyway:7.11.0 $*" > /usr/local/bin/flyway && chmod +x /usr/local/bin/flyway' 

目錄結(jié)構(gòu)

  1. [root@centos7 ~]# tree -L 1 ./flyway-7.11.0 
  2. ./flyway-7.11.0 
  3. ├── conf       #配置文件 
  4. ├── drivers    #JDBD驅(qū)動(dòng)程序 
  5. ├── flyway     #macOS/Linux 可執(zhí)行文件 
  6. ├── flyway.cmd #Windows 可執(zhí)行文件 
  7. ├── jars       #基于 Java 的遷移(作為 jars) 
  8. ├── jre 
  9. ├── lib 
  10. ├── licenses 
  11. ├── README.txt 
  12. └── sql       #SQL 遷移 
  13.  
  14. 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ò)的遷移

命令行使用

  1. > flyway [options] command 

flyway主要基于6種基本命令

  1. migrate  #遷移數(shù)據(jù)庫(kù) 
  2. clean    #清除所有配置 
  3. info     #打印遷移相關(guān)的詳細(xì)信息與狀態(tài)信息 
  4. validate #驗(yàn)證遷移的可用性 
  5. baseline #為現(xiàn)有數(shù)據(jù)庫(kù)創(chuàng)建基準(zhǔn)版本,高于基準(zhǔn)版本的才會(huì)被執(zhí)行遷移動(dòng)作 
  6. repair   #修改 schema history 表 

修改配置文件

  1. [root@centos7 ~]# cd ./flyway-7.11.0/conf/ 
  2. [root@centos7 conf]# ll 
  3. total 24 
  4. -rw-r--r-- 1 root root 22943 Jul  1  2021 flyway.conf 
  5. [root@centos7 conf]# vim flyway.conf 
  6. flyway.url=jdbc:mysql://localhost:3306/mingongge?useUnicode=true 
  7. flyway.user=root 
  8. 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ǔ)句。

  1. [root@centos7 sql]# pwd 
  2. /root/flyway-7.11.0/sql 
  3. [root@centos7 sql]# cat V1.0__Create_test_table.sql 
  4. CREATE TABLE `test_table` 
  5.   `id`          bigint(10)   NOT NULL, 
  6.   `username`    varchar(64)  DEFAULT NULL, 
  7.   `password`    varchar(64)  DEFAULT NULL, 
  8.   PRIMARY KEY (`id`) 
  9. ) ENGINE = InnoDB 
  10.   DEFAULT CHARSET = utf8 COMMENT ='測(cè)試表'

開(kāi)始遷移

  1. [root@centos7 ~]# flyway migrate 
  2. Flyway Teams Edition 7.11.0 by Redgate 
  3. Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7) 
  4. ---------------------------------------- 
  5. Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams 
  6. ---------------------------------------- 
  7. Successfully validated 0 migrations (execution time 00:00.028s) 
  8. WARNING: No migrations found. Are your locations set up correctly? 
  9. 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è)唄。

  1. [root@centos7 ~]# flyway baseline 
  2. Flyway Teams Edition 7.11.0 by Redgate 
  3. Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7) 
  4. ---------------------------------------- 
  5. Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams 
  6. ---------------------------------------- 
  7. Creating Schema History table `mingongge`.`flyway_schema_history` with baseline ... 
  8. Successfully baselined schema with version: 1 

再次執(zhí)行

  1. [root@centos7 sql]# flyway migrate 
  2. Flyway Teams Edition 7.11.0 by Redgate 
  3. Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7) 
  4. ---------------------------------------- 
  5. Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams 
  6. ---------------------------------------- 
  7. Successfully validated 1 migration (execution time 00:00.225s) 
  8. Migrating schema `mingongge` to version "1.0 - Create test table" 
  9. 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

  1. [root@centos7 ~]# cat flyway-7.11.0/sql/V1.0.1__add_data.sql  
  2. INSERT INTO test_table (id,username, PASSWORD) 
  3. VALUES ('001','test1''123456'); 
  4. INSERT INTO test_table (id,username, PASSWORD) 
  5. VALUES ('002','test2''123456'); 

這個(gè)時(shí)候查看狀態(tài)信息,可以發(fā)現(xiàn)如下: 

 

 

 

執(zhí)行遷移動(dòng)作

  1. [root@centos7 ~]# flyway migrate 
  2. Flyway Teams Edition 7.11.0 by Redgate 
  3. Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7) 
  4. ---------------------------------------- 
  5. Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams 
  6. ---------------------------------------- 
  7. Successfully validated 2 migrations (execution time 00:00.042s) 
  8. Current version of schema `mingongge`: 1.0 
  9. Migrating schema `mingongge` to version "1.0.1 - add data" 
  10. 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ì)讓很多人踩坑的。。。。切記?。。?!

  1. [root@centos7 sql]# cat U1.0.1__delete_data.sql  
  2. DELETE FROM test_table; 

注意 :這里的回滾,一定是回滾到已存在的版本號(hào),所以命名時(shí)注意版本號(hào)這塊,如果這里寫(xiě)成1.0,就會(huì)出現(xiàn)報(bào)錯(cuò):

  1. ERROR: Unable to undo migration to version 1.0.1 as no corresponding undo migration has been found. 

執(zhí)行回滾

  1. [root@centos7 ~]# flyway undo 
  2. Flyway Teams Edition 7.11.0 by Redgate 
  3. Database: jdbc:mysql://localhost:3306/mingongge (MySQL 5.7) 
  4. ---------------------------------------- 
  5. Flyway Teams features are enabled by default for the next 27 days. Learn more at https://flywaydb.org/?ref=v7.11.0_teams 
  6. ---------------------------------------- 
  7. Current version of schema `mingongge`: 1.0.1 
  8. Undoing migration of schema `mingongge` to version 1.0.1 - delete data 
  9. 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ù)的信息。

  1. <!--引入flyway插件--> 
  2. <plugin> 
  3.     <groupId>org.flywaydb</groupId> 
  4.     <artifactId>flyway-maven-plugin</artifactId> 
  5.     <version>7.11.0</version> 
  6.     <configuration> 
  7.         <url>jdbc:mysql://localhost:3306/mingongge</url> 
  8.         <user>root</user> 
  9.         <password>root</password> 
  10.     </configuration> 
  11.     <dependencies> 
  12.         <dependency> 
  13.             <groupId>mysql</groupId> 
  14.             <artifactId>mysql-connector-java</artifactId> 
  15.             <version>8.0.15</version> 
  16.         </dependency> 
  17.     </dependencies> 
  18. </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/

 

責(zé)任編輯:張燕妮 來(lái)源: 民工哥技術(shù)之路
相關(guān)推薦

2022-02-25 15:06:53

PowerDesig開(kāi)源工具

2012-12-19 13:06:31

MySQL

2022-06-17 10:27:25

Navicat開(kāi)源數(shù)據(jù)庫(kù)管理工具

2019-01-25 19:19:28

數(shù)據(jù)工具互聯(lián)網(wǎng)

2023-10-19 09:00:00

數(shù)據(jù)庫(kù)GitOps

2023-04-18 18:22:31

開(kāi)源工具數(shù)據(jù)庫(kù)

2022-07-27 08:12:44

SchemaHero云原生

2011-09-23 09:09:38

數(shù)據(jù)庫(kù)遷移

2020-08-13 07:42:15

數(shù)據(jù)庫(kù)Flyway代碼

2021-10-27 10:07:59

GitHub代碼開(kāi)發(fā)者

2021-01-10 15:29:53

開(kāi)源數(shù)據(jù)庫(kù)數(shù)據(jù)庫(kù)

2020-07-17 08:20:27

數(shù)據(jù)庫(kù)開(kāi)源技術(shù)

2022-05-23 09:03:35

Heighliner開(kāi)發(fā)者工具

2021-11-15 08:24:17

數(shù)據(jù)庫(kù)database同步工具

2021-05-17 08:00:00

數(shù)據(jù)庫(kù)工具Flyway

2009-03-19 09:44:07

SQL Server數(shù)據(jù)庫(kù)遷移數(shù)據(jù)庫(kù)

2011-05-11 10:26:36

MySQL數(shù)據(jù)庫(kù)無(wú)縫遷移

2019-08-13 15:52:34

數(shù)據(jù)庫(kù)同步遷移

2011-04-29 14:30:23

2009-06-18 16:29:00

牛新莊數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)