Using Liquibase with Springboot and Maven tutorial
When developing software, it is very common to create a database schema during production. Liquibase is a great migration tool that helps with creating the database schema, then running deployment and some testing to the database to ensure that the changes will work in production. The purpose of this tutorial is to demonstrate using Liquibase migration tool for a Java Springboot application with Maven.
Liquibase can be run in a Spring environment by declaring a liquibase.spring.SpringLiquibase bean.
What is Liquibase?
Liquibase provides a great starting point for teams addressing the challenges that come with managing database schema changes.
It has the ability to manage revisions of your database schema scripts. It works across various types of databases, and supports various file formats for defining the DB structure. Liquibase has the ability to roll changes back and forward from a specific point - saving you from the need to know what was the last change/script you ran on a specific DB instance.
What is Springboot?
Springboot is an open source Java-based framework used to create a micro Service. It is developed by Pivotal Team and is used to build stand-alone and production-ready Spring applications.
What is Maven?
Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.
- Install the latest version of Liquibase (These instructions include how to install Java.)
- Download and install Maven.
- Create a new project folder and name it LiquibaseProj.
- In your LiquibaseProj folder, create a plain text document named dbchangelog.xml.
changelog files contain a sequence of changesets, each of which make small changes to the structure of your database. Instead of creating an empty changelog file in step 2, you can also use an existing database to generate a changelog. In this tutorial, you will manually add a single change. To add this change:
- Open the dbchangelog.xml file and update the changelog file with the following code snippet:
- In your LiquibaseProj folder create a plain text document named liquibase.properties.
- Edit the liquibase.properties file to add the following properties:
The changeLogFile property will point to the changelog we have created. 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 a relative path should be added.
changeLogFile: ..\\<path to changelog>\\changelogs\\dbchangelog.xml
../<path to changelog>/changelogs/dbchangelog.xml
The url property is your Database url. In this example we are using an in-memory h2 database. If there is a user and password associated with the database, then the username and password properties should be added to the properties file as well.
Note: 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>
- Adding a changeset to the changelog – Change Sets are uniquely identified by “author” and ”id” attributes. Liquibase attempts to execute each changeset in a transaction that is committed at the end.
In the dbchangelog.xml file line 9 to 20 add a new “department” create table changeset as follows:
Note: This create table change set is XML format. When the
update command is run, the changeset will be used to generate SQL
that is specific to the database platform. In this example, we are using an h2 in-memory database just for an example, and the SQL
generated would look like this:
- In your LiquibaseProj folder create a plain text document named pom.xml.
- Open the pom.xml file and update it with the following code snippet:
- Download and unzip the
src.zipto your LiquibaseProj directory.
In the src directory you will notice the path to your application code: src/main/java/com/application.java and the path to some unit tests: src/test/java/com/applicationTests.java.
- The Springboot Maven plugin has many great features.
- It collects all the jar files in the classpath and builds a single “uber-jar”. This makes it more convenient to execute your service.
- It searches for the “public static void main()” method to flag any classes with that method signature as a runnable class.
- Open the command prompt or Bash. Navigate to the LiquibaseProj directory.
Run the following command:
This command will compile and test your Springboot Application code.
- In the console output you should notice the following SQL executions: For example: 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.
Also, you should see two more tables updated:
- DATABASECHANGELOGtracking table – This table keeps a record of all the changesets that were deployed. This way, next time when you deploy again, the changesets in the changelog will be compared with the DATABASECHANGELOG tracking table and only the new changesets that were not found in the DATABASECHANGELOG will be deployed. You will notice that a new row was created in that table with the changeset information we have just deployed. For this example:
- DATABASECHANGELOGLOCK– This table is used internally by Liquibase to manage access to the changelog table during deployment.
Congratulations! You are now able to use Liquibase to manage both your Springboot application and its database with Maven. There is more information available on the Maven plugin and its usage.