Liquibase操作指南

LiquiBase是一个用于数据库重构和迁移的开源工具,通过日志文件的形式记录数据库的变更,然后执行日志文件中的修改,将数据库更新或回滚到一致的状态。LiquiBase的主要特点有:
支持几乎所有主流的数据库,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
支持多开发者的协作维护;
日志文件支持多种格式,如XML, YAML, JSON, SQL等;
支持多种运行方式,如命令行、Spring集成、Maven插件、Gradle插件等;
安装liquibase

wget https://github.com/liquibase/liquibase/releases/download/liquibase-parent-3.6.2/liquibase-3.6.2-bin.tar.gz
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.47.tar.gz
tar -zxvf  liquibase-3.6.2-bin.tar.gz -C ./liquibase
tar -zxvf mysql-connector-java-5.1.47.tar.gz -C ./liquibase/lib
export PATH=./liquibase:$PATH
mkdir -p changelogpath
cat db.changelog.xml
<?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.1.xsd">
    <changeSet id="1" author="bob">
        <createTable tableName="department">
            <column name="id" type="int">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="active" type="boolean" defaultValueBoolean="true"/>
        </createTable>
    </changeSet>
</databaseChangeLog>


命令行式执行:
字符集问题"jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8

./liquibase --driver=com.mysql.jdbc.Driver \
     --classpath=/root/liquibase/lib/mysql-connector-java-5.1.47.jar \
     --changeLogFile=/root/liquibase/changelogpath/db.changelog-master.xml \
     --url="jdbc:mysql://127.0.0.1:3306/test" \
     --username=root \
     --password=gj1234 \
     --contexts=QA \
     update \
     -Dschema.name=test

回滚命令:

./liquibase --changeLogFile=D:/Tool/liquibase-3.5.3-bin/Release0003.sql \
 --driver=com.mysql.jdbc.Driver \
 --classpath=/root/liquibase/lib/mysql-connector-java-5.1.47.jar \
 --url="jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=UTF-8" \
 --username=test \
 --password=test \
 rollbackToDate 2018-01-11 16:52:32

会在库中产生两个表
databasechangelog:保存变更记录
databasechangeloglock:保存锁,只允许一个执行。完整解决方案:
changelog 
        db.changelog-master.xml
        db.changelog-1.0.xml
        db.changelog-1.1.xml
        db.changelog-2.0.xml
db.changelog-master.xml

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

  <include file="com/example/db/changelog/db.changelog-1.0.xml"/> 
  <include file="com/example/db/changelog/db.changelog-1.1.xml"/> 
  <include file="com/example/db/changelog/db.changelog-2.0.xml"/> 
</databaseChangeLog> 
db.changelog-1.0.xml
<?xml version="1.0" encoding="UTF-8"?> 
<databaseChangeLog 
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.9" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.9
                      http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd"> 
  <changeSet author="authorName" id="changelog-1.0">
    <createTable tableName="TablesAndTables">
      <column name="COLUMN1" type="TEXT">
        <constraints nullable="true" primaryKey="false" unique="false"/>
      </column>
    </createTable>
  </changeSet>
</databaseChangeLog> 


The master.xml includes the changelog for the releases in the correct order.
The db.changelog-master.xml is the changelog you pass to all Liquibase calls.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值