每次服務(wù)的代碼更新部署,難免會存在數(shù)據(jù)庫結(jié)構(gòu)
的變更以及字典數(shù)據(jù)的添加
,手動執(zhí)行更新腳本
是一個耗時耗力
的工作,而且還會出現(xiàn)遺漏或者其他狀況,SpringBoot
內(nèi)部集成了一個自動執(zhí)行數(shù)據(jù)庫腳本的第三方依賴Flyway
來解決這個繁瑣的問題。
什么是Flyway
官網(wǎng)給出的定義是`Version control for your database.
Robust schema evolution across all your environments.
With ease, pleasure and plain SQL.`(數(shù)據(jù)庫的版本控制,在所有環(huán)境中進行穩(wěn)健的架構(gòu)演變,輕松,愉快和簡單的SQL。)
Flyway
是一款開源的數(shù)據(jù)庫版本管理工具,它更傾向于規(guī)約優(yōu)于配置的方式。
Flyway
可以獨立于應用實現(xiàn)管理并跟蹤數(shù)據(jù)庫變更,支持數(shù)據(jù)庫版本自動升級,并且有一套默認的規(guī)約,不需要復雜的配置,Migrations
可以寫成 SQL 腳本
,也可以寫在 Java 代碼中,不僅支持 Command Line
和 Java API
,還支持 Build 構(gòu)建工具和 Spring Boot
等,同時在分布式環(huán)境下能夠安全可靠地升級數(shù)據(jù)庫
,同時也支持失敗恢復
等。
Flyway運行原理
當我們運行配置使用Flyway
的應用程序時,會自動在配置數(shù)據(jù)源的數(shù)據(jù)庫內(nèi)創(chuàng)建一個名為
flyway_schema_history的表,該表內(nèi)存放了數(shù)據(jù)庫的歷史記錄
信息。
然后通過掃碼應用程序的/reosurces/db/migration
目錄下的歷史版本腳本SQL文件,文件格式為:V?__desc.sql
,如:V1__init-db.sql
,根據(jù)版本號進行排序后,獲取最大的版本號與flyway_schema_history
表內(nèi)執(zhí)行成功的最大版本號進行比對,如果項目內(nèi)版本較高,則自動執(zhí)行腳本文件。
創(chuàng)建項目
通過idea
工具創(chuàng)建SpringBoot
項目,在pom.xml
添加相關(guān)依賴如下所示:
org.springframework.bootspring-boot-starterorg.flywaydbflyway-coreorg.springframework.bootspring-boot-starter-jdbccom.zaxxerHikariCPmysqlmysql-connector-javaruntimeorg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engine
添加數(shù)據(jù)庫配置
在application.yml
配置文件內(nèi)添加數(shù)據(jù)源信息,如下所示:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/flyway
username: root
password: 123456
type: com.zaxxer.hikari.HikariDataSource
添加Flyway版本腳本
腳本比較簡單,大家可以任意添加一些SQL來查看結(jié)構(gòu)或者數(shù)據(jù)變動。
db.migration
目錄是SpringBoot
在整合Flyway
時默認讀取版本腳本的目錄,我們可以在application.yml
配置spring.flyway.locations
參數(shù)進行修改。
測試
當我們啟動項目時,會自動比對腳本的版本,在db.migration
目錄內(nèi)找到V1.1__add_logging.sql
為最高版本,拿著1.1
再去flyway_schema_history
表內(nèi)執(zhí)行成功最大的版本比對,如果低于1.1
則自動執(zhí)行V1.1_add_logging.sql
腳本內(nèi)容,否則跳過。
flyway_schema_history表
每次啟動項目如果存在可更新的腳本信息,執(zhí)行完成后會自動在flyway_schema_history
表內(nèi)添加一條記錄。
installed_rank | version | description | type | script | checksum | installed_by | installed_on | execute_time | success |
---|---|---|---|---|---|---|---|---|---|
1 | 1 | init | SQL | V1__init.sql | 2034194600 | root | 2019-10-23 21:44:36 | 17 | 1 |
2 | 1.1 | add logging | SQL | V1.1_add_logging.sql | 1859098444 | root | 2019-10-23 21:46:50 | 54 | 1 |
敲黑板,劃重點
本章簡單的介紹了Flyway
的基本使用方法,它很強大,功能遠遠不止于此,使用腳本統(tǒng)一自動執(zhí)行可大大減少手動執(zhí)行出現(xiàn)的遺漏、錯誤等。
存在既有道理,為什么不嘗試使用呢?
審核編輯 黃昊宇
-
JAVA
+關(guān)注
關(guān)注
19文章
2975瀏覽量
105168 -
數(shù)據(jù)庫
+關(guān)注
關(guān)注
7文章
3851瀏覽量
64698 -
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14393 -
MySQL
+關(guān)注
關(guān)注
1文章
830瀏覽量
26747
發(fā)布評論請先 登錄
相關(guān)推薦
分布式云化數(shù)據(jù)庫有哪些類型
MySQL數(shù)據(jù)庫的安裝
![MySQL<b class='flag-5'>數(shù)據(jù)庫</b>的安裝](https://file1.elecfans.com/web3/M00/05/E2/wKgZPGeF2XWAe83fAAAW9lhgvGk652.jpg)
云數(shù)據(jù)庫是哪種數(shù)據(jù)庫類型?
SSM與Hibernate的整合使用
數(shù)據(jù)庫數(shù)據(jù)恢復—Mysql數(shù)據(jù)庫表記錄丟失的數(shù)據(jù)恢復流程
![<b class='flag-5'>數(shù)據(jù)庫</b><b class='flag-5'>數(shù)據(jù)</b>恢復—Mysql<b class='flag-5'>數(shù)據(jù)庫</b>表記錄丟失的<b class='flag-5'>數(shù)據(jù)</b>恢復流程](https://file.elecfans.com/web2/M00/7B/26/pYYBAGNzCiiANj77AAH4iOB3xKM259.png)
數(shù)據(jù)庫數(shù)據(jù)恢復—MYSQL數(shù)據(jù)庫ibdata1文件損壞的數(shù)據(jù)恢復案例
數(shù)據(jù)庫數(shù)據(jù)恢復—通過拼接數(shù)據(jù)庫碎片恢復SQLserver數(shù)據(jù)庫
![<b class='flag-5'>數(shù)據(jù)庫</b><b class='flag-5'>數(shù)據(jù)</b>恢復—通過拼接<b class='flag-5'>數(shù)據(jù)庫</b>碎片恢復SQLserver<b class='flag-5'>數(shù)據(jù)庫</b>](https://file1.elecfans.com/web1/M00/F4/07/wKgaoWcjE32AbQdWAAJD_hojvJc119.png)
一文講清什么是分布式云化數(shù)據(jù)庫!
Oracle數(shù)據(jù)恢復—異常斷電后Oracle數(shù)據(jù)庫啟庫報錯的數(shù)據(jù)恢復案例
![Oracle<b class='flag-5'>數(shù)據(jù)</b>恢復—異常斷電后Oracle<b class='flag-5'>數(shù)據(jù)庫</b>啟<b class='flag-5'>庫</b>報錯的<b class='flag-5'>數(shù)據(jù)</b>恢復案例](https://file1.elecfans.com/web2/M00/08/B8/wKgZomb6Ns-AbiICAAFlXAFpKKU086.png)
數(shù)據(jù)庫數(shù)據(jù)恢復—SQL Server數(shù)據(jù)庫出現(xiàn)823錯誤的數(shù)據(jù)恢復案例
![<b class='flag-5'>數(shù)據(jù)庫</b><b class='flag-5'>數(shù)據(jù)</b>恢復—SQL Server<b class='flag-5'>數(shù)據(jù)庫</b>出現(xiàn)823錯誤的<b class='flag-5'>數(shù)據(jù)</b>恢復案例](https://file1.elecfans.com/web2/M00/07/F4/wKgaombs78mANJ1GAAPeSoXHVPE244.png)
啟明信息完成國產(chǎn)化Doris數(shù)據(jù)庫升級替代任務(wù)
數(shù)據(jù)庫數(shù)據(jù)恢復—raid5陣列上層Sql Server數(shù)據(jù)庫數(shù)據(jù)恢復案例
![<b class='flag-5'>數(shù)據(jù)庫</b><b class='flag-5'>數(shù)據(jù)</b>恢復—raid5陣列上層Sql Server<b class='flag-5'>數(shù)據(jù)庫</b><b class='flag-5'>數(shù)據(jù)</b>恢復案例](https://file.elecfans.com/web2/M00/A2/AD/pYYBAGRLbSSAHhFWAAI9vWNRQec919.png)
MongoDB數(shù)據(jù)恢復—MongoDB數(shù)據(jù)庫文件損壞的數(shù)據(jù)恢復案例
![MongoDB<b class='flag-5'>數(shù)據(jù)</b>恢復—MongoDB<b class='flag-5'>數(shù)據(jù)庫</b>文件損壞的<b class='flag-5'>數(shù)據(jù)</b>恢復案例](https://file.elecfans.com/web2/M00/9F/F3/poYBAGQ-Pg6AWHFxAAJ9VHkTNrk678.png)
評論