官网:http://www.liquibase.org/
Liquibase是一个用于跟踪、管理和应用数据库变化的开源的数据库重构工具。它将所有数据库的变化(包括结构和数据)都保存在XML文件中,便于版本控制。
Liquibase具备如下特性:
- 不依赖于特定的数据库,目前支持包括Oracle/Sql Server/DB2/MySql/Sybase/PostgreSQL/Caché等12种数据库,这样在数据库的部署和升级环节可帮助应用系统支持多数据库。
- 提供数据库比较功能,比较结果保存在XML中,基于该XML你可用Liquibase轻松部署或升级数据库。
- 以XML存储数据库变化,其中以作者和ID唯一标识一个变化(ChangSet),支持数据库变化的合并,因此支持多开发人员同时工作。
- 在数据库中保存数据库修改历史(DatabaseChangeHistory),在数据库升级时自动跳过已应用的变化(ChangSet)。
- 提供变化应用的回滚功能,可按时间、数量或标签(tag)回滚已应用的变化。通过这种方式,开发人员可轻易的还原数据库在任何时间点的状态。
- 可生成数据库修改文档(HTML格式)
- 提供数据重构的独立的IDE和Eclipse插件。
LiquiBase的spring boot 入门配置
pom.xml
<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
</dependency>
增加 LiquibaseConfig.java
package com.aop8.config;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import liquibase.integration.spring.SpringLiquibase;
@Configuration
public class LiquibaseConfig {
@Bean
public SpringLiquibase liquibase(DataSource dataSource) {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:config/liquibase/master.xml");
liquibase.setContexts("development,test,production");
liquibase.setShouldRun(true);
return liquibase;
}
}
增加 master.xml
路径:src/main/resources 目录下 config/liquibase/master.xml
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="classpath:config/liquibase/changelog/201712022057_add_entity_Base.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
201712022057_add_entity_Base.xml
路径:
src/main/resources 目录下
config/liquibase/changelog/201712022057_add_entity_Base.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">
<property name="now" value="now()" dbms="mysql"/>
<changeSet id="20171202205" author="WATER">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="tb_member"/>
</not>
</preConditions>
<createTable tableName="tb_member">
<column name="id" type="bigint" autoIncrement="true" remarks="主键" >
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="mobile" type="varchar(50)" remarks="手机号" >
<constraints nullable="false" />
</column>
<column name="real_name" type="varchar(50)" remarks="姓名" >
<constraints nullable="false" />
</column>
<column name="introduce" type="varchar(255)" remarks="说明" >
<constraints nullable="false" />
</column>
<column name="status" type="varchar(10)" remarks="账号状态" />
<column name="created_by" type="varchar(50)" remarks="创建人" >
<constraints nullable="false"/>
</column>
<column name="created_date" type="timestamp" defaultValueDate="${now}" remarks="创建时间" >
<constraints nullable="false"/>
</column>
<column name="last_modified_by" type="varchar(50)"/>
<column name="last_modified_date" type="timestamp"/>
</createTable>
</changeSet>
</databaseChangeLog>