Working with Liquibase

Tango Charlie
3 min readOct 21, 2023

--

How to use liquibase effectively to manage database changes and versioning

Working with liquibase

Introduction:
Liquibase is a tool used in software development to manage and version database schemas. It allows developers to define and track changes to the database over time, ensuring that everyone working on the project is using the same database structure. Think of it as a way to keep track of changes made to a database, so developers can collaborate effectively and maintain a consistent and reliable database structure as the application evolves.

How do I set it up in springboot?

Working springboot service folder structure

Step 1: Add Liquibase Dependency

In your build.gradle file, ensure you have added the Liquibase dependency:

implementation 'org.liquibase:liquibase-core'

Along with liquibase, you will need to follow the below dependencies to get it working quickly

 implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.liquibase:liquibase-core'
implementation 'mysql:mysql-connector-java:8.0.23'

compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

Below is my effective build.gradle file

plugins {
id 'java'
id 'org.springframework.boot' version '3.1.5'
id 'io.spring.dependency-management' version '1.1.3'
}

group = 'com.sp.liq.learn'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.boot:spring-boot-starter-web'

implementation 'org.liquibase:liquibase-core'
implementation 'mysql:mysql-connector-java:8.0.23'

compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

tasks.named('test') {
useJUnitPlatform()
}

Below is my application.yml file. If you give datasource and liquibase details in the below format, upon springboot application startup, springboot will create a liquibase bean that will be used to execute your changelog scripts against the database.

spring:
application:
name: SP liqubase POC
datasource:
url: jdbc:mysql://localhost:3306/sp_liquibase_poc?createDatabaseIfNotExist=true&useLegacyDatetimeCode=false&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
liquibase:
enabled: true
change-log: classpath:/db/changelog/db.changelog-root.xml

Under the resources/db/changelog folder, create a master changelog file. In my case, I have named this file as `db.changelog-root.xml` . This is the file where Liquibase starts its execution. Files listed in this file using the include attribute will be executed by Liquibase one by one.

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

Now you have to create a separate change log file for each change that you want to apply to the database.

In my case, I added one table creation changes to file 20231021_changes.xml and then included this file in db.changelog-root.xml file

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd">
<changeSet author="codemonk" id="13243" >
<createTable tableName="person">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true" unique="true"/>
</column>
<column name="name" type="VARCHAR(255)" />
</createTable>
</changeSet>

</databaseChangeLog>

Now, run your springboot application; if the schema is not there, Liquibase will create one for you. If it is already there, it will use it to run the changelog scripts. After springboot application startup, you should be able to see the table in your database.

--

--

Tango Charlie

I am a seasoned Java developer with a diverse skillset that encompasses Java, Spring Boot, microservices, SQL, Kubernetes, Docker, AWS, Kafka and system design.