Liquibase

How Liquibase Works?

Liquibase is used to track database changes. It is an Open Source Library. We all have worked in systems where the typical database change operation process was to write DDL, and DML scripts in SQL lines which were source-controlled, and database changes were executed by the developer or by some other team. But there wasn’t really a nice way to define the sequence of these changes, it was followed as stylish practice in the design. We always had to write SQL for rollbacks as well migration to other databases was a tough task for developers.

Liquibase tries to address all these issues in a simple way. Liquibase scripts are generally written in XML format( because XML is more readable),

though other formats like JSON and yaml. These are also Supported scripts by Liquibase. Database changes are defined in change logs lines as small change sets which are uniquely linked by change set name or id and author. The sequence is defined in master change log tables. Rollbacks are handled by Liquibase itself in the utmost cases. And since these scripts are database individual, migrations to other databases are fairly smooth. As of now, Liquibase supports the utmost of the popular database including MySQL, Oracle, DB2, and PostgreSQL.

Add Liquibase Dependency to the project

<dependency>

   <groupId>org.liquibase</groupId>

   <artifactId>liquibase-core</artifactId>

   <version>4.3.2</version>

</dependency>

<dependency>

   <groupId>org.liquibase</groupId>

   <artifactId>liquibase-maven-plugin</artifactId>

   <version>4.3.2</version>

</dependency>

 

Implementation of Liquibase in Database

#liquibase

spring.liquibase.enabled=true

spring.liquibase.change-log=classpath:/db/liquibase_master.xml

spring.datasource.url=jdbc:mysql://localhost:3306/LiqubaseTracker?createDatabaseIfNotExist=true

spring.datasource.username=root

spring.datasource.password=1234

spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

 

Project Structure

  • Track database change logs

For this, we create a master_liquibase.xml file for every new file added and every single change we mention here to track the logs in the database

 

<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-4.1.xsd">

                               

     <include file="db/ddl/DDL.xml"/>

     <include file="db/dml/USER.XML"/>

     <include file="db/dml/HOTEL.XML"/>

</databaseChangeLog>

 
  • ChangeSet Example

 

<changeSet author="akash.mishra" id="041220222111">

   <createTable tableName="USER">

       <column name="ID" type="VARCHAR(255)">

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

       </column>

       <column name="NAME" type="VARCHAR(255)"/>

       <column name="NUMBER" type="VARCHAR(20)"/>

       <column name="PROFILE_IMAGE" type="VARCHAR(20)"/>

   </createTable>

</changeSet>

For the first time create two additional tables in the databases

1 DatabasechangelogLock

2 Database-changelog

 

Some important rules that we have to follow

  1. Organizing change logs As shown in the design structure over, elect a directory to store all your change log lines and define their sequence in the master change log train. Liquibase suggests organizing these lines by major interpretation but working in a multi-inventor terrain, I would recommend skipping interpretation from train names to avoid conflicts arising to inventors choosing the same performances. It’s better to choose a logical and explicatory name for these change log lines which upon first look would give some idea about the type of database changes it contains.E.g.db.changelog-add-queued-messages-table.xmldb.changelog- add- retry- column- to- queued- dispatches-table.xml

 

  1. Changeset id and author name: a combination of changeset id and author name uniquely identifies a change set. Changeset id should be logical and the author name should easily identify the developer who developed the change set.
<changeSet author="akash.mishra" id="0412">

       <insert tableName="USER">

           <column name="ID" value="1"/>

           <column name="NAME" value="Akash"/>

           <column name="NUMBER" value="01A"/>

           <column name="PROFILE_IMAGE" value="Akash.jpeg"/>

       </insert>

</changeSet>

 

Liquibase offers rollback for DDL statements but for all DML statements rollback has to be handled explicitly by the inventor. For illustration, the below change set of creating a hand table will produce one record in the database changelog table for this change set. For rolling back this change.

 

<?xml version="1.1" encoding="UTF-8" standalone="no"?>

<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-3.3.xsd">

                

                

<changeSet author="akash.mishra" id="0412">

       <insert tableName="USER">

           <column name="ID" value="1"/>

           <column name="NAME" value="Akash"/>

           <column name="NUMBER" value="01A"/>

           <column name="PROFILE_IMAGE" value="Akash.jpeg"/>

       </insert>

</changeSet>

   <changeSet author="akash.mishra" id="2201221222">

       <insert tableName="USER">

           <column name="ID" value="2"/>

           <column name="NAME" value="AkashMishra"/>

           <column name="NUMBER" value="872838389"/>

           <column name="PROFILE_IMAGE" value="Akash"/>

       </insert>

   </changeSet>




   <rollback>

       <sql>

           delete from USER where ID = "1"

       </sql>

   </rollback>




</databaseChangeLog>

Leave a Reply