Liquibase CLI
About
Liquibase is an database schema management tool that helps managing and versioning of the database schema changes. It allows defining database schema changes in a declarative way using XML, YAML, JSON, or SQL formats, and then apply these changes to the database. It supports a wide range of relational databases, including Oracle, MySQL, SQL Server, DB2, and others.
Liquibase and manual SQL queries.
Liquibase and manual SQL queries are two different approaches to managing database schema changes.
Manual SQL queries requires writing of the SQL statements necessary to create, modify, or delete database objects, such as tables, indexes, and constraints. These SQL statements need to be run manually, either through a database console or a script, to apply the changes to the database schema.
Liquibase, on the other hand, is a tool or library that provides a declarative way to manage database schema changes. With Liquibase, schema changes can be defined in a change log file, which can be versioned and tracked just like the application code. This makes it easy to manage database schema changes across different environments.
For more details on Liquibase, visit the official website - https://docs.liquibase.com/
Setup and Run Liquibase using CLI method
There are several ways to setup Liquibase, depending on the needs and preferences. In this, we will use CLI method.
Download the zip file from the liquibase release page and extract it - https://github.com/liquibase/liquibase/releases

Open the profile (in my case .zprofile) and add the path for liquibase so that it is accessible via terminal.
Open the terminal and type below command to verify the installation.

Setup Mysql Instance
We will use mysql instance in local, through which liquibase will interact. Let's use docker compose method for the same.
docker-compose.yaml
Start the mysql instance with below command.
Note: We can connect to mysql db through MySQLWorkbench with the help of root as username and password

Mysql Driver
Liquibase will need mysql driver to work with mysql. We can download the mysql driver from the official site - https://dev.mysql.com/downloads/connector/j/

Changelog Files Setup
In Liquibase, a changelog is an XML, YAML, or JSON file that describes the set of changes to be applied to a database. The "changeLog-master" file is the entry point that includes or references multiple individual changelog files. We will use YAML in this blog. The purpose of the "changeLog-master" file is to provide a central place to organize and manage all the changes to the database schema. It helps in maintaining a structured and sequential order of changes and facilitates collaboration among multiple developers working on the same database.
Let's create changeLog-master.yaml file which will have reference to 3 separate changelog file.
changeLog-master.yaml
Note: 3 changelog files are housed in tables-setup-0_1 folder
add-table-books-04052023.yaml (Creates a tables with name - books)
add-table-users-03052023.yaml (Creates a table with name - users)
update-table-users-05052023.yaml (Updates table - books to add new column - author)
Folder structure will look like below

Liquibase Properties
Now, let's create liquibase.properties file to hold liquibase configuration data.
Overall Structure
Overall the folder structure is as below

Execute Changelog Files
It's time to execute our changelog files.

Run the below liquibase command on the terminal from the root folder of liquibase-example

We can from the logs that all the changelog files were applied. Let's us verify from the database as well.

Files are attached for the reference below.
Last updated