Liquibase with Spring Boot

About

Liquibase is a powerful tool that provides a declarative way to manage database schema changes. With Liquibase, schema changes can be defined in a change log file, which can be versioned and tracked just like the application code. This makes it easy to manage database schema changes across different environments, such as development, staging, and production.

Spring Boot has built-in support for Liquibase, which means Liquibase can easily integrate into the Spring Boot application by adding the liquibase-core dependency to the project's Maven or Gradle build file, and configuring it with the necessary properties, such as the JDBC URL, username, and password.

To use Liquibase in a Spring Boot application, create a Liquibase change log file, which defines the changes to be applied to the database schema. It can be in YAML or XML or any other format which is supported, and can include a series of change sets that define the individual changes to be made to the schema.

Overall, using Liquibase as part of a Spring Boot project can help us manage database schema changes more effectively and maintain the consistency of our database schema across different environments.

Init Container vs Main Container wrt liquibase

Init Container

An init container is a separate container that runs and completes before the main application container starts. It is primarily used for performing initialization tasks or pre-requisites required by the main container. In the context of Liquibase, an init container can be used to apply database schema changes or migrations before the main application container starts. This ensures that the database is properly prepared before the application attempts to connect to it. Liquibase can be executed as part of the init container, allowing us to manage database changes as a separate initialization step. In case of any issues with init container, main container won't be starting.

Main Container

The main container refers to the primary application container that runs the actual application logic. In the case of a Spring Boot application, the main container would be responsible for running the Spring Boot application itself including the liquibase changes.

circle-check

Example

Firstly, we will need mysql instance up and running. We will use docker-compose method to bring mysql.

docker-compose.yaml

We will create liquibase-example-service spring boot project.

Add the following dependencies to the pom.xml file

Create the main application.java class

Add the required properties in application.yaml file

Now, let's create changelog files. We will create master changelog file and it will have reference to separate changelog files.

ree

changeLog-master.yaml

add-table-books-04052023.yaml

add-table-users-03052023.yaml

update-table-users-05052023.yaml

Overall, the folder structure will look like below

ree

Build the project with below maven command and run the application

ree
ree
ree

From the above logs, we can see that liquibase has applied the database changes and let's verify from the MySQl Workbench as well.

ree

Files are attached for the reference below.

Last updated