LiquiBase 进行数据库版本管理。利用编辑好的sql文件或者xml文件进行数据库管理。
- 支持几乎所有主流的数据库,如MySQL, PostgreSQL, Oracle, Sql Server, DB2等;
- 支持多开发者的协作维护;
- 日志文件支持多种格式,如XML, YAML, JSON, SQL等;
- 支持多种运行方式,如命令行、Spring集成、Maven插件、Gradle插件等;
- 支持csv文件导入
注意:并不能适用于带数据转移,不支持存储过程。
系统集成liquibase后,自动在数据库中创建两个表格databasechangelog、databasechangeloglock。存储了数据库更新的版本和一些具体信息。
第一个特定于 LiquiBase 的表称为
databasechangelog
,它跟踪应用到数据库的所有变更 —有助于跟踪谁执行了数据库变更以及原因。第二个特定于 LiquiBase- 的表是
databasechangelock
,标识出具有数据库变更锁的用户。
创建liquibase
步骤:
1.系统加入mysql-connector-java、liquibase
2、配置数据库相关
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<changeLogFile>/src/main/resources/my-changelog-master.xml</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>jdbc:mysql://localhost:3308/test</url>
<username>root</username>
<password>root</password>
</configuration>
<executions>
<execution>
<goals>
<goal>update</goal>
<goal>tag</goal>
<goal>rollbackSQL</goal>
<goal>rollback</goal>
</goals>
</execution>
</executions>
</plugin>
3.配置web.xml
<context-param>
<param-name>liquibase.changelog</param-name>
<param-value>/src/main/resources/my-db.changelog-master.xml</param-value>
</context-param>
<context-param>
<param-name>liquibase.datasource</param-name>
<param-value>java:jboss/datasources/LiquibaseDS</param-value>
</context-param>
<listener>
<listener-class>liquibase.integration.servlet.LiquibaseServletListener</listener-class>
</listener>
4.编写处理数据的my-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-2.0.xsd">
<include file="db.changelog-1.0.xml" relativeToChangelogFile="true" />
<include file="db.changelog-2.0.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
include 中连接的file就是需要处理的xml文件,也可以替换成sql文件。
<include>
的file属性表示要包含的changelog文件的路径,这个文件可以是LiquiBase支持的任意格式,relativeToChangelogFile如果为true,则表示file属性表示的文件路径是相对于根changelog而不是CLASSPATH的,默认为false。5.编写xml文件
<changeSet id="1" author="qiuwushuang">
<createTable tableName="TEST_TABLE">
<column name="ID" type="bigint(20)" autoIncrement="true">
<constraints primaryKey="true" nullable="false" />
</column>
<column name="CODE" type="varchar(20)">
<constraints nullable="false" unique="true" />
</column>
<column name="DESCRIPTION" type="varchar(200)" />
<column name="VALUE" type="varchar(200)" />
</createTable> </changeSet>
注意:
此处如果需要修改数据库,需要添加changeSet 。liquibase会检索到你新添加的changeSet,把添加的语句生成数据库脚本
修改数据库,并且把id author保存到databasechangelog 中,保存你修改信息。
在SQL 文件中,下划线行就是author和id
--liquibase formatted sql --changeset songjiantao:1 CREATE TABLE `ceshi` ( `ID` VARCHAR (200) );
运行liquibase
首先当然是 mvn clean install
- 更新数据库:mvn liquibase:update
- 打版本标签:mvn liquibase:tag
- 回滚到最近的更新版本,或指定的标签版本,或日期,或更新次数:mvn liquibase:rollback -Dliquibase.rollbackCount=1
- 生成sql更新脚本:mvn liquibase:updateSQL
1、添加版本标签:
a、使用命令行:
mvn liquibase:tag -Dliquibase.tag=checkpoint
b、使用配置文件:
<executions> <execution> <phase>process-resources</phase> <configuration> <tag>${project.version}</tag> </configuration> <goals> <goal>update</goal> <goal>tag</goal> </goals> </execution> </executions>
2、可以使用如下命令回滚到某个版本:
mvn liquibase:rollback -Dliquibase.rollbackTag=checkpoint
对应的maven配置为:
<executions> <execution> <phase>process-resources</phase> <configuration> <changeLogFile>src/main/resources/liquiabse/master-changelog.xml</changeLogFile> <driver>${jdbc.driverClassName}</driver> <url>${jdbc.url}</url> <username>${jdbc.username}</username> <password>${jdbc.password}</password> <rollbackTag>1.1</rollbackTag> </configuration> <goals> <goal>update</goal> <goal>rollback</goal> </goals> </execution> </executions>
多数据库下使用
可以在pom文件使用多个execution标签支持多数据库,但是需要注意每个execution一定要定义id标签
<plugin> <groupId>org.liquibase</groupId> <artifactId>liquibase-plugin</artifactId> <version>1.9.5.0</version> <executions> <execution> <phase>process-resources</phase> <configuration> <changeLogFile>src/main/resources/db.changelog.xml</changeLogFile> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/charm</url> <username>***</username> <password>***</password> </configuration> <goals> <goal>update</goal> </goals> </execution> <execution> <phase>process-resources</phase> <configuration> <changeLogFile>src/main/resources/db.changelog.xml</changeLogFile> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/charm2</url> <username>***</username> <password>***</password> </configuration> <goals> <goal>update</goal> </goals> </execution> </executions> </plugin>