How to use Liquibase with Spring Boot and Maven

Posted By : Abhishek Purohit | 28-Jun-2021

Overview

Liquibase is a migration tool that provides services like  creating database schema, testing the database to ensure reliability and manage revisions of your database schema scripts. It supports multiple databases and file formats for defining the DB structure.

Prerequisites

  1. JDK 8+
  2. Maven (Any latest version ).

Procedure

Below are the steps to create a Liquibase project with

  1. Create a new project folder with a good name.
  2. In your Project folder, create a new .properties file for liquibase configuration.
  3. The liquibase.properties file should be edited in order to add the following properties:
changeLogFile: dbchangelog.xml
url: jdbc:h2:mem:my_db;MODE=Mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false;INIT=CREATE SCHEMA IF NOT EXISTS my_db\\;SET SCHEMA my_db
classpath: ../../Liquibase_Drivers/postgresql-42.2.8.jar

The changeLogFile property points to the changelog file that we will create later in this tutorial. Since the changelog is in the home directory, there is no need to specify a path to it. If the changelog is located somewhere else, then add a relative path.

Windows example: changeLogFile: ..\\<path to changelog>\\changelogs\\dbchangelog.xml

Linux example: ../<path to changelog>/changelogs/dbchangelog.xml

The url property is your database url. In this example, we use an in-memory h2 database. If there is a user and password associated with the database, then add the username and password properties to the properties file as well.

If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties file:

liquibaseProLicenseKey: <paste license key>
  1. Create a new text file in your "project name" folder and name it dbchangelog.xml. The changelog files contain a sequence of changesets, each of which makes small changes to the structure of your database.

Note: To generate a changelog, instead of creating an empty changelog file, you can also use an existing database. In this tutorial, you will manually add a single change. To add this change, open the dbchangelog.xml file and update the changelogfile with the following code snippet:

<?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.8.xsd">
	</databaseChangeLog>

 

  1. Add a changeset to the changelog. The changesets are uniquely identified by author and id attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In the dbchangelog.xml file, add a new “department” create table changeset as follows:
 <?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.8.xsd">

    <changeSet  id="1"  author="bob">
        <createTable  tableName="department">
            <column  name="id"  type="int">
                <constraints  primaryKey="true"  nullable="false"/>
            </column>
            <column  name="name"  type="varchar(50)">
                <constraints  nullable="false"/>
            </column>
		<column  name="active"  type="boolean"
			defaultValueBoolean="true"/>
        </createTable>
   </changeSet>
</databaseChangeLog>

Note: The preceding changeset is XML format. The corresponding SQL statement looks like the following:

  CREATE  TABLE  "department"
(  	"id"  number(*,0),
	"name"  VARCHAR2(50  BYTE),
	"active"  NUMBER(1,0)  DEFAULT  1
);
  1. In your "project name" folder create a text document named pom.xml.
  2. Open the pom.xml file and update it with the following code snippet:
 <?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.8.xsd">

    <changeSet  id="1"  author="bob">
        <createTable  tableName="department">
            <column  name="id"  type="int">
                <constraints  primaryKey="true"  nullable="false"/>
            </column>
            <column  name="name"  type="varchar(50)">
                <constraints  nullable="false"/>
            </column>
		<column  name="active"  type="boolean"
			defaultValueBoolean="true"/>
        </createTable>
   </changeSet>
</databaseChangeLog>
  1. Download and unzip the src.zip to your "project name" directory. The file contains java scripts in order to run a Spring application.
  2. You need to open the command prompt and go to the "project name" directory.
  3. To compile and test your Spring Boot application code, run the following command:
mvn package
  1. Run the following command to deploy your changes:
mvn liquibase:update
  1. You can check in the console for the executed sql script:
CREATE TABLE department (id INT NOT NULL, name VARCHAR(50) NOT NULL, active BOOLEAN DEFAULT TRUE, CONSTRAINT PK_DEPARTMENT PRIMARY KEY (id))
Table department created.

About Author

Author Image
Abhishek Purohit

He is Java Developer with core programming skills with the knowledge of Spring MVC & Spring Boot framework .He likes to be involved in strategy building games such as chess.He also likes to play Massive Multiplayer Online Games.

Request for Proposal

Name is required

Comment is required

Sending message..