Liquibase with Spring Boot

Data centre
Reading Time: 3 minutes

The purpose of this blog is to show you the process of using Liquibase as a piece of your Spring Boot workflow. Springboot makes it easy to create standalone, production-maven Spring-based applications.

Introduction

Liquibase is an open-source database that has an independent library for tracking, managing, and applying database schema changes. Liquibase was started in 2006 and it is used to allow easier tracking of database changes, especially in an agile software development environment.

Prerequisites

Make sure that you have  Java Development Kit (JDK 8, 11, or 16).

Use of Liquibase with Spring Boot as a Maven Project

Use Liquibase with spring-boot to create and configure single spring applications and automate your database updates. Spring-boot with build systems like Maven permits you to create Java applications initiated by running java -jar or war deployments.

The Liquibase Spring Boot integration makes the application database updated together with the application code by applying Spring Boot auto-configuration and features.

To use Liquibase and Spring Boot:

  1. Firstly, install a maven project and add it to your system.
  2. Make sure you have Java Development Kit (JDK 8, 11, or 16).
  3. Generate a basic maven project by using your IDE :
  • If you already have an existing spring-boot project, then add the liquibase-core dependency on your project pom.xml.
  • To dynamically create a new spring-boot project, follow the Spring Boot Getting Started documentation.
  • To create a basic spring-boot project, you can also use a web-based service that is called Spring Initializr.

Insert the below information in Spring Initializr.

  • Project Type: Maven
  • Language Used: Java
  • Spring-Boot: the version you need
  • Project Description :
    • Group: com. knoldus.liquibase
    • Artifact: spring-boot-liquibase-example project
    • Name: spring-boot-project
    • Description: Liquibase-spring-boot project example
    • Package name: com.knoldus.spring-boot.liquibase-example project
    • Packaging Type: Jar
    • Java version: 8, 11, or 16
  • Dependencies: Spring Data JPA and Liquibase Migration. This service will add our database driver dependency and any developer tool.
  • Now click on  GENERATE to download your created project template as a .zip file.

After selecting the above options, the project window needs to look similar to the below screenshot:

Finally, Click GENERATE to upload your project template as a zip file. After you require to extract it and open it in your IDE.

Run the Liquibase database with Spring Boot and the Maven project

  1. Open the existing Spring Boot application.properties file. To get the file, traverse to src/main/resources/application.properties.
  2. Add the following properties to run Liquibase migrations. Now update the values depending on your database prerequisite:
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase;

DB_CLOSE_ON_EXIT=FALSE

spring.datasource.username = ${dbName}

spring.datasource.password = ${dbPass}

spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
  1. Create a new text file known as pom.xml or utilize the pom.xml file created for your project using the Spring Initializr.
  2. Specify attributes in your pom.xml based on the example:
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 

https://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<parent>

		<groupId>org.springframework.boot</groupId>

		<artifactId>spring-boot-starter-parent</artifactId>

		<version>2.7.4</version>

		<relativePath/> <!-- lookup parent from repository -->

	</parent>

	<groupId>com.knoldus</groupId>

	<artifactId>spring-boot-liquibase-example</artifactId>

	<version>0.0.1-SNAPSHOT</version>

	<name>spring-boot-liquibase-example</name>

	<description>Demo project for Spring Boot</description>

	<properties>

		<java.version>11</java.version>

	</properties>

	<dependencies>

		<dependency>

			<groupId>org.springframework.boot</groupId>

			<artifactId>spring-boot-starter-data-jpa</artifactId>

		</dependency>

		<dependency>

			<groupId>org.springframework.boot</groupId>

			<artifactId>spring-boot-starter-jdbc</artifactId>

		</dependency>

		<dependency>

			<groupId>org.liquibase</groupId>

			<artifactId>liquibase-core</artifactId>

		</dependency>

		<dependency>

			<groupId>mysql</groupId>

			<artifactId>mysql-connector-java</artifactId>

			<scope>runtime</scope>

		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>

			<artifactId>lombok</artifactId>

			<optional>true</optional>

		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>

			<artifactId>spring-boot-starter-test</artifactId>

			<scope>test</scope>

		</dependency>

	</dependencies>

	<build>

	  <plugins>

	    <plugin>

		<groupId>org.springframework.boot</groupId>

		 <artifactId>spring-boot-maven-plugin</artifactId>

		  <configuration>

                     <excludes>

			<exclude>

			   <groupId>org.projectlombok</groupId>

			   <artifactId>lombok</artifactId>

			</exclude>

		     </excludes>

		  </configuration>

	     </plugin>

	  </plugins>

	</build>

</project>

Setting your changelog file :

  • Create a changelog file: src/main/resources/db/changelog/db.changelog-master.xml. Liquibase is also keep up the .sql, .yaml, or .json changelog formats. To get more information, see Introduction to Liquibase and Getting Started with Liquibase.
  • Add the following code snippet to your changelog file, including changesets:
<?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="Sankata">

    <createTable tableName="books"

      remarks="A table to contain all books">

      <column name="id" type="int" autoIncrement="true">

        <constraints nullable="false" unique="true" primaryKey="true"/>

      </column>

      <column name="name" type="varchar(255)">

        <constraints nullable="false" unique="true"/>

      </column>

      <column name="author" type="int">

        <constraints nullable="false"/>

      </column>

    </createTable>

    <createTable tableName="authors"

      remarks="A table to contain all the authors">

      <column name="id" type="int" autoIncrement="true">

        <constraints nullable="false" primaryKey="true"/>

      </column>

      <column name="name" type="varchar(100)">

        <constraints nullable="false"/>

      </column>

    </createTable>

    <addForeignKeyConstraint baseTableName="books" baseColumnNames="author"

      constraintName="author_fk"

      referencedTableName="authors" referencedColumnNames="id"/>

  </changeSet>

</databaseChangeLog>

To run the first migration with the below command

mvn compile package

Written by 

He is a Software Consultant at Knoldus Inc. He has done B.Tech from Dr. APJ Kalam Technical University Uttar-Pradesh. He is passionate about his work and having the knowledge of various programming languages like Java, C++, Python. But he is passionate about Java development and curious to learn Java Technologies. He is always impatient and enthusiastic to learn new things. He is good skills of Critical thinking and problem solving and always enjoy to help others. He likes to play outdoor games like Football, Volleyball, Hockey and Kabaddi. Apart from the technology he likes to read scriptures originating in ancient India like Veda,Upanishad,Geeta etc.