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
- JDK 8+
- Maven (Any latest version ).
Procedure
Below are the steps to create a Liquibase project with
- Create a new project folder with a good name.
- In your
Project
folder, create a new.properties
filefor liquibase configuration
. - 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>
- Create a new text file in your
"project name"
folder and name itdbchangelog.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>
- Add a changeset to the changelog. The changesets are uniquely identified by
author
andid
attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end. In thedbchangelog.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
);
- In your
"project name"
folder create a text document namedpom.xml
. - 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>
- Download and unzip the
src.zip
to your"project name"
directory. The file contains java scripts in order to run a Spring application. - You need to open the command prompt and go to the
"project name"
directory. - To compile and test your Spring Boot application code, run the following command:
mvn package
- Run the following command to deploy your changes:
mvn liquibase:update
- 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.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.