Implementing Liquibase with Spring Boot and H2


Welcome to a journey of seamless database version control and schema management with our Git Repository. In this project, we marry the simplicity of Spring Boot with the efficiency of the H2-embedded database, all orchestrated by Liquibase.

Source Code https://github.com/pbkn/H2-Liquibase

Key Elements: pom.xml Dependencies

		<dependency>
			<groupId>org.liquibase</groupId>
			<artifactId>liquibase-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>

Our project hinges on essential dependencies, crucial for managing Liquibase commands and the H2 datasource within Spring Boot. Check out these dependencies in our pom.xml snippet.

Configuring application.properties: The Heartbeat of Our Project

spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:liquibaseDB
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=pass
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
# default path: h2-console
spring.h2.console.path=/h2
spring.jpa.hibernate.ddl-auto= update
spring.jpa.show-sql=true
spring.liquibase.change-log=classpath:/db/changelog/db-changelog-root.xml

The application.properties file is where the magic begins. It’s the central hub that powers our H2 in-memory database. Here’s a glimpse of the configurations that bring our application to life. Notice how the spring.liquibase.change-log property skillfully directs Liquibase to the root XML for database alterations.

Mapping the Journey: db-changelog-root.xml

<?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"
        xmlns:pro="http://www.liquibase.org/xml/ns/pro"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
                      http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd
                      ">
    <!-- preserving order of file execution by arranging them in folders-->
    <includeAll path="/db/changelog/release"/>
    <includeAll path="/db/changelog/data/"/>
</databaseChangeLog>

This XML file is the roadmap of our database evolution, ensuring that SQL files are executed in perfect harmony. It’s crucial to maintain the order of file execution; otherwise, prepare for a bumpy ride with errors in your Unit Tests.

Note: If the order of files is misaligned, say you are executing ALTER TABLE before CREATING TABLE then the DB changes will not be implemented and throw an error in Unit Test.

Witnessing Liquibase in Action: Run H2LocalServerApplication.java

Launch the application and watch Liquibase choreograph the database changes. The logs will tell you the whole story.

Logs of liquibase execution while starting the app
2023-12-30T00:21:17.855+05:30 INFO 27092 --- [ restartedMain] liquibase.database : Set default schema name to PUBLIC
2023-12-30T00:21:18.606+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Reading resource: db/changelog/release/function/random_function.sql
2023-12-30T00:21:18.627+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Reading resource: db/changelog/release/person_schema.sql
2023-12-30T00:21:18.635+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Reading resource: db/changelog/data/init_data.sql
2023-12-30T00:21:18.733+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Creating database history table with name: PUBLIC.DATABASECHANGELOG
2023-12-30T00:21:18.945+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Reading from PUBLIC.DATABASECHANGELOG
2023-12-30T00:21:19.030+05:30 INFO 27092 --- [ restartedMain] liquibase.lockservice : Successfully acquired change log lock
2023-12-30T00:21:19.033+05:30 INFO 27092 --- [ restartedMain] liquibase.command : Using deploymentId: 3875879032
2023-12-30T00:21:19.037+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Reading from PUBLIC.DATABASECHANGELOG
Running Changeset: db/changelog/release/function/random_function.sql::1::pbkn
2023-12-30T00:21:21.219+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Custom SQL executed
2023-12-30T00:21:21.222+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : ChangeSet db/changelog/release/function/random_function.sql::1::pbkn ran successfully in 2140ms
Running Changeset: db/changelog/release/person_schema.sql::1::pbkn
2023-12-30T00:21:21.247+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Custom SQL executed
2023-12-30T00:21:21.248+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : ChangeSet db/changelog/release/person_schema.sql::1::pbkn ran successfully in 5ms
Running Changeset: db/changelog/data/init_data.sql::1::pbkn
2023-12-30T00:21:21.267+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Custom SQL executed
2023-12-30T00:21:21.268+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : ChangeSet db/changelog/data/init_data.sql::1::pbkn ran successfully in 14ms
Running Changeset: db/changelog/data/init_data.sql::2::pbkn
2023-12-30T00:21:21.283+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : Custom SQL executed
2023-12-30T00:21:21.285+05:30 INFO 27092 --- [ restartedMain] liquibase.changelog : ChangeSet db/changelog/data/init_data.sql::2::pbkn ran successfully in 4ms
2023-12-30T00:21:21.299+05:30 INFO 27092 --- [ restartedMain] liquibase.util : UPDATE SUMMARY
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : Run: 4
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : Previously run: 0
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : Filtered out: 0
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : -------------------------------
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : Total change sets: 4
2023-12-30T00:21:21.300+05:30 INFO 27092 --- [ restartedMain] liquibase.util : Update summary generated
2023-12-30T00:21:21.303+05:30 INFO 27092 --- [ restartedMain] liquibase.command : Update command completed successfully.
Liquibase: Update has been successful. Rows affected: 6
2023-12-30T00:21:21.307+05:30 INFO 27092 --- [ restartedMain] liquibase.lockservice : Successfully released change log lock
2023-12-30T00:21:21.308+05:30 INFO 27092 --- [ restartedMain] liquibase.command : Command execution complete

Stepping into the H2 Console Universe

Access the H2 console at http://localhost:8080/h2/, and you’ll see the fruits of Liquibase’s labor. Tables created and managed with precision!

Note: Enter the username and password mentioned in the application.properties file

The SQL Spotlight: Queries That Reveal All

Execute these SQL queries to unveil the intricate workings of Liquibase. Witness the tables and contents sculpted by Liquibase’s masterful hands.

SELECT * FROM DATABASECHANGELOG;

SELECT * FROM DATABASECHANGELOGLOCK;

SELECT * FROM PERSON;

Elevate Your Skills: Become a Liquibase Certified Practitioner

Did you know Liquibase offers a FREE Liquibase Certified Practitioner Exam (LBF102)? Seize the opportunity to certify your expertise!