Database Versioning — Liquibase

Waq Ahmed
2 min readMay 6, 2022

Liquibase Community is an open-source project that helps to manage database schema changes. It helps us to create versioning of DB (database) and revert to any changes at any time without restoring the whole DB.

Traditionally we (System Administrators) manage the DB by backing up the DB and then restoring it completely if want to revert the changes. There were challenges while setting up the RTO ( Recovery Time Objective) and RPO( Recovery Point Objective) for DB backup. Also to revert one change or query reverting the whole database was a cumbersome process. Thanks to Liquibase to address this problem here.

In this blog I will be demonstrating the Liquibase with JAVA source code to make changes to DB and roll back the changes as required. We will use Maven to compile the source code. A Liquibase plugin for Maven is located in the central Maven repository.

  1. In Liquibase Maven project directory, create pom.xml file and add the following section
<plugin> <groupId>org.liquibase</groupId> 
<artifactId>liquibase-maven-plugin</artifactId> <version>4.2.0</version>
</plugin>

2. Create a text file called changelog.xml in the src/main/resources/db/changelog directory. This file contains the SQL query that you want to execute against your database. Always give changeset id unique number with author name to create a version number in database which will be needed at the time of rollback.

Changelog.xml file that contains SQL query to run against your DB

3. Specify your changelog file along with the database URL, username, and password in the pom.xml file to run Maven goals:

<changelog-file>src/main/resources/db/changelog/changelog.sql</changelog-file> <url>YourJDBCConnection</url> 
<username>dbuser</username>
<password>dbpassword</password>

4. Deploy your changes by using the update goal:

mvn compile package
mvn liquibase:update

Once you run the liquibase update, it creates 2 tables in DB with names ‘databasechangelog’ & ‘databasechangelock’. This table contains all the records of the query executed against the database.

datbasechangelog created and contains 1st changeset entry & Person123 table created

To rollback the change, use the id number in databasechangelog to rollback

mvn liquibase:rollback -Dliquibase.rollbackcount=1

I hope that this article will be helpful to easy to understand, if you like this please hit the clap icon 👏

--

--

Waq Ahmed

I’m an DevOps Engineer and have keen interest and experienced in Cloud Computing, Docker, Kubernetes, and InfraStructure provisioning tool