Saturday, August 2, 2008

Liquibase + Maven

In IT today, our processes are too often manual (i.e., slow and costly) which limits change and, in part, forces us to model a dynamic business with a database designed for the business as it was years ago. By incorporating a versioned database build along with the regular application build, developers can be in control of the application environment rather than slaves to past design decisions no longer make sense.

Here I've documented a Maven build that includes execution of a database modification using Liquibase. Liquibase is a database model management tool that integrates with your build tool such that database changes are managed along with application code. Implementing and rolling back modifications and refactorings affecting application code and the database occur easily and simultaneously. With Liquibase, changes to the database can be diffed and merged along side application code. Liquibase integrates easily with Hibernate to automatically create the database updates required by changes to your Hibernate classes. Use Maven and Liquibase; fear change no more.

The pom.xml file was created using the default archetype (fig. 1) then edited to include the MySQL and Liquibase dependencies and the Liquibase build element(fig. 2). The Liquibase changelog file (src/main/resources/changelog.xml) is from the Liquibase Quickstart example (fig.3). Finally, there are the commands to start MySQL, create the database and run Maven (fig. 4).

C:\src\java\liquibase_project>mvn archetype:create 
-DartifactId=mouseapp -DarchetypeArtifactId=maven-archetype-webapp

<project xmlns="" 
  <name>mouseapp Maven Webapp</name>

<?xml version="1.0" encoding="UTF-8"?>


    <changeSet id="1" author="bob" dbms="mysql">
      <createTable tableName="department">
         <column name="id" type="int">
             <constraints primaryKey="true" nullable="false"/>
         <column name="name" type="varchar(50)">
             <constraints nullable="false"/>
         <column name="active" type="boolean" defaultValue="1"/>

C:\src\java\liquibase_project\mouseapp>mysql -u root -p
Enter password: ****
mysql> create database mousedb;
mysql> quit
C:\src\java\liquibase_project\mouseapp>mvn clean package