Hello everyone,

Greetings today!

Today you will learn what Liquibase is, how to download and install Liquibase, and how to use Liquibase in your Spring Boot application. 

Let's get started,

What is liquibase?

Liquibase is an open-source library that helps track and execute database changes.

Spring-hibernate has a hibernate.hbm2ddl.auto property with possible values ​​validate, update, create create-drop & none.

In a production environment, you would normally keep the value none which is also the default value. Other values ​​are suitable only for development environments. In the production environment, we create a script that is checked by a DBA and executed manually.

Therefore, libraries like liquibase are used to overcome this manual script execution.

Download & Install Liquibase On Windows

Liquibase can be downloaded from here.

The Pro edition is paid after 30 days free trial.




Once the installer is downloaded, double-click it to complete the installation process.

How to use liquibase with spring boot?

Create a sample spring boot application from Spring Initializr with the following dependency 


Note if you are creating a project manually we need to add the below liquibase dependency 

<dependency>
  <groupId>org.liquibase</groupId>
  <artifactId>liquibase-core</artifactId>
  <version>4.3.3</version>
</dependency>

The next step is to open the generated project in your IDE.

Now as I am using PostgreSQL I will add database-related properties for the same.

  
spring.datasource.url= jdbc:postgresql://localhost:5432/test
spring.datasource.username= add_db_username
spring.datasource.password= add_db_password
spring.datasource.initialization-mode=never
spring.datasource.driverClassName=org.postgresql.Driver

Now we need to specify the location of the Spring Boot changelog file. The default location for this is under db/changelog with the file name db.changelog-master.yaml. Let's add the following property to specify the location of the changelog file.

spring.liquibase.change-log=classpath:liquibase-changeLog.xml

Now on startup of our spring boot application, it will go and scan all changelog in liquibase-changeLog.xml and execute the same automatically.

As it's not good practice to have all our scripts in the same file we will add include all tags in liquibase-changeLog.xml and specify the location of our SQL files, so it will pick all SQL files from that location and execute them.

So below is the code of liquibase-changeLog.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/
    dbchangelog-4.3.xsd">

    <includeAll path="/liquibase/changesets/"/>

</databaseChangeLog>

As specified under include all tag liquibase will pick all scripts from folder /liquibase/changesets/  and execute it on startup 

So let's create folders as mentioned above and add 1 SQL file with the name 000_student_table.sql under the same.

--liquibase formatted sql

--changeset author.name:00 create-student-table

CREATE TABLE public.student
(
    id bigint GENERATED BY DEFAULT AS IDENTITY NOT NULL,
    mobile_number character varying(20),
    name character varying(255),
    CONSTRAINT student_pkey PRIMARY KEY (id)
)

So here the first line is --liquibase formatted sql  which is a mandatory comment,

Second-line is --changeset author.name:00 create-student-table  which is recommended format by liquibase, so let's say you have update/insert query for the same table then you can add a new script with 

--changeset author.name:01 create-student-table

So we need to increment it by one, note we can have multiple scripts under the same number until the script is executed in the database, once the script is executed we can not do any modification to the existing script nor we can add a new script under the same number, we need to write new scripts only with a new incremented number.

So we are done with all setup we need for liquibase, on starting the spring boot application you will find your scripts are executed automatically.

Also, liquibase maintains 2 tables to track which script it has executed to execute the new script if added on the next restart.

Those tables are

1)databasechangelog

2)databasechangeloglock

Below is the attached project structure for reference 



Feel free to leave a comment if you encounter any issues while implementing the above solutions. We are happy to support you.

Thanks

Enjoy your learning!

Other reference articles

For other spring tutorials refer