简介
Liquibase社区版是一个开源项目,可以帮助开发人员快速管理数据库架构更改。跟踪、版本化和部署数据库变更。目前基于Maven插件将Liquibase集成到项目中,可以使数据库可持续开发部署管理。
Liquibase配置文件
通过Maven的profile,不同环境的数据库使用不同的Liquibase配置文件( local,dev,stg,prod),如下图所示
changeLogFile=src/main/resources/db/changelog/db.changelog-master.xml
driver=org.postgresql.Driver
defaultSchemaName=minefocus
url=jdbc:postgresql://localhost:5432/demo
username=postgres password=123456
outputChangeLogFile=/var/data/liquibase/changelog/db.changelog-export.xml
diffTypes=tables,columns,views,primaryKeys,indexes,foreignKeys,sequences,data dataDir=/var/data/liquibase/changelog/csv
outputDirectory=target/docs
Liquibase目录结构
Liquibase的changelog文件放在resources/db/changelog目录下,如下图所示,我们只需要追加更改这些文件来更新数据库。db.changelog-master.xml是主文件,只用于包含文件,一般不做更改。根据修改类型:增加,修改,删除表对象和数据导入分别在各自文件添加。数据文件以csv格式放到csv目录下。
如果多次release,可以通过文件夹加版本号增加新的changelog文件。如:
master.xml
--release1.0.0
----release.xml
----add.xml
----update.xml
----remove.xml
----data.xml
----csv
--release1.0.1
----release.xml
----add.xml
----update.xml
----remove.xml
----data.xml
----csv
中小项目如果对changgelog都非常熟悉,可以用一个xml定义changelog。
大型项目可以按照模块划分目录结构,如
master.xml
--release1.0.0
----release.xml
----user.xml
----user.csv
----company.xml
--release1.0.1
----release.xml
----user.xml
----user.csv
----company.xml
以上仅供参考,根据项目情况可以自定义目录结构。
数据库changelog
1. changelog
有xml、yaml,json结构,根据需要选择其中一种,xml易读;yaml简洁;json介于前两者之间。
Tag |
Description |
preConditions |
Pre-conditions required to execute the changelog. Read More |
property |
Value to set property to, if not set by another means. Read More |
changeSet |
The changesets to execute. Read More |
include |
Additional files containing changesets to execute. Read More |
context |
Context to be appended (using AND) to all changesets since 3.5 |
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext