簡(jiǎn)介
Liquibase是一個(gè)用于跟蹤荷憋、管理和應(yīng)用數(shù)據(jù)庫(kù)變化的開源的數(shù)據(jù)庫(kù)重構(gòu)工具衔峰。它將所有數(shù)據(jù)庫(kù)的變化(包括結(jié)構(gòu)和數(shù)據(jù))都保存在XML文件中,便于版本控制。
Liquibase使參與應(yīng)用程序發(fā)布過(guò)程的任何人都可以輕松地:
- 不依賴于特定的數(shù)據(jù)庫(kù),
Liquibase
會(huì)自動(dòng)適配目標(biāo)數(shù)據(jù)庫(kù)進(jìn)行腳本初始化默责,目前支持至少30種主流數(shù)據(jù)庫(kù)。 - 提供數(shù)據(jù)庫(kù)比較功能么库,比較結(jié)果保存在
XML
中傻丝,基于該XML
可以用Liquibase
輕松部署或升級(jí)數(shù)據(jù)庫(kù)。 - 以
XML
記錄/存儲(chǔ)數(shù)據(jù)庫(kù)變化诉儒,其中以author
和id
唯一標(biāo)識(shí)一個(gè)變化(ChangSet
)葡缰,支持?jǐn)?shù)據(jù)庫(kù)變化的合并,因此支持多開發(fā)人員同時(shí)工作。 - 在數(shù)據(jù)庫(kù)中保存數(shù)據(jù)庫(kù)修改歷史(
DatabaseChangeHistory
)泛释,在數(shù)據(jù)庫(kù)升級(jí)時(shí)自動(dòng)跳過(guò)已應(yīng)用的變化(ChangSet
)滤愕。 - 提供變化應(yīng)用的回滾功能,可按時(shí)間怜校、數(shù)量或標(biāo)簽(
tag
)回滾已應(yīng)用的變化间影。通過(guò)這種方式,開發(fā)人員可輕易的還原數(shù)據(jù)庫(kù)在任何時(shí)間點(diǎn)的狀態(tài)茄茁。 - 可生成數(shù)據(jù)庫(kù)修改文檔(
HTML
格式) - 將所有變化(包括結(jié)構(gòu)和數(shù)據(jù))存在
XML
文件中魂贬,便于版本控制的工具 - 支持Springboot,只需要導(dǎo)入依賴裙顽。
- application.yml配置(可選)
- 不配置付燥,默認(rèn)去
resource
下/db/changelog
下找db.changelog-mastert.yaml
文件
Quick Start
- step1:通過(guò)
PowerDesigner
等數(shù)據(jù)庫(kù)建模工具生成數(shù)據(jù)庫(kù)建表語(yǔ)句 - step2:通過(guò)命令行對(duì)數(shù)據(jù)庫(kù)生成(全量)變更集(change set)
- step3:通過(guò)命令行對(duì)數(shù)據(jù)庫(kù)生成(增量)變更集(change set)
- step4:校驗(yàn)數(shù)據(jù)庫(kù)中的變更
SpringBoot 、Maven 集成
添加maven依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<liquibase.version>4.8.0</liquibase.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
<diffChangeLogFile>${basedir}/src/main/resources/config/liquibase/changelog/${project.version}_changelog.xml</diffChangeLogFile>
<outputChangeLogFile>${basedir}/src/main/resources/config/liquibase/changelog/generate_changelog.xml</outputChangeLogFile>
<!-- 是否需要彈出確認(rèn)框 -->
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
<!--輸出文件的編碼 -->
<outputFileEncoding>UTF-8</outputFileEncoding>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>diff</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
application.properties
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/world1?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# 是否啟用
spring.liquibase.enabled=true
# 指定配置文件路徑
spring.liquibase.change-log=classpath:/config/liquibase/master.xml
liquibase.properties
changeLogFile=src/main/resources/config/liquibase/changelog/0.0.1-SNAPSHOT_changelog.xml
zone=UTC
verbose=true
# 覆蓋本地 ddl dml
dropFirst=false
#目標(biāo)數(shù)據(jù)庫(kù)
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/world1?serverTimezone=UTC
username=root
password=123456
#引用數(shù)據(jù)庫(kù)
referenceDriver=com.mysql.cj.jdbc.Driver
referenceUrl=jdbc:mysql://localhost:3306/world?serverTimezone=UTC
referenceUsername=root
referencePassword=123456
添加/生成 liquibase xml
master.xml
liquibase 配置文件入口愈犹,主要用來(lái)引用其他的changelog.xml键科,如下配置文件中的include,當(dāng)然也可以使用includeAll來(lái)引用整個(gè)目錄
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
<!-- 項(xiàng)目自身的sql腳本 -->
<include file="config/liquibase/changelog/0.0.1-SNAPSHOT_changelog.xml" relativeToChangelogFile="false"/>
<!-- <includeAll path="config/liquibase/changelog" relativeToChangelogFile="false"/>-->
</databaseChangeLog>
generate_changelog.xml
使用mvn 插件逆向生成xml漩怎,主要記錄了ddl的變化信息勋颖,比如 如下配置文件中需要?jiǎng)?chuàng)建的表
啟動(dòng)測(cè)試
測(cè)試changeset版本控制
我們?cè)趕pms_tenant_user_attr_config表中修改一個(gè)type字段的長(zhǎng)度,便直接在之前的changeset中修改了字段勋锤,如下圖所示饭玲,然后啟動(dòng)項(xiàng)目看結(jié)果
圖四
報(bào)錯(cuò)信息是我們直接修改了changeset后導(dǎo)致md5值與之前的不匹配(直接在之前的changeset中做了修改)
答疑
- 問:liquibase如何判斷 是同一changeset的?
- 答:
author
和id
唯一標(biāo)識(shí)一個(gè)變化(ChangSet
)
- 問: liquibase是如何進(jìn)行changeset版本控制的叁执?
- 答:
Liquibase
會(huì)對(duì)已經(jīng)執(zhí)行的changelog
的每一個(gè)changeSet
的內(nèi)容進(jìn)行md5
計(jì)算咱枉,生成的值是databasechanglog
表的MD5SUM
字段。
當(dāng)重新啟動(dòng)Liquibase
時(shí)徒恋,會(huì)對(duì)每個(gè)changeSet
進(jìn)行md5
值計(jì)算,與databasechanglog
表中的MD5SUM
字段進(jìn)行對(duì)比欢伏,如果不一致入挣,說(shuō)明changeSet
值已經(jīng)被修改,無(wú)法啟動(dòng)成功硝拧。
plugin-比較數(shù)據(jù)庫(kù)差異
首先使用
liquibase diff
功能前径筏,我們?cè)?code>liquibase.properties中加入?yún)⒖嫉臄?shù)據(jù)庫(kù)world
配置信息,用于差異比較的數(shù)據(jù)庫(kù)障陶,以此數(shù)據(jù)庫(kù)為準(zhǔn)滋恬,生成diff xml
;然后再用liquibase
插件看下生成的diff xml
信息抱究;執(zhí)行完畢后恢氯,查看diff xml
內(nèi)容如下:
圖五
plugin-生成增量的changelog SQL
讀取
diff
生成的差異changeset
,輸出增加更新的SQL
文件
圖六
圖七
總結(jié)
- 首次使用liquibase,可以直接根據(jù)數(shù)據(jù)庫(kù)逆向生成changeSet xml
- 增量使用liquibase勋拟,使用diff命令生成需要增量更新的changeSet xml(也可用 updateSQL 生成可執(zhí)行的SQL語(yǔ)句)