Liquibase Series: Automating Database Changes with Liquibase

Manish Sharma
5 min readJan 30, 2023

--

Liquibase

  • Liquibase is an open-source database-agnostic library for tracking, managing, and applying database schema changes.
  • It works with most popular relational databases, such as MySQL, Oracle, and PostgreSQL, and also supports NoSQL databases like MongoDB.
  • It is designed to make tracking, versioning, and deploying database schema changes easier and more reliable.
  • Liquibase allows you to track and manage database schema changes, and supports different ways of applying them.
  • It can be used to keep a database in sync with a version control system, and also helps automate database deployments.
  • In addition to this, it provides tools to help you analyze your database structure and make sure it adheres to the proper standards.

Benefits

The major benefit of using Liquibase is that

  • it makes database deployment and maintenance easier.
  • It enables you to track and manage database schema changes, so that you can ensure that your database is always up-to-date and secure.
  • Other benefits include the ability to integrate database changes into a version control system, making them easier to manage, and providing an easy way to monitor and analyze your database structure for errors or inconsistencies.

Use Cases

Examples of use cases includes,

  • Managing database migrations
  • Maintaining database versioning
  • Automating database deployments, and
  • Performing data analysis

Installation

  1. Download the latest version of Liquibase from Liquibase’s official website.
  2. Extract the ZIP file you downloaded in the previous step.
  3. Add the Liquibase executable directory to your PATH environment variable.
export PATH=$PATH:<path to liquibase installation>

4. Confirm that the installation was successful by running

liquibase --version

on your terminal. This should display the version number of Liquibase.

Liquibase Gobal Configuration

  • The liquibase.properties file is a Java-based configuration file located in the root of your project directory.
  • It contains information used to configure Liquibase, such as the database URL and driver class name, database username and password
  • The file also allows you to define global properties which can be accessible to all change sets in the changelog file.
changeLogFile=changelog.sql

liquibase.command.url=jdbc:mysql://<db-hostname>:3306/<db_name>
liquibase.command.username=<DATABASE_USER>
liquibase.command.password=<DATABASE_PASSWORD>

Example ChangeLog File

Liquibase formatted SQL in a changeset file is a structured format for capturing and storing DDL (Data Definition Language) changes to a database.

This format is used to make sure that the changes are properly tracked, so that they can be applied to different environments and rolled back if needed.

Each SQL file must begin with the following comment --liquibase formated sql

--liquibase formated sql

--changeset CREATE:1
CREATE TABLE EMPLOYEE(
EMP_ID int primary key,
EMP_NAME varchar(30) not null,
)

--rollback DROP TABLE EMPLOYEE;
--changeset CREATE:2
CREATE TABLE SALARY(
ID int primary key,
EMP_ID int not null,
EMP_SALARY int not null
)
--rollback DROP TABLE SALARY;

Liquibase Commands

1. Validate

The Liquibase validate command is used to verify that the changes about to be applied to a database are valid. It checks for syntax errors and other potential problems, making sure that the changes will not cause any issues when applied. The command can be used to check individual change sets, all changes in a changelog file, or all changes in a database.

liquibase validate --changeLogFile=example.xml --logLevel=debug

This command will read the example.xml changelog file, check all changes for validity, and log any errors to the console at debug level.

2. Update

The Liquibase update command is used to apply database changes to a database. It can be used to apply a single change, or all the changes in a changelog file. This command will read the changelog file and execute each change set sequentially. The command can also be used with the — validate option to ensure that all changes are valid before applying them.

liquibase update --changeLogFile=example.xml --contexts=production

This command will read the example.xml changelog file and apply all changes to the production database context.

3. Rollback

  • The Liquibase rollback-count command is used to rollback a specified number of changes from the database.
  • It searches for the most recent change sets and rolls back that number of changes.
  • This command is useful for undoing mistakes or reverting to a previous version of the database.
  • The command takes a parameter that specifies the number of changes to rollback, as well as an optional context parameter to limit which changes are rolled back.
liquibase rollback-count 5 --contexts=production --changelog-file=changeset.sql 

This command will rollback the most recent 5 changes from the production database context.

Liquibase GitLab Pipeline

You can use a Gitlab CI/CD pipeline to automate Liquibase database deployments.

The pipeline will make sure that the database changes are applied in the correct order and that all database objects are up-to-date with the version control system.

To set up a Liquibase pipeline, you’ll need to define the jobs and steps for your project. Examples of tasks include running

  • Liquibase update command,
  • validating the database, and
  • running tests to confirm that the database has been properly updated.
image: liquibase/liquibase:latest

variables:
DEFAULT_FILE: liquibase.properties
CHANGE_LOG_FILE: changeLogFile.sql
MYSQL_JAVA_DRIVER: ./driver/mysql-connector-java-8.0.28.jar

stages:
- validate
- deploy
- rollback

validate:
stage: validate
script:
- liquibase validate --changeLogFile=$CHANGE_LOG_FILE --default-file=$DEFAULT_FILE --classpath=$MYSQL_JAVA_DRIVER
only:
- dev

deploy:
stage: deploy
when: manual
script:
- liquibase update --changeLogFile=$CHANGE_LOG_FILE --default-file=$DEFAULT_FILE --classpath=$MYSQL_JAVA_DRIVER
only:
- dev

rollback:
stage: rollback
when: manual
script:
- liquibase rollback-count 5 --changeLogFile=$CHANGE_LOG_FILE --default-file=$DEFAULT_FILE --classpath=$MYSQL_JAVA_DRIVER --forceRollbackOnError=true
only:
- dev
  • Each database schema change you make with Liquibase is called a changeset.
  • All changesets are tracked by Liquibase using changelogs.
  • Liquibase allows you to create a trigger that updates the database automatically by pointing to the changelog file. From here, it makes it easy to integrate the process into your overall CI/CD process:
  1. Push your changeset files to your feature repository
  2. Create a pull request against the Dev branch
  3. After peer review and approvals, merge the feature branch with the Dev branch
  4. The CI/CD implementation configured on the Dev server triggers Liquibase for database updates
  5. Liquibase automatically executes any new changelog files

Best Practices

  • Use version control to track your database schema changes
  • Use descriptive change set names and IDs
  • Test all changes before deploying to production
  • Use tags or labels to mark different versions of the database schemas
  • Consider the rollback options available with Liquibase
  • Keep the context(s) up-to-date when deploying changes
  • Ensure all database objects are properly managed and tracked
  • Monitor the performance of your database at regular intervals

Liquibase Security Consideration

Here are some security considerations when using Liquibase:

  • Protect sensitive data by using encryption, hashing, and/or obfuscation
  • Limit access to the database to only those who need it
  • Set up a secure connection between your application and the database
  • Make sure that the database user running Liquibase has the minimum necessary privileges
  • Monitor usage of the Liquibase commands and look for anomalies
  • Use version control to track and review changes to the database

Please show your appreciation by following and liking this article!

--

--

Manish Sharma

I am technology geek & keep pushing myself to learn new skills. I am AWS Solution Architect — Associate, Professional & Terraform Associate Developer certified.