Using Liquibase for database updates

This is a post-it for me so I get a resource on how to install and run liquibase in case I forget.

What is Liquibase ?

LiquiBase is an open source (LGPL), database-independent library for tracking, managing and applying database changes. It is built on a simple premise:

All database changes are stored in a human readable yet trackable form and checked into source control.

Source :http://www.liquibase.org

There are few introductory videos available here :

Installation

Do not use the RC version because it does not seem to work well yet. Using 1.9.5 version is fine and works out of the box.

Download Liquibase here : http://www.liquibase.org/download

Store it in :

/opt/jerome/local/liquibase-1.9.5

Install the Java connector for mysql and postgresql :

sudo port install mysql-connector-java postgresql-jdbc

Now use the following XML changelog file for testing. Do not expect any relevancy in this XML file it is just there to test that Liquibase works :

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<changeSet id="1" author="me">
    <comment>bla bla bla bla</comment>
    <createTable tableName="person">
    <column name="id" type="int">
        <constraints primaryKey="true" nullable="false"/>
    </column>
    <column name="firstname" type="java.sql.Types.SMALLINT"/>
    <column name="lastname" type="java.sql.Types.REAL"/>
    <column name="username" type="java.sql.Types.LONGVARCHAR">
        <constraints unique="true" nullable="false"/>
    </column>
    <column name="testid" type="int"/>
    </createTable>
</changeSet>
</databaseChangeLog>

http://java.sun.com/j2se/1.3/docs/api/java/sql/Types.html Use also the following liquibase.properties file

classpath:/opt/local/share/java/mysql-connector-java-5.0.jar:/opt/local/share/java/postgresql.jar
changeLogFile=createtable.xml

# Mysql Settings
username=root
password=1234
url=jdbc:mysql://localhost/testsliquibase

# Postgres driver
#url=jdbc:postgresql://localhost/postgres
#username=postgres

logLevel=severe

Now try everything works fine:

liquibase updateSQL

Troubleshooting

In case Liquibase fails to connect to MySQL make sure the login credentials are correct and that it is possible to connect to mysql by using the mysql CLI client. If yes, but Liquibase still refuses to connect to MySQL then comment the following line in /etc/hosts :

::1             localhost

and retry the liquibase command. It should work now.

Comments !

blogroll

social