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 the application developed using programming languages.

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

Liquibase maintains and tracks changes in scripts or changelog.

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

  • Supports all major databases

  • database difference report generate

  • Database schema chaneglog

  • Database migrations are made with these tools

How to Integrate Liquibase in Spring Boot app?

This tutorial does not cover how to create a 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:

databaseChangeLog:
  - 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"?>

<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>

Here is a 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>

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>

If you want to create an database during maven install, you can configure below 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>

If you are using the 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 the database which contains id, name, and role.

Once build dependencies are installed and configured, the Spring boot app automatically runs 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 application.properties

liquibase.enabled=false or
spring.liquibase.enabled=false

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

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

LiquibaseFlyway
Compare database versions are supportedComparison of databases are not supported
Rollback database supportedRollback supported in Paid version
Changes are defined XML, SQL,JSON, and YAMlChanges are defined SQL only
Easy to manage changesIt is complex compared with Liquibase
Dashboard to track changes and viewDashboard not supported
Dry Runs like in Git are supporteddry runs are supported in Paid version