簡介
Liquibase是一個用于跟蹤、管理和應(yīng)用數(shù)據(jù)庫變化的開源的數(shù)據(jù)庫重構(gòu)工具。它將所有數(shù)據(jù)庫的變化(包括結(jié)構(gòu)和數(shù)據(jù))都保存在XML文件中,便于版本控制。
Liquibase使參與應(yīng)用程序發(fā)布過程的任何人都可以輕松地:
- 不依賴于特定的數(shù)據(jù)庫,
Liquibase會自動適配目標數(shù)據(jù)庫進行腳本初始化,目前支持至少30種主流數(shù)據(jù)庫。 - 提供數(shù)據(jù)庫比較功能,比較結(jié)果保存在
XML中,基于該XML可以用Liquibase輕松部署或升級數(shù)據(jù)庫。 - 以
XML記錄/存儲數(shù)據(jù)庫變化,其中以author和id唯一標識一個變化(ChangSet),支持數(shù)據(jù)庫變化的合并,因此支持多開發(fā)人員同時工作。 - 在數(shù)據(jù)庫中保存數(shù)據(jù)庫修改歷史(
DatabaseChangeHistory),在數(shù)據(jù)庫升級時自動跳過已應(yīng)用的變化(ChangSet)。 - 提供變化應(yīng)用的回滾功能,可按時間、數(shù)量或標簽(
tag)回滾已應(yīng)用的變化。通過這種方式,開發(fā)人員可輕易的還原數(shù)據(jù)庫在任何時間點的狀態(tài)。 - 可生成數(shù)據(jù)庫修改文檔(
HTML格式) - 將所有變化(包括結(jié)構(gòu)和數(shù)據(jù))存在
XML文件中,便于版本控制的工具 - 支持Springboot,只需要導入依賴。
- application.yml配置(可選)
- 不配置,默認去
resource下/db/changelog下找db.changelog-mastert.yaml文件
Quick Start
- step1:通過
PowerDesigner等數(shù)據(jù)庫建模工具生成數(shù)據(jù)庫建表語句 - step2:通過命令行對數(shù)據(jù)庫生成(全量)變更集(change set)
- step3:通過命令行對數(shù)據(jù)庫生成(增量)變更集(change set)
- step4:校驗數(shù)據(jù)庫中的變更

圖一
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>
<!-- 是否需要彈出確認框 -->
<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
#目標數(shù)據(jù)庫
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/world1?serverTimezone=UTC
username=root
password=123456
#引用數(shù)據(jù)庫
referenceDriver=com.mysql.cj.jdbc.Driver
referenceUrl=jdbc:mysql://localhost:3306/world?serverTimezone=UTC
referenceUsername=root
referencePassword=123456
添加/生成 liquibase xml
master.xml
liquibase 配置文件入口,主要用來引用其他的changelog.xml,如下配置文件中的include,當然也可以使用includeAll來引用整個目錄
<?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">
<!-- 項目自身的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的變化信息,比如 如下配置文件中需要創(chuàng)建的表

圖二
啟動測試

圖三
測試changeset版本控制
我們在spms_tenant_user_attr_config表中修改一個type字段的長度,便直接在之前的changeset中修改了字段,如下圖所示,然后啟動項目看結(jié)果
圖四
報錯信息是我們直接修改了changeset后導致md5值與之前的不匹配(直接在之前的changeset中做了修改)
答疑
- 問:liquibase如何判斷 是同一changeset的?
- 答:
author和id唯一標識一個變化(ChangSet)
- 問: liquibase是如何進行changeset版本控制的?
- 答:
Liquibase會對已經(jīng)執(zhí)行的changelog的每一個changeSet的內(nèi)容進行md5計算,生成的值是databasechanglog表的MD5SUM字段。
當重新啟動Liquibase時,會對每個changeSet進行md5值計算,與databasechanglog表中的MD5SUM字段進行對比,如果不一致,說明changeSet值已經(jīng)被修改,無法啟動成功。
plugin-比較數(shù)據(jù)庫差異
首先使用
liquibase diff功能前,我們在liquibase.properties中加入?yún)⒖嫉臄?shù)據(jù)庫world配置信息,用于差異比較的數(shù)據(jù)庫,以此數(shù)據(jù)庫為準,生成diff xml;然后再用liquibase插件看下生成的diff xml信息;執(zhí)行完畢后,查看diff xml內(nèi)容如下:
圖五
plugin-生成增量的changelog SQL
讀取
diff生成的差異changeset,輸出增加更新的SQL文件
圖六
圖七
總結(jié)
- 首次使用liquibase,可以直接根據(jù)數(shù)據(jù)庫逆向生成changeSet xml
- 增量使用liquibase,使用diff命令生成需要增量更新的changeSet xml(也可用 updateSQL 生成可執(zhí)行的SQL語句)



