How to add changelog to database changes in java | Liquibase spring boot tutorial
We utlized source code repositories like git and svn to manage and track code changes in applications developed using various programming languages. Have you ever pondered how to manage, track, and implement database schema changes?
This post introduces a tool called Liquibase, an open-source library designed to track and implement database changes from a centralized single place.
Liquibase effectively maintains and tracks changes through scripts or a changelog. This changelog can be configured in XML, JSON, YAML, or SQL.
Features:
- Supports all major databases.
- Generates database difference reports.
- Manages database schema changelogs.
- Facilitates database migrations through these tools.
How to Integrate Liquibase in a Spring Boot App?
This tutorial assumes you already have a Spring Boot application set up.
Create a Changelog file
Create a .yaml
file containing a list of change logs defined in XML or any other format.
Here’s an example of liquibase/db.changelog-master.yaml
:
databaseChangeLog:
- include:
file: liquibase/create-user.xml
- include:
file: liquibase/02-insert-users.xml
Below is the code tracking changes for the table schema definition in Liquibase for creating a User
liquibase/create-user.xml:
<?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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="01" author="Kiran">
<createTable tableName="users"
remarks="Table to store all user information">
<column name="id" type="int" autoIncrement="true">
<constraints nullable="false" unique="true" primaryKey="true"/>
</column>
<column name="name" type="varchar(100)">
<constraints nullable="false" unique="true"/>
</column>
<column name="role" type="varchar(100)">
<constraints nullable="false"/>
</column>
</createTable>
</databaseChangeLog>
And here is the code tracking changes for inserting initial user data:
liquibase/insert-user.xml:
<?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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="01" author="Kiran">
<comment>user insertion data and changelog</comment>
<insert tableName="users">
<column name="id" valueNumeric="1"/>
<column name="name" value="Eric"/>
<column name="role" value="admin"/>
</insert>
<insert tableName="users">
<column name="id" valueNumeric="2"/>
<column name="name" value="Andrew"/>
<column name="role" value="sales"/>
</insert>
<insert tableName="users">
<column name="id" valueNumeric="3"/>
<column name="name" value="John"/>
<column name="role" value="marketing"/>
</insert>
<insert tableName="users">
<column name="id" valueNumeric="4"/>
<column name="name" value="Denv"/>
<column name="role" value="admin"/>
</insert>
</changeSet>
</databaseChangeLog>
Now, let’s add Liquibase Maven dependency into pom.xml:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
To create a database during Maven install, configure the following settings:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>4.4.0</version>
<configuration>
<changeLogFile>liquibase/db.changelog-master.xml</changeLogFile>
<driver>com.mysql.jdbc.Driver</driver>
<url>${jdbc.url}</url>
<username>${jdbc.username}</username>
<password>${jdbc.password}</password>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
For Gradle build tool users, use the following dependency:
dependencies {
classpath 'org.liquibase:liquibase-core:4.4.0'
classpath "org.liquibase:liquibase-gradle-plugin:2.0.4"
}
Assuming you have a user table in the database with id, name, and role columns,Once build dependencies are installed and configured, the Spring boot app allows Liquibase will automatically run the scripts tracking changes in the database at startup.
This configure and run database schema changes at startup
How to Disable Liquibase at Startup in a Spring Boot Application?
In application.properties
, you can control Liquibase startup behavior.
liquibase.enabled=false
or
spring.liquibase.enabled=false
These properties can be enabled or disabled to run Liquibase at startup. liquibase.enabled
is used in Spring 4.x.x versions, while spring.liquibase.enabled
is used for Spring 5.x.x versions.
difference between Liquibase and FlyWay tools
Let’s see the comparison of liquibase and flyway databases.
Liquibase and Flyway are opensource library tools and these are used as database migration tools These are the key differences between Liquibase and Flyway
Liquibase | Flyway |
---|---|
Supports comparing database versions | Does not support comparing databases |
Supports rollback database changes | Rollback is supported in the paid version |
Allows changes defined in XML, SQL, JSON, YAML | Changes are defined in SQL only |
Easy to manage changes | Relatively complex compared to Liquibase |
Provides a dashboard to track changes and views | Does not support a dashboard |
Supports dry runs similar to Git | Dry runs are supported in the paid version |