Liquibase-數(shù)據(jù)庫腳本版本管理控制

簡介

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ù)庫變化,其中以authorid唯一標識一個變化(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的?
  • 答:authorid唯一標識一個變化(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語句)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容