Categories
Database

Integrating Liquibase with Spring Boot Project

Liquibase is an open-source database-independent library for tracking, managing and applying database schema changes. It supports various file formats (like XML, YAML, JSON and SQL) for defining the DB structure.The feature that is probably most attractive in Liquibase is its ability to roll changes back and forward from a specific point -saving the user from needing to know what was the last change/script executed on a specific DB instance.

Liquibase uses simple mechanisms to track , version and deploy changes to databases.

  • ChangeLog – An Ordered list of database changes.The ChangeLog acts as a ledger of changes and contains list of changeSets. The database changes are stored as text files and apply to the database. These ChangeLogs can be stored in source control to enable collaboration with team.
  • ChangeSets – The units of changes that Liquibase tracks.        Each ChangeSet is uniquely identified by the author, id, and filename attributes. When Liquibase runs, it queries the DATABASECHANGELOG table for the ChangeSets that are marked as executed and then executes all ChangeSets in the ChangeLog file that have not yet been executed.
  • Tracking tables – Liquibase tracks which ChangeSets have or have not been deployed in a tracking table called a DATABASECHANGELOG. If the database does not already contain a tracking table, Liquibase will create it one.

Liquibase also prevents conflicts from different callers’ updates on a secondary table called DATABASECHANGELOGLOCK. Liquibase also has high-level features such as Contexts, Labels, and Preconditions to accurately control when and where a ChangeSet is deployed.

  • Contexts – Contexts can be applied to ChangeSets to control whether they are run in different environments. If no context is specified, the ChangeSet will run regardless of the execution context.
  • Labels – Same as contexts, labels can also be applied to ChangeSets to control which ChangeSets are executed.
  • Preconditions – Preconditions can be applied to either the ChangeLog as a whole or individual ChangeSets. Preconditions control the execution of an update based on the state of the database, and can halt the update, skip a ChangeSet, mark a ChangeSet as run, or show a warning.

For more details refer Liquibase official documentation

I have used Spring boot framework, Postgres database and Liquibase library in the sample Spring Boot project.

The essential steps to integrate Liquibase with spring boot project are as follows:

  1. Include following snippets into pom.xml

Maven Dependency for Liquibase library

Plugin configuration

  1. For liquibase , you will require to create two files under db/changelog directory structure db.changelog-master.xml and changelog-v1.xml.

You can refer best practices page from Liquibase website for directory structure and sample db.changelog-master.xml.

The db.changelog-master.xml includes the ChangeLog for the releases in the correct order. In the example above it could look like this:

Each of the included XML files needs to be in the same format as a standard XML database change log, something like this:

The db.changelog-master.xml is the ChangeLog you pass to all Liquibase calls.

  1. Final step , Add database properties into application properties file in Spring Boot project

After these required configuration steps, whenever we run Spring boot project, Liquibase will automatically track and deploy database changes.

I hope this article was helpful in integrating Liquibase with Spring boot project .

Leave a Reply

Your email address will not be published. Required fields are marked *