liquibase数据库工具使用

2019-01-30  本文已影响0人  浪漫茶

liquibase安装

运行

# liquibbase --help
http://www.liquibase.org

[[http://www.liquibase.org/documentation/command_line.html | liquibase官网命令行文档说明]]

liquibase changelog格式

<?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.4.xsd">

    <include file="./changelog/0100_20160217_01_init.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/0100_20160219_01_addtable.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_CONFIG.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_DICT.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_LABEL.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>

sql文件 在SQL文件里,必须加上头标签如下所示

--liquibase formatted sql
--changeset JasonYe:Release0100-1
update table1 set column1=333;
--rollback delete from table1 where column1=333;

比较数据库差异

liquibase \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
diff \
--referenceUrl=jdbc:mysql://database2.com:3306/schema \
--referenceUsername=username \
--referencePassword=password

比较数据库差异,生成changelog

liquibase \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
--changeLogFile=changelog.xml 
diffChangeLog \
--referenceUrl=jdbc:mysql://database2.com:3306/schema \
--referenceUsername=username \
--referencePassword=password

根据changelog更新数据库命令updateSQL,生成sql脚本

liquibase \
--driver=com.mysql.jdbc.Driver \
--classpath=mysql-connector-java-bin.jar \
--changeLogFile=changelog.xml \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
--outputFIle=./xxx.sql \
updateSQL

根据changelog和数据库信息生成dbDoc

liquibase \
--driver=com.mysql.jdbc.Driver \
--classpath=mysql-connector-java-bin.jar \
--changeLogFile=changelog.xml \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
dbDoc <your_outputDir_path>

回滚

这个命令的前提是你通过liquibase tag命令在数据库中打上tag。

liquibase \
--driver=com.mysql.jdbc.Driver \
--classpath=mysql-connector-java-bin.jar \
--changeLogFile=changelog.xml \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
rollback <tag比如1.0.0>

liquibase \
--driver=com.mysql.jdbc.Driver \
--classpath=mysql-connector-java-bin.jar \
--changeLogFile=changelog.xml \
--url=jdbc:mysql://database1.com:3306/schema \
--username=username \
--password=password \
rollbackToDate <date,比如2019-01-30T09:55:37> 

命令行参数设置

如果觉得上面关于数据库的配置比较麻烦,可以在执行命令的目录下创建liquibase.properties,文件内容比如:

url: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false
username: 1234
password: Abc.1234
referenceUrl: jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false
referenceUsername: root
referencePassword: 123456

然后在改目录下直接运行命令
liquibase --changeLogFile=./xx.xml diff

也可以通过--defaultFile参数来指定参数配置文件。比如
liquibase --changeLogFile=./xx.xml --defaultFile=./myliquibase.properties diff

上一篇 下一篇

猜你喜欢

热点阅读