Evolving Database using Spring Boot and Liquibase

ebook-2
Reading Time: 3 minutes

Introduction

In this Blog, we will see an example of evolving database using Spring Boot and Liquibase with YAML and SQL configuration. Here we will learn how to build applications using maven build tools.

Liquibase is an open-source library for tracking, managing, and deploying database changes that can be used for any database. It helps you create the schema, run them during deployment, and also helps you write automated tests so that your changes will work in production.

Create Project

We need to create maven based project.

If you are creating maven based project then add the following dependencies.

Create Changelog File

Create a YAML file called db.changelog-master.yaml under the src/main/resources/db folder. It will cover all the changelogs written in different files. The complete master changelog file content is given below:

Notice in the above YAML file, we have not specified any endDelimiter for changeSet ids insertTableAddresses and insertTableUsers because the default end delimiter is ;.

Now create below SQL file 01-create-users-and-addresses-schema.sql under src/main/resources/db/changelog/scripts to create tables in the MySQL database:

NowcreatebelowSQLfile 02-insert-data-addresses.sql under src/main/resources/db/changelog/scripts folder to insert data into the ADDRESSES table:

Now create below SQL file 02-insert-data-users.sql under src/main/resources/db/changelog/scripts to insert data into the USERS table:

Application Properties

Create file src/main/resource/application.properties to load the database changelog file during Spring Boot application startup. Here we configure the database connection in this file.

The default location of the changelog master file is classpath:/db/changelog and Liquibase searches for a file db.changelog-master.yaml. So we need to declare the location of the changelog-master file.

In the Spring Boot application the key liquibase.change-log does not work, so you need to use spring.liquibase.changeLog.

Create Main Class

Here is the main application in order to start the application and the creation and insertion of the above table into the database will be occurring during the application execution.

Testing the Application

After running the project it will have the following output:

Conclusion

You will find tables created in the database. The two rows are inserted into addresses and two rows are inserted into users’ tables. Also, there are three rows inserted into the table DATABASECHANGELOG and the row identifies all details about the executed file. We will also find one row inserted into the table DATABASECHANGELOGLOCK and this row identifies whether the current operation holds a lock on changesets or not.

Written by 

Deepak kumar is a Software Intern at Knoldus Inc. He has done Post Graduation from Ajay Kumar Garg Engineering College Ghaziabad. He has the knowledge of various programming languages. He is passionate about Java development and curious to learn Java Technologies. He is a quick learner, problem solver and always enjoy to help others. His hobbies playing Cricket and watching Hollywood movies.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading