liquibase安装
- [liquibase下载](https://download.liquibase.org/ liquibase)
- 解压
- 下载slf4j-api-1.7.25.jar,logback-core-1.2.3.jar,logback-classic-1.2.3.jar,放到解压包的lib/目录下
- 配置环境变量LIQUIBASE_HOME=<解压包目录>
- 把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
网友评论