tezu memo blog

日々行った作業をメモしていきます

データベースをDiffするツール liquibase

設計書の突き合せが面倒なので探していたら良いツールがありました

Liquibase | Database Refactoring | Liquibase
 

ダウンロード

現時点の最新2.0.5を取得
http://github.com/downloads/liquibase/liquibase/liquibase-2.0.5-bin.zip

今回はAntを使って実行しています
こちらからDL出来ます → Apache Ant - Binary Distributions
 

インストール

%ANT_HOME%\lib下にliquibase.jarとJDBCドライバをコピー(PostgreSQLを使ってます)

$ ls -l
-rwx------+ 1 Administrators mkpasswd    920 Apr  3 13:59 build.xml
-rwx------+ 1 Administrators mkpasswd 926426 May  2  2012 liquibase.jar
-rwx------+ 1 Administrators mkpasswd 579785 Apr  2 10:16 postgresql-9.2-1002.jdbc4.jar

 

パラメータ(build.xml

こんな感じです
property要素のvalue属性でDatabaseを指定しています

<?xml version="1.0" encoding="UTF-8"?>
<project name="database-diff" default="default" basedir=".">

  <!-- ver3.3.0とver3.3.2の比較 -->
  <property name="url"value="jdbc:postgresql://localhost:5434/ver332"/>
  <property name="referenceeUrl" value="jdbc:postgresql://localhost:5434/ver330"/>

  <echo message="url is ${url}"/>
  <echo message="referenceeUrl is ${referenceeUrl}"/>

  <path id="classpath">
    <fileset dir="." includes="*.jar"/>
  </path>

  <target name="default">
    <taskdef resource="liquibasetasks.properties">
      <classpath refid="classpath"/>
    </taskdef>
    <diffDatabase
        driver="org.postgresql.Driver"
        url="${url}"
        username="postgres"
        password="postgres"
        referenceUrl="${referenceeUrl}"
        referenceUsername="postgres"
        referencePassword="postgres"
        outputFile="outputfile.txt"
        classpathref="classpath">
    </diffDatabase>
  </target>

</project>

 

実行

build.xmlで指定したoutputfile.txtが作成されてます

$ ant
Buildfile: build.xml
     [echo] url is jdbc:postgresql://localhost:5434/ver332
     [echo] referenceeUrl is jdbc:postgresql://localhost:5434/ver314

default:

BUILD SUCCESSFUL

$ ls -l outputfile.txt
-rwx------+ 1 Administrators mkpasswd 29746 4月   4 17:34 outputfile.txt
Total time: 20 seconds

 

結果

Reference Database: postgres @ jdbc:postgresql://localhost:5434/ver314
Target Database: postgres @ jdbc:postgresql://localhost:5434/ver332
Product Name: EQUAL
Product Version: EQUAL
Missing Tables: 
     AAAAATbl
     BBBBBTbl
     CCCCCTbl
Unexpected Tables: 
     DDDDDTbl
     EEEEETbl
     FFFFFTbl
Missing Views: NONE
Unexpected Views: NONE
Changed Views: NONE
Missing Columns: 
     ZZZZZTbl.name
     ZZZZZTbl.tel
Unexpected Columns: 
     YYYYYTbl.zipcode
     YYYYYTbl.address

 

おまけ

上記build.xmlは修正済みですが、マニュアル(http://www.liquibase.org/ja/manual/diffdatabase_ant_task)通りに作成するとエラーが発生します

$ ant
BUILD FAILED
build.xml:27: diffDatabase doesn't support the "baseUrl" attribute

Javadochttp://www.jarvana.com/jarvana/view/org/liquibase/liquibase-core/2.0-rc5/liquibase-core-2.0-rc5-javadoc.jar!/index.html?liquibase/integration/ant/DiffDatabaseTask.html)を確認したところ、プロパティの名称がbaseXXXからreferenceXXXに変更されてますね、、

Antのマニュアルの修正が漏れているみたいです