关于changeset具体的配置参见文档 change type.
Liquibase是一个用于跟踪、管理数据库变化的开源数据库重构工具。它将数据库的所有变化(包括结构和数据)都保存在XML文件中,便于版本控制。Liquibase不依赖特定数据库,支持常见的12种数据库,包括:Oracle,MySQL, DB2,SQL Server,PostgreSQL,h2等。
Spring boot中使用LiquiBase
Spring boot内置了对Liquibase的支持,只需要在项目中引入Liquibase依赖并进行配置即可。添加依赖
org.liquibase
liquibase-core
3.5.3
添加配置
配置Liquibase可以直接在application.properties中指定属性,也可以使用Java code来指定属性。
-application.properties配置方式
liquibase.change-log=classpath:config/liquibase/master.xml //存储变化的xml文件的位置
liquibase.user=sa //访问数据库的用户名
liquibase.password= //访问数据库的密码
liquibase.url=jdbc:h2:file:~/.h2/testdb //访问数据库的连接地址
liquibase.enabled=true //启用liquibase,Spring boot 2.0之后使用spring.liquibase.enabled
liquibase.drop-first=false //默认为false,如果设置为true,liquibase将首先删除所有数据库对象的所有连接的用户。
-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;
}
}配置DB change
DB配置一般分为master配置和细节配置,master中使用include引入细节配置文件。
-添加master.xml
该文件被存放在src/main/resources 目录下 config/liquibase/master.xml。
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/dbchangeloghttp://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
relativeToChangelogFile="false"/>
-添加子配置文件201712022057_add_entity_Base.xml
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/dbchangeloghttp://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
通过上面的示例,我们已经知道在spring boot中怎么使用liquibase。由上可知,databaseChangeLog中会包含多个changeSet,Liquibase会在一个transaction中执行一个changeSet。执行上面的配置文件,会在DB中生成数据表tb_member,同时会生成表databasechangelog和databasechangeloglock。前者记录database上执行的所有statements,后者用于确保两台机器不会同时修改database。
Liquibase执行时会检查表databasechangelog,从而只执行没有执行过的changeSet。
Liquibase构件讲解
databasechangelog中包含元素include,property,changeSet,preConditions。
-preConditions
preConditions作用于databaseChangelog或changeSet来控制执行。changelog level的precoditions应用于所有的changeSet,包含include进来的changeSet。preConditions条件满足,按顺序执行其中的changeSet和include;preConditions条件不满足,失败退出并解释失败的原因。
下面是使用preConditions的示例。只有database type为oracle且username为SYSTEM时才执行该changelog。同时,只有oldtable中没有数据时才能drop table。
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.8"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.8http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.8.xsd">
select count(*) from oldtable
Comments should go after preCondition. If they are before then liquibase
usually gives error.
下面是常见的判断结果:
当出现上面的结果时,可以进行一些处理。
当结果为onFail或onError时,可执行下面操作:
当结果为onUpdateSQL时,可执行下面操作:
条件之间可以使用AND/OR/NOT进行拼接来构建复杂的环境,默认为and。下面为示例。
可用的precoditions包括下面item:
、、、、、、、、、、等。
-property
Liquibase允许动态替换changelog中的参数,参数一般使用${}表示。参数值的搜索顺序:a. Liquibase runner的参数; b.JVM系统参数; c. changelog中提供的参数。
property具有下面的属性,设置context或dbms,参数只会在这些context或dbms中使用。
下面是property的示例。
-changeSet
changeSet将database changes组合在一起,使用id,author以及changelog file path来唯一识别。
Liquibase顺序执行changelog中的changeSet,根据表databasechangelog判断id/author/filepath是否已经执行过,若执行过则跳过该changeSet,除非指定runAlways为true. 当changeset执行完,在表databasechangelog插入一行来记录id/author/filepath以及changeset的MD5sum值。
Liquibase通常在一个transaction中执行changeSet。
下面是changeSet具有的属性。
changeSet中可以包含如下的sub-tags。
其中,rollback tag用来描述怎么rollback change,可以使用SQL、change tags或之前changeSet的reference。
// Use SQL
drop table testTable
// Use change tag
// Use change set
Liquibase执行changeSet时会计算check sum并保存在DB中。当check sum不匹配时,说明有人修改了changeSet,Liquibase将会报错并退出。若想忽略该错误,可将id/author/filepath对应的check sum设置为null,这样,下次执行时会生成新的check sum。设置runOnChange为true,当check sum不同时都会重新执行。
-include
用来在root change log file中引入其他的change log file。示例如下。
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.9http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.9.xsd">
include具有如下属性。
include目前具有重复引用和循环引用的问题。重复引用因为不会执行已经执行过的changeSet而自动解决。
详情参见文章include。
-contexts
Liquibase使用contexts作为tag来限制changeSet是否执行,可将changeSet进行分组。若不设置context,该changeSet将总是执行。context可以具有任意名称且大小写不敏感。
下面是使用context的示例。
可以使用and,or,!以及()来组合context,优先级顺序! > and > or。context也可以用,分隔,优先级比较高。test, qa and master等价于(test) or (qa and master)。
-changes
Liquibase中提供了很多可以作用于database的changes。Table/View
包括create, drop, rename table/view/column.index
包括create,drop index/procedure/primary key/foreign key/unique constraint/not null constraint/default valueinsert/delete
下面hi创建table的示例。
remarks="A String"
schemaName="public"
tableName="person"
tablespace="A String">
详情参见文章changes .
执行Liquibase
Liquibase可以on demand执行,也可以自动执行。On demand执行的方式包括command line, Ant, Maven。自动执行的方式包括servlet listener, spring listener。更多信息参见文章Running。
更多关于Liquibase的资料参见Document . Special care exist project , Log trim