Spring — Liquibase Quickstart
Setting up Liquibase in spring
Whats about Liquibase ?
Liquibase in simple terms is an open source library allows developers to track, version, and deploy database schema changes. Using Liquibase you can define database changes in a human-readable, database-independent XML or YAML format, and Liquibase will apply those changes to your database.It suits well since its Database independent and can easily be integrated with any build tools like maven or gradle and CI/CD pipeline to automate process of changing or creating database accross various enviorments. Liquibase also supports rollbacks and validation of database changes.
Changelogs and changesets
Changelogs contains series of database changes that has to be applied by Liquibase. It would mostly be of YAML or XML. it could be any command which has to be run on a database engine that ranges from creating or modifying tables, indexes, and foreign keys.Each change is defined as a changeSet in the changelog file, which includes a unique ID, author, and a description of the change. Liquibase uses these attributes to keep track of which changes have already been applied to the database.Once the changelog file is executed and applied to a database it automatically keeps track on the subsequent changes that is made and updates things accordingly.
Get your hands dirty now 🛠👨🏾💻
Now lets create a sample Spring project and an inmemory database to test the same. Here i will be using java but again its language independet , so feel free to try it out with any language that you have hands on !
I have chosen an in memory H2 Database for this example however liquibase also provide this internally in their depandancy.
So my model class would be very simple with some of laptop properties like below :
Before we goahad with change log and change set ,lets quickly create a repository interface for the same.
Define the database schema — The change log files which could either be XML or YAML , but for this exaple i will be using XML .Lets a master change log which would be the entry point of liquibase for the execution of its queries and in the master changelog i would be providing the table files along with their versions. The folder structure for placing the files would be something like this :
Also you can see the logo of xml files getting changed to how liquibase logo is 🙂
MasterChangeLog :
<?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.5.xsd">
<include file="v1-add-laptop-table.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
DatabaseChangeLog
<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.5.xsd">
<changeSet author="gokulmenon" id="create laptoptable">
<comment>create generated_documents table</comment>
<sql>
CREATE TABLE laptop
(
id BIGSERIAL PRIMARY KEY,
model_name VARCHAR NOT NULL,
manufacturer VARCHAR NOT NULL,
storage VARCHAR NOT NULL,
colour VARCHAR
);
</sql>
</changeSet>
</databaseChangeLog>
You need to configure Liquibase to work with H2 database by setting up the appropriate database connection details in application.properties file
liquibase.change-log = classpath:db/changelog/changelog-master.xml
liquibase.url= jdbc:h2:mem:testdb
liquibase.password=password
liquibase.user=sa
liquibase.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Now lets navigate to the test package and test this functionality quicly with a simple test :
Now if i run and query my logs we can see that liquibase is create a table on the go and inserting the fields which we have mentioned and also querying them while fetching it :
After running the changelog, you can verify that the data has been inserted correctly into the H2 database by querying the database using a tool such as H2’s built-in console, or by using a JDBC driver to connect to the database from your application code. This approach can be useful for managing small to medium-sized datasets or for bootstrapping a new database with initial data.
You can checkout the entire project in my github. Thanks for reading my post! If you’d like to connect with me and stay up-to-date on my latest articles, please feel free to follow me on Twitter and connect with me on LinkedIn. I’m always looking to engage with fellow tech enthusiasts and discuss the latest industry trends ! Drop me a message and let’s connect.
Happy coding ☕️ 🎧 ❤️