Liquibase — Exploring with Spring boot and Maven

Vignesh Ravichandran
3 min readMay 6, 2022
title image

This is a short article about how we could use liquibase in a spring boot project and how we can use it to generate changelogs automatically

Just to clarify some technical things
DB used: Postgres 14(latest)
Springboot : 2.6.3
Java: 1.8x

I have compiled how we can use liquibase under various scenarios

Scenario 1: Liquibase for DB management

This is one of the most common use cases of liquibase. when we normally configure a database with liquibase we get two tables created automatically named datachangelog and databasechangeloglock

with changelog we can find which developer has made the script along with the script executed time and comments

databasechangelogtable

Incase if we want to lock the DB and prevent further script execution, we can use the databasechangeloglock table

databasechangeloglock

Now, let’s jump into our first changelog execution along with Spring boot

A Changelog is a file which comprises of scripts which a database tool can execute and make some changes to an existing database

To create changelog we have automated ways too(which will be dealt in scenario 2)

Creating a springboot App :

The first step is to create a simple Springboot app., For this please use the website https://start.spring.io/ and select maven, Java 1.8 and then under dependencies add liquibase, Spring data JPA and postgres

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

Now the generated zip file should have these dependencies., If not please add them in the pom.xml

Creating a simple Changelog

Once you’ve setup your project in your favorite IDE(preferably intellij IDEA), then run your Springboot application to check things are fine.

Create a file named liquibase-changelog.xml under the folder resources/db/changelog/

Now this is your master changelog file which will act as a manager for all the liquibase transactions in your API. Open your application properties and add a reference to this file

spring.liquibase.change-log=classpath:/db/changelog/liquibase-changelog.xml

We have made 90% of the work needed to complete this task

Master Changelog file

We are using XML format of liquibase changelogs, if you are not comfortable feel free to use with JSON/ SQL/ YAML .

you have to include the changelogs like the code snippet below

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<include file="classpath:/db/changelog/bank-changelog.xml"/>
</databaseChangeLog>

Insome circumstances these could fail., I will give some reference at the end fot the liquibase page for the xsd and xmlns links

Executable changelog for Table creation

Create a file under the same db/changelog folder to maintain code discoverability. We are going to create a table named bank., for that a reference changelog will be like this. Add some other columns with different datatypes too

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">
<changeSet author="admin" id="1">
<createTable tableName="BANK">
<column name="id" type="INTEGER">
<constraints nullable="false" primaryKey="true" />
</column>
<column name="country" type="VARCHAR(255)" />
<column name="region" type="VARCHAR(255)" />
<column name="currency" type="VARCHAR(255)" />
</createTable>
<rollback>
<dropTable tableName="BANK"/>
</rollback>
</changeSet>
</databaseChangeLog>

The final one pending is to ensure database connections are perfect.,

Database with Springboot

Create a DB named postgres(which might be there by default), use the following code in application properties of our Springboot application

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

Now our application setup for a basic create/update a DB with liquibase is done., when you execute it you should be able to see the empty table named Bank created.

Happy Coding..!

The next scenarios with Liquibase like automated creation of changelog based upon existing database, comparing two databases and comparing an entity and database will be covered in next articles soon.

Reference:https://docs.liquibase.com/concepts/changelogs/working-with-changelogs.html

--

--