Liquibase is an open source technology for tracking, managing and applying database schema changes.

All changes to the database are stored in XML files in the form of change-sets and is identified by a combination of an “id” and “author” tag. All the change-sets are applied sequentially one by one.It also creates DatabaseChangeLog Table and DatabaseChangeLogLock table when you first execute a changeLog File.

DatabaseChangeLogLock table maintains the lock information granted to the particular user. The purpose of this table is to make sure that two machines doesn’t try to modify the data simultaneously.

Screenshot:
liquibase2

DatabaseChangeLog table contains the list of the statements executed on the database.

Screenshot:

liquibase1

Sample liquibase xml file:

<?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">

  <changeSet author='Vivek S' id="#99999- Contracts">

        <createTable tableName="contract">
            <column name="id" type="java.sql.Types.INTEGER">
                <constraints nullable="false" primaryKey="true" primaryKeyName="contract_pkey"/>
            </column>
            <column name="entity_id" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>

            <column name="active_since" type="java.sql.Types.DATE" />
            <column name="notes" type="java.sql.Types.VARCHAR(200)"/>
            <column name="active" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>
            <column name="deleted" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>
            <column name="template" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>
            <column name="automatic" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>
            <column name="automatic_frequency" type="java.sql.Types.INTEGER"/>
            <column name="optlock" type="java.sql.Types.INTEGER">
                <constraints nullable="false"/>
            </column>
        </createTable>


<addForeignKeyConstraint constraintName="contract_entity_id_fk" referencedTableName="entity" referencedColumnNames="id" baseTableName="discount" baseColumnNames="entity_id"/>
    

    </changeSet>

</databaseChangeLog>

To execute the liquibase script from command line use this:-

liquibase --driver=com.mysql.jdbc.Driver 
     --classpath=/path/to/classes 
     --changeLogFile=com/example/db.changelog.xml 
     --url="jdbc:mysql://localhost/example" 
     --username=user 
     --password=pass 

The main question is why to use liquibase ?

The main reason is that it keeps a track of all changes applied to a database. We can also rollback to a particular change. It also enables use to specify pre-conditions like which change-set should be executed on which db type like this:

<preConditions>
     <or>
         <dbms type="oracle" />
         <dbms type="mysql" />
     </or>
 </preConditions>

Following are the advantages:

  • Executable via command line.
  • Support for more than 5 database systems
  • DBMS Check, user check using preconditions
  • That was all about liquibase! I hope you liked it. 🙂