Liquibase – versional control for database

As a Java developer dealing with databases i knew about hibernate and ORM tool to connect project with databases using simple xml configuration files. I though liquibase would be something similar, since had a rough idea that I stores the database table structure (rather script) in an Xml file. But to my surprise it is entirely different.

 

Problems to address related to databases especially in Agile development

# I have taken the new code on my sand box but functionality is not working, I think there are some database related changes.

–        Conventional solution: I picked the latest database script and run it on mysql database and functionality started working well.

–        Liquibase solution : As i run the mvn install after code synchronization, the database also got synchronized to current version.

 

# I found some changes in the database but I do not know who has made those changes and when.

–        Conventional solution I emailed the entire dev group and waited till some came up with the owner ship of that change.

–        Liquibase solution I just referred the author name for the change set and pinged him to discuss without sending any junk mails.

# I have reverted my code to a particular tag or release point, but I am not aware about the database status at that point of time.

–        Conventional solution I searched the database logs and scripts with date stamp and after a struggle I found the script to run.

–        Liquibase solution I used Liquibase tag to generate database base to a particular time line.

# I have to change the database from mysql to oracle or derby.

–        Conventional solution A new script is required for the new database

–        Liquibase solution Same xml with minor modification could be used to accommodate any database changes.

 

There are few problems which are common especially in agile development where database keeps on changes every now and that. To address all these problem especially for developers a new tool called as Liquibase is developed. It is integrated seamlessly in the codebase, which can be controlled using any conventional versioning tool. Hence like the project code changes, you can also maintain the database changes related to code changes in your work space and modify it.

 

What is Liquibase ?

As we have versioning tool for our code so that we can maintain different tags to roll back and maintain the incremental evolution/history of our code. Similarly, now that database changes can be tracked with timestamp, id, author and tag. Instead of making direct changes to database the changes are done through liquibase Databasechangelog xml file example “database_mods.xml”.

The advantage of using this is, it become a single entry point to make changes in database, easier to track, and roll back to a restore point for a particular release of project. You will get to know how has made what all changes and when.

In short:

LiquiBase is an open source database-independent library for tracking, managing and applying database changes. In laymen terms it is more like a database script in xml format. However it have its own IDE, plugins for eclipse and perform several other important activities.

 

Two important table of liquibase:

“databasechangeloglock ” is used to control the concurrency while updating the database. For example if more that 2 developers makes changes to database table instance at the same time, one developer acquires the lock so prevent the inconsistency.

 

“Databasechangelog” is the  change log table in which all the changes set with their description, author, timestamp, ids and other properties gets stored.

How do we run liquibase, mvn – so when we build we used to run it before the compilation.

 

Restore point :

To get to a particular restore point of database one can use any of the below :

1.      Roll back to a particular tag.

2.      Roll back to a particular date.

3.      Roll back to a particular count.

 

Database Diff:

Before final release you want to make sure that databases are in sync you use Diff tool for databases for the same by following methods:

1.      Generate a report

2.      xml change log block and compare it later.

 

Sample liquibase xml file:

 

<?xml version=”1.0″ encoding=”UTF-8″?>

<databaseChangeLog xmlns=”http://www.liquibase.org/xml/ns/dbchangelog/1.5&#8243;

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

xsi:schemaLocation=”http://www.liquibase.org/xml/ns/dbchangelog/1.5 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.5.xsd”&gt;

 

<changeSet id=”153-mysql” author=”ajain” dbms=”mysql”>

<comment> adding a new table for roles  </comment>

<createTable tableName=”fusion_role”>

<column name=”role_id” type=”bigint”>

<constraints primaryKey=”true” nullable=”false”/>

</column>

<column name=”description” type=”varchar(255)” />

<column name=”name” type=”varchar(255)”/>

<column name=”version” type=”integer”/>

<column name=”organization_id” type=”bigint” />

</createTable>

<addAutoIncrement tableName=”fusion_role” columnName=”role_id” columnDataType=”bigint”/>

</changeSet>

</databaseChangeLog>

 

 

Video presentation to know liquibase more :

http://www.liquibase.org/swf/overview_long/LiquiBase%20TechTalk.html



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s