Introduction to Liquibase and its Advantages

background
Reading Time: 5 minutes

New to Liquibase? Welcome! This blog will explain the essential introduction to Liquibase and its Advantages.

Common Liquibase Definition

Liquibase is an open-source database-independent library for tracking, managing, and applying database schema changes.

A little background

Before we go into detail about Liquibase, let us see the common problems we face as a developer while working with the application and the database scripts. Now, as a developer, we have version control tools for maintaining the application code but we do not have any tools which actually maintain the version control for your database changes. That’s the reason, once you have promoted the application code to the higher environment, sometimes the developer may further go to execute the database scripts which actually have the dependency for that particular application.

That is the reason we are going to face some problems when we are promoting our application code to the production environment. So to eradicate such kind of problems with respect to the database scripts, we are introducing a tool called Liquibase.

Liquibase will actually act as version control for your database Scripts. Liwuibase is a source control tool for your database. It also supports branching and merging. Like other versioning tools, it supports multiple developers working on it at the same time.

Liquibase in detail/Advantages:

  • Liquibase is a source control tool for your database: It is going to maintain the version of the scripts that you are going to execute.
  • Liquibase supports code branching and merging: liquibase works best on different files types like XML, YAML, JSON, etc so you can provide your database change-related scripts in these formats so that a particular format is branched out and finally gets merged.
  • Support multiple developers: multiple devs can work together and create their own scripts with respect to the database and then commit it as part of their code.
  • Supports context-dependent logic: if you have created a few scripts which might have to be executed at a particular version, so you can specify that in the context. It will be executed only for that particular version.
  • Cluster-safe database upgrades: if you are creating a fresh project or old projects are there that you want to upgrade to liquibase, it gets very easy with liquibase.
  • Generate database change documentation: you can easily generate documentation for your database changes.
  • Generate database “diffs”: if you are on two different databases, you can easily see the diffs(differences) between both using liquibase.
  • Run through your build process, embedded in your application or on demand. E.g: you can integrate with ant/maven/Gradle build tools.
  • Automatically generate SQL scripts for DBA code review (update SQL, rollback SQL).
  • Does not require a live database connection.
  • Datical is both the largest contributor to the liquibase project and the developer of Datical DB.

Liquibase supports a list of database types

  • MySQL
  • PostgreSQL
  • Oracle
  • MSSQL
  • Sybase
  • asany
  • db2
  • derby
  • hsqldb
  • h2
  • Informix
  • firebird
  • SQLite

How does Liquibase work?

Liquibase is a source control version tool for your database scripts.

Liquibase basically works based on the log element that is the Database change log. The database change log file can be in form of XML/JSON/YAML/SQL. Now once you have prepared the database change logs, now you have to move to the changeset. ChangeSet is nothing but SQL that contains the changes that are to be done in the database.

ChangeSet takes two fields ie. id and author. If the id and author match the actual database then further operations are carried out otherwise it throws an exception.

Sample change log file for XML format:

<?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"
	xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
	xmlns:pro="http://www.liquibase.org/xml/ns/pro"
	xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
		http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
		http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
		http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
    <preConditions>  
        <runningAs  username="liquibase"/>  
    </preConditions>  

    <changeSet  id="1"  author="nvoxland">  
        <createTable  tableName="person">  
            <column  name="id"  type="int"  autoIncrement="true">  
                <constraints  primaryKey="true"  nullable="false"/>  
            </column>  
            <column  name="firstname"  type="varchar(50)"/>  
            <column  name="lastname"  type="varchar(50)">  
                <constraints  nullable="false"/>  
            </column>  
            <column  name="state"  type="char(2)"/>  
        </createTable>  
    </changeSet>  

    <changeSet  id="2"  author="nvoxland">  
        <addColumn  tableName="person">  
            <column  name="username"  type="varchar(8)"/>  
        </addColumn>  
    </changeSet>  
    <changeSet  id="3"  author="nvoxland">  
        <addLookupTable  
            existingTableName="person"  existingColumnName="state"  
            newTableName="state"  newColumnName="id"  newColumnDataType="char(2)"/>  
    </changeSet>  

</databaseChangeLog>

Sample change log file for JSON format:

{
            "databaseChangeLog": [
              {
                "preConditions": [
                  {
                    "runningAs": {
                      "username": "liquibase"
                    }
                  }
                ]
              },
              {
                "changeSet": {
                  "id": "1",
                  "author": "nvoxland",
                  "changes": [
                    {
                      "createTable": {
                        "tableName": "person",
                        "columns": [
                          {
                            "column": {
                              "name": "id",
                              "type": "int",
                              "autoIncrement": true,
                              "constraints": {
                                "primaryKey": true,
                                "nullable": false
                              },
                              
                            }
                          },
                          {
                            "column": {
                              "name": "firstname",
                              "type": "varchar(50)"
                            }
                          },
                          {
                            "column": {
                              "name": "lastname",
                              "type": "varchar(50)",
                              "constraints": {
                                "nullable": false
                              },
                              
                            }
                          },
                          {
                            "column": {
                              "name": "state",
                              "type": "char(2)"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "changeSet": {
                  "id": "2",
                  "author": "nvoxland",
                  "changes": [
                    {
                      "addColumn": {
                        "tableName": "person",
                        "columns": [
                          {
                            "column": {
                              "name": "username",
                              "type": "varchar(8)"
                            }
                          }
                        ]
                      }
                    }
                  ]
                }
              },
              {
                "changeSet": {
                  "id": "3",
                  "author": "nvoxland",
                  "changes": [
                    {
                      "addLookupTable": {
                        "existingTableName": "person",
                        "existingColumnName": "state",
                        "newTableName": "state",
                        "newColumnName": "id",
                        "newColumnDataType": "char(2)",
                        
                      }
                    }
                  ]
                }
              }
            ]
          }

In the same way, you can provide any number of the changelog. To know more about change log patterns you can simply visit the Official Liquibase change log documentation.

Once you are done with preparing the database changelogs, now liquibase needs to identify which type of database you want to execute these scripts. Now in order to identify the database type, whenever we are using a maven/Gradle, you are going to define what type of database it is in the configurations/properties file.

You will define what type of database it is, JDBC Driver, the DB URL, and the password. Once the database is identified, it looks for the database changelogs and identifies the changeset. Now it will prepare the SQL statements according to the identified database and the properties given in the JSON/XML etc file.

Whenever it is executing the scripts, if it is the first time it is executing then liquibase will create two tables and maintain the version control for your database changes.

Tables created in Liquibase:

The first table will be DATABASECHANGELOG, it will contain whatever scripts you are executing. The second table is DATABASECHANGELOGLOCK, it is basically used to maintain the atomic transactions i.e only one person at a time can modify the database scripts at a time.

The next time you execute any database scripts, the tables are not created from the scratch. Only updation will happen in the already created tables.

This is all about the working of Liquibase.

Conclusion

After knowing a briefly about liquibase, we can say that liquibase is beneficial to use to keep the database version control in check, to generate various reports and many more advantages.

To read more about Liquibase, please stay tuned and keep visiting Knoldus Blogs.

Reference links – Liquibase Documentation

Written by 

Sakshi Mittal is a Software Consultant at Knoldus Software. She has completed her MCA from BCIIT and Bachelors in Computer Applications from GGSIPU. Her practice area is Java but she loves to explore machine learning field as well. She likes writing tech blogs and contribute to open source. When not working you will find her watching travel and food vlogs.

Leave a Reply