Using Liquibase in Spring Boot application

In the previous post, I showed you how to use database migration with Liquibase in Spring MVC. So what about Spring Boot? In this tutorial, I will show you how to do this! It’s really simple!

First, I will create a new Spring Boot project:

Using Liquibase in Spring Boot application

with Liquibase Migration, Spring Data JDBC and PostgreSQL dependencies as follows:

Using Liquibase in Spring Boot application

The purpose I use Spring Data JDBC is to configure the dataSource for Liquibase and I will use PostgreSQL for this tutorial.

Result:

Using Liquibase in Spring Boot application

If you pay attention, you will see that the Spring Starter Project in the Spring Tool Suite has generated the src/main/resources/db/changelog directory. This is the directory containing the changelog file of Liquibase.

As you all know, for Liquibase to work, we need to declare the dataSource and the path to the changelog file, so now we will define them one by one.

We will define the dataSource information for Liquibase in the application.properties file.

If you run this application now, you will see the following error:

By default, Spring Boot will read the Liquibase changelog file defined in the YAML file, in the src/main/resouces/db/changelog/ directory, named db.changelog-master.yaml. You can change this default configuration using property spring.liquibase.change-log.

I will define a changelog file with XML format in the directory src/main/resources/db/changelog with the following content:

Configure the spring.liquibase.change-log property in the application.properties file:

Now, run the application again, check the database, you will see the following results:

Using Liquibase in Spring Boot application

Add Comment