liquibase详解

liquibase是一个用于跟踪、管理和应用数据库变化的开源数据库重构工具。它将所有数据库的变化(包括数据和结构)都保存到XML文件中,便于版本控制。

liquibase特性

  • 不依赖于特定的数据库,目前支持包括Oracle/Sql/Mysql等多种数据库,这样在数据库的部署和升级环节课帮助应用系统支持多数据库。
  • 提供数据库比较功能,比较结果保存在XML中,基于该XML你可用liquibase轻松部署或升级数据库。
  • 以XML存储数据库变化,其中以作者和ID唯一标识一个变化(ChangeSet),支持数据库变化的合并,一次支持多开发人员同时工作。
  • 在数据库总保存数据库修改历使(DatabaseChangeHistory),在数据库升级时自动跳过已应用的变化(ChangeSet)。
  • 提供变化应用的回滚功能,可按时间、数量或标签(tag)回滚已应用的变化。通过这种方式,开发人员可轻易的还原数据库在任何时间点的状态。
  • 可生成数据据修改文档(HTML格式)。
  • 提供数据重构的独立的IDE和Eclipse插件。

使用liquibase管理数据库的变更

使用步骤

  • step1:创建一个数据库变更日志(change log)文件。
  • step2:在变更日志文件内部创建一个变更集(change set)。
  • step3:通过命令行或构建脚本对数据库进行变更集。
  • step4:检验数据库中的变更。

step1:创建change log文件(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">
 
</databaseChangeLog>

step2:添加变更集

<?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>

每个更改集都用"id"属性和"author"属性唯一标识。“author"属性可以最大限度地减少重复的可能性。liquibase将每个更改集视为要应用于数据库的原子更改。通常最好只在更改集中包含一个更改。

step3:运行ChangeSet

liquibase可以使用命令行Ant Maven Spring等工具运行

命令演示

liquibase --driver=com.mysql.jdbc.Driver \
--classpath=/path/to/classes \
--changeLogFile=com/example/db.changelog.xml \
--url="jdbc:mysql://localhost/example" \
--username=user \
--password=asdf \
update

step4:检查数据库

运行完成后数据库中会包含一个名为"department"的表。还创建了另外表:"databasechangelog"和"databasechangeloglock"。databasechangelog表包含已针对数据库运行的所有语句列表。databasechangeloglock表用于确保两台计算机不会同时尝试修改数据库。

常见数据库操作:

添加列

在项目的开始,几乎不可能考虑到数据库中的所有列。而有时候,用户要求新的特性-例如为存储在系统中的信息收集更多的数据,这就要求添加新的列。

使用LiquiBase变更集中的Add Column数据库重构

<!--向数据库的distributor表添加了一个列-->
<changeSet id="4" author="tom">
    <addColumn tableName="distributor">
    <column name="phonenumber" type="varchar(255)"/>
    <addColumn>
</changeSet>

 创建表

<!--向数据库添加一个新表,并定义列、约束和默认值-->
<changeSet id="3" author="betsey">
<createTable tableName="distributor">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="address" type="varchar(255)">
<constraints nullable="true"/>
</column>
<column name="active" type="boolean" defaultValue="1"/>
</createTable>
</changeSet>

操作数据

<!--插入数据-->
<changeSet id="3" author="betsey">
<code type="section" width="100%">
<insert tableName="distributor">
<column name="id" valueNumberic="3"/>
<column name="name" value="Beer Company"/>
</insert>
<insert tableName="distributor">
<column name="id" valueNumberic="4"/>
<column name="name" value="Beer Distributors"/>
</insert>
</changeSet>

猜你喜欢

转载自blog.csdn.net/li_w_ch/article/details/109125209