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:

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 an code tracking changes for insert 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 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 application.properties

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

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
THE BEST NEWSLETTER ANYWHERE
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
Subscribe
You'll get a notification every time a post gets published here.