How to add changelog to database changes in java | Liquibase spring boot tutorial

We have many source code repositories like git and svn for managing and tracking code changes to application developed using programming languages.

Have you ever thought of how do you manage and track and apply database schema changes? This post explains about tool called liquibase, an opensource library to track and make database changes in single place.

Liquibase maintains and track changes in scripts or changelog .

This changelog are configured in XML,JSON,YAML and SQL. Features:

  • Supports all major databases
  • database difference report generate
  • Database schema chaneglog
  • Database migrations are made with this tools

How to Integrate Liquibase in Spring Boot app?

This tutorial does not cover how to create an spring boot app.

Create a Changelog file

This .yaml contains list of change logs defined in xml or any formats liquibase/db.changelog-master.yaml:

  - include:
      file: liquibase/create-user.xml
  - include:
      file: liquibase/02-insert-users.xml

Here is an code tracking changes for table schema definition in Liquibase for creating User. liquibase/create-user.xml:

<?xml version="1.0" encoding="UTF-8"?>


  <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 name="name" type="varchar(100)">
        <constraints nullable="false" unique="true"/>
        <column name="role" type="varchar(100)">
        <constraints nullable="false"/>


Here is an code tracking changes for insert initial user data


<?xml version="1.0" encoding="UTF-8"?>


  <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 tableName="users">
      <column name="id" valueNumeric="2"/>
      <column name="name" value="Andrew"/>
      <column name="role" value="sales"/>
    <insert tableName="users">
      <column name="id" valueNumeric="3"/>
      <column name="name" value="John"/>
      <column name="role" value="marketing"/>
    <insert tableName="users">
      <column name="id" valueNumeric="4"/>
      <column name="name" value="Denv"/>
      <column name="role" value="admin"/>

Let’s add liquibase maven dependency into pom.xml


If you want to create an database during maven install, you can configure below settings.


If you are using gradle build tool, you can use below dependency

dependencies {
    classpath 'org.liquibase:liquibase-core:4.4.0'
    classpath "org.liquibase:liquibase-gradle-plugin:2.0.4"

We have a user table in database which contains id,name and role.

Once build dependencies are installed and configured, Spring boot app automatically run scripts in db tracking changes.

This configure and run database schema changes at startup

How to disable liquibase at startup in Spring boot application?

Spring boot configurations are maintained in

liquibase.enabled=false or 

These properties can be enable or disabled to run liquibase at startup. liquibase.enabledproperty used in Spring 4.x.x versions spring.liquibase.enabled property used for Spring 5.x.x versionx Spring

difference between Liquibase and FlyWay tools

Let’s see the comparison of liquibase and flyway database.

Liquibase and Flyway are opensource library tools and these are used as database migration tools

Liquibase   Flyway  
Compare database versions are supported Comparison of databases are not supported
Rollback database supported Rollback supported in Paid version
Changes are defined XML,SQL,JSON and YAMl Changes are defined SQL only
Easy to manage changes It is complex compared with Liquibase
Dashboard to track changes and view Dashboard not supported
Dry Runs like in Git are supported dry runs are supported in Paid version
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.