Database Version Control With Liquibase
Written by Suresh Regmi
Let me give you a scenario,
You have a project with multiple database instances in different environments (Dev, QA, Production) and you need to manage the database schema changes that are done against those environments. Let’s assume that you are managing those changes by creating a git project or a shared file on a drive and adding a new SQL file for each database changes you are doing. To implement your changes in that database, for each SQL file, you need to run the changes in each environment manually and add a flag or note to indicate which change is run on which environment.
Would it be able to complete your task? Yes, Yet, is it a decent method to manage schema changes?
Of Course Not.
Here are some of the many problems you might face while doing so
- Hard to synchronize database and application code changes in different environments
- The tedious process to run each change manually in different environments
- Collaboration across the development team on what change is deployed and what is not
- Hard to roll-back to the previous version of the database
- Possibility of data loss
Here comes Liquibase
Liquibase is an open-source library for tracking and managing database schema changes that can be used for any database with a JDBC driver.
It is a platform-independent database migration tool that allows the database changes referred to as ‘changesets’ to be written in various formats including XML, JSON, YAML, and SQL.
- Supports almost all databases that have a JDBC driver.
- Changesets can be written in different formats like XML, JSON, YAML, and SQL.
- Can be used to automatically generate changesets for an existing database
- Easy to integrate with build tools like Jenkins, Maven etc
- Supports database rollbacks
- Supports context-dependent logic allowing us to use global context and preconditions
- Can be executed via command line, Apache Maven, Apache Ant, Spring Framework
- Has feature to generate changeset from an existing database and can also generate schema difference as changesets
Different ways to run liquibase
- Embed liquibase with your app: Embedding liquibase with your application code will automatically deploy liquibase on the app startup.
- Run liquibase using build tools: Integrate liquibase into your build process (with build tools like Jenkins, Ant, Maven, and Gradle) and update them without being tied up with the application.
- Generate the SQL and run it manually: Using update SQL, Liquibase provides the SQL generated from the changeset along with the database changes required to keep the tracking tables up to date. DBA will then inspect the SQL and run them against the database.
Prerequisites: Liquibase requires Java 8+
There are two ways to install Liquibase, Manual installation and using liquibase installer.
If you set up liquibase using the liquibase installer, dependencies, directories, config and properties files will all be in place already. It also provides some examples which will provide you with the core concepts required to understand the changesets.
In the case of manual installation, you need to download the compressed liquibase file and extract it in your workspace.
For windows users, you need to add a new
PATH variable in the
For macOS users, the path should be added to the
For detailed instruction on Installation please follow this document .
liquibase.properties: The file
liquibase.properties is a text-based file that stores common properties like database connection parameters, driver details, classpath parameters, global changelog parameters etc. If you install liquibase using liquibase installer, it will provide pre-written
liquibase.properties file while in case of manual installation, you need to create
liquibase.properties file using a sample file provided.
DatabaseChangeLog: Databasechangelog is a file where all changesets go. Each database changelog can include one or more changesets.
Changeset: In liquibase, a changeset is represented as an atomic change to the database. Each changeset should be uniquely identified using author and id fields. The database handles each changeset as a single transaction. Changesets can be written in JSON, XML, SQL and YAML formats.
DATABASECHANGELOG & DATABASECHANGELOGLOCK: These two tables are created by liquibase to track the changes that are run against the database and to make sure that no other migrations age going on.
What if I don’t like Liquibase?
If you told me that you don’t like liquibase and are looking for alternatives, I would ask why not Liquibase first.
Liquibase is a sophisticated tool for database migration that has all features that you need for professional database refactoring and versioning.
But still, if you don’t want to use liquibase, here are some alternatives.
- Flyway: Flyway is an open-source Apache licenced tool for database migration where you can write migrations in database-specific SQL or using Java code. For more details on Flyway, you can refer to this website. https://flywaydb.org/
- YUNIQL: YUNIQL is also an open-source schema versioning and database migration engine that uses plain SQL scripts which can be integrated with CI/CD pipelines. If you want to check out YUNIQL, you can refer to this website. https://yuniql.io/
Originally published at https://www.turtle-techies.com.