Вы находитесь на странице: 1из 2

Liquibase

http://www.liquibase.org/

Installation
Download Liquibase from http://www.liquibase.org/download and extract the archive file.

Usage
You can run from the command line with liquibase or liquibase.bat in the extracted folder, or simply run java -jar liquibase.jar.

Generate The Very First Change Log


java -jar liquibase.jar --changeLogFile=changelog.xml generateChangeLog

Generate Data Change Log


java -jar liquibase.jar --changeLogFile=changelog.data.xml --diffTypes="data" generateChangeLog

liquibase.properties
When running from the command line, you can put the required properties in ./liquibase.properties . Below is an example:

#liquibase.properties
driver: oracle.jdbc.OracleDriver
classpath: jdbcdriver.jar
url: jdbc:oracle:thin:@localhost:1521:oracle
username: scott
password: tiger

Integrate Liquibase with Spring


Add below code snippets in your pom.xml.

<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>2.0.1</version>
</dependency>

Setup below in the setting of Spring and it will automatically execute update when you initial Spring context.

<bean id="liquibase" class="liquibase.integration.spring.SpringLiquibase">


<property name="dataSource" ref="jpaDataSource" />
<property name="changeLog"
value="classpath:com/meshinnovation/gtcompany/db/changelog/db.changelog-master.xml" />
</bean>

Integrate Liquibase with Maven


Add below code snippets in your pom.xml.

<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</build>

And liquibase.properties in you src/main/resources:

changeLogFile=com/meshinnovation/gtcompany/db/changelog/changelog-master.xml
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/gt_crm?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
username=gt_crm
password=9t_crm
verbose=true
dropFirst=true

Then you can run update with liquibase:update.

Best Practice
ref: http://www.liquibase.org/bestpractices

架構

在 src/main/resources 中建立相關 package ,如:com.meshinnovation.gtcompany.db.changelog ,將 changelog 統一放置其中。


建立 changelog-master.xml 如下。這個 changelog 可以保證 changelog 被執行的順序。並且是一個可以被統一引用的檔案(例如在 Spring 設定或
Maven 設定,需指定 changelog 路徑時可以使用)。

<?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="com/meshinnovation/gtcompany/db/changelog/changelog-initial-20110310.xml" />
<include file="com/meshinnovation/gtcompany/db/changelog/changelog-initial-data-20110310.xml" />
</databaseChangeLog>

為避免衝突, changeset id 的命名方式如下:changelog-YYYYMMDD-USERNAME-SEQUENCE.xml,例如 changelog-20110310-reder-1.xml。

使用方式

1. 撰寫 changelog.xml ,進行你想要做的 DB 變更。建議撰寫 rollback ,在 commit 之前都可以方便進行 rollback 。


2. 進行相關程式碼修改。
3. 進行 integration test 或起始 web container 。兩個動作都會起始 Spring ,所以會執行 SpringLiquibase ,進行相關更新。
4. 送交相關修改的程式碼以及 changelog.xml 。

Known Issues
MySQL enum 的 defaultValue 問題
<column defaultValue="private" name="share" type="enum('public','private')"/>

會有問題,

<column defaultValue="'private'" name="share" type="enum('public','private')"/>

才是對的。(差一組 '')

Вам также может понравиться