Java4Shell (CVE-2026-114938) is fixed in Liberica JDK

See details
Posts

How to Use Liquibase with Spring Boot 3 and 4

Updated

Catherine Edelveis

33

Liquibase is a database migration tool that allows you to track, apply, and roll back changes to the database schema using declarative changelog files. It integrates with version control systems, ensures automatic and reproducible deployment of migrations across different environments, makes the change history transparent, and works consistently with various databases.

In Spring Boot 4, add spring-boot-starter-liquibase (in Spring Boot 3, liquibase-core). Spring Boot runs the changelog at db/changelog/db.changelog-master.yaml on startup.

This article will guide you through setting up and using Liquibase with a Spring Boot application and Maven. The tutorial uses Spring Boot 4.1.2, Liquibase 5.0.4, and Java 25 (LTS).

You will learn how to:

  • Set up Liquibase for PostgreSQL,
  • Write migration scripts in YAML and SQL,
  • Use preconditions,
  • Perform rollbacks,
  • Use Liquibase diff to compare two versions of a database and create migration scripts automatically,
  • Run database migrations in CI using GitHub Actions.

The code from this article is available on GitHub.

Liquibase Structure and Commands

Liquibase enables you to evolve database schemas using data-agnostic formats: YAML, XML, and JSON. These formats are helpful if you need to manage one schema across several databases. However, you can also write scripts in SQL if you prefer.

To work with Liquibase, you need to familiarize yourself with the following key concepts.

Changelogs

Changelog files are the main building blocks of database migrations. They sequentially describe the history of the database changes. You can write a changelog in any supported format, and Liquibase will detect the format by the file extension.

It is a good practice to maintain a main changelog that includes links to other changelogs with the actual schema changes.

Changesets

Changesets are the atomic units of work included in a changelog. For easier rollback, it is a good practice to specify one schema change per changeset. By default, Liquibase applies each changeset once.

Liquibase records every applied changeset (its id, author, file path, and checksum) in the DATABASECHANGELOG table. The DATABASECHANGELOGLOCK table ensures that only one Liquibase instance migrates the database at a time, for instance, when several app instances start at once. On startup, Liquibase checks the recorded history and applies eligible pending changesets. Attributes such as runOnChange can make an already applied changeset run again.

Change types

Change types are database-independent operations specified in a changeset. They correspond to SQL statements applied to the database. Some examples are createTable, createIndex, addColumn, addForeignKeyConstraint, etc.

Changelog tags

Changelog tags can be described as execution controls. They help to specify when and where a changeset should run. Three types of changelog tags are:

  • Preconditions that help to perform checks against the current database state. For instance, “table X must exist.” If the check fails, Liquibase can halt, skip, or mark the changeset as ‘ran’ as instructed by you.
  • Context tags are boolean flags you attach to changesets, e.g., dev, staging. You pass --context-filter at runtime to select matching changesets alongside those without a context.
  • Labels are similar to context tags but are added to changesets as labels and specified at runtime with the --label-filter option.

Liquibase Commands

Liquibase commands can be classified into six categories:

  • Init to bootstrap Liquibase to a project.
  • Update to apply any pending changes to the target database.
  • Rollback to undo changes by count, tag, or date.
  • Inspection to compare sources and generate diffs.
  • Change tracking to get the deployment status of changes.
  • Utility to manage schema documents and tasks.

Integrate Liquibase into a Spring Boot Application

Prerequisites:

  • Java 25 to build and run the demo. I use Liberica JDK 25 as it is a runtime recommended by Spring.
  • Local PostgreSQL Server or Docker Compose if you want to spin up a database in a container. This demo uses the second option.
  • Your favorite IDE.

I’m going to use a cyberpunk-themed demo. You can clone the finished project or follow the examples incrementally in your own app.

If you use your own project, add the Liquibase dependency for your Spring Boot version:

Spring Boot line

Liquibase integration dependency

4.x

spring-boot-starter-liquibase

3.x

org.liquibase:liquibase-core

For Spring Boot 4 with Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-liquibase</artifactId>
</dependency>

With Gradle:

implementation("org.springframework.boot:spring-boot-starter-liquibase")

Spring Boot manages the versions, so you don't need to specify them.

In the application.yaml file, configure the database connection, disable Hibernate DDL, enable Liquibase and specify the path to the main changelog file:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/neurowatch
    username: neurowatch_user
    password: ${DB_PASSWORD} # read from an environment variable
  jpa:
    # Hibernate is disabled because Liquibase will manage schema
    hibernate:
      ddl-auto: none
    show-sql: true
  liquibase:
    enabled: true
    change-log: classpath:db/changelog/db.changelog-master.yaml

For the cloned demo, set the database password and start the app. Spring Boot starts PostgreSQL through Docker Compose and runs the migrations:

export DB_PASSWORD='local-demo-password'
./mvnw spring-boot:run

Use your chosen local password in place of the example value. If you run PostgreSQL yourself, create the database and user shown above with the same password.

By default, Spring Boot looks for the main changelog at db/changelog/db.changelog-master.yaml on the classpath, so the change-log line only restates the default.

If you added Liquibase when creating the project, Spring created db/changelog directories for you under resources. Otherwise, you need to create them manually.

In the db/changelog directory, create a db.changelog-master.yaml file with the following content:

databaseChangeLog:
  - include:
      file: db/changelog/001-create-tables.yaml

The 001-create-tables.yaml will be our first changelog. Other changelogs will be added to the main changelog in a similar fashion. Note that the include path is resolved from the classpath root (src/main/resources), not the project directory: a path like /src/main/resources/db/changelog/... fails on startup with "file not found in the configured search path."

Spring Boot 4: why your Liquibase migrations don't run

Replace liquibase-core with spring-boot-starter-liquibase.

In Spring Boot 4, LiquibaseAutoConfiguration lives in spring-boot-liquibase. The starter brings in that module and liquibase-core. Older instructions that add only liquibase-core describe the Spring Boot 3 dependency model.

In a Spring Boot 4.1.2 test with only liquibase-core, the application context started without Liquibase log entries or a DATABASECHANGELOG table. Queries failed later because the expected tables had never been created.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-liquibase</artifactId>
</dependency>

If you're midway through a bigger migration, spring-boot-starter-classic brings back all auto-configuration modules, so liquibase-core alone works again. Treat it as an interim step only: the Spring Boot 4.0 Migration Guide recommends eventually moving away from the classic starters. For the rest of the upgrade, see our guide to migrating from Spring Boot 3 to Spring Boot 4.

Configure Liquibase with spring.liquibase.* properties

Property

Default

What it's for

spring.liquibase.change-log

classpath:/db/changelog/db.changelog-master.yaml

Path to the main changelog

spring.liquibase.enabled

true

Switch off migrations on startup, e.g., where a CI/CD pipeline applies them instead

spring.liquibase.contexts

—

Filter context-tagged changesets

spring.liquibase.label-filter

—

Filter labeled changesets

spring.liquibase.default-schema

—

Schema for database objects if it's not the connection's default

spring.liquibase.drop-first

false

Drop the schema before migrating. Never use it in production!

spring.liquibase.url, spring.liquibase.user, spring.liquibase.password

—

Separate connection for migrations

By default, Liquibase migrates the database through the application's primary DataSource, i.e., with the spring.datasource.* settings. Setting spring.liquibase.url or spring.liquibase.user makes Liquibase open its own connection, and any of the three connection properties you leave out is taken from its spring.datasource equivalent. This way, the application can run as a restricted user, while migrations run as the schema owner with DDL privileges.

For test-only seed data, set context: "@test" on the changeset and spring.liquibase.contexts=test in the test environment (see test-only migrations).

We’re all set. It's time to write some migrations!

Write a Changelog File in YAML

The first changelog will describe the initial database schema.

The file starts with a top-level key pointing out that this file is a changelog:

databaseChangeLog:

After that, you add a changeSet entry and specify its id, author, any other required info, and preconditions if any:

databaseChangeLog:
  - changeSet:
      id: 001
      author: cyberjar
      preConditions:

Then, you begin the list of actual operations, a.k.a. the change types. In this case, we will have two change types, createTable and addForeignKeyConstraint. Everything indented under the line with the change type configures that operation:

databaseChangeLog:
  - changeSet:
      id: 001
      author: cyberjar
      changes:
        - createTable:

The whole file:

databaseChangeLog:
  - changeSet:
      id: 001
      author: cyberjar
      changes:
        - createTable:
            tableName: civilian
            columns:
              - column:
                  name: id
                  type: BIGINT
                  autoIncrement: true
                  constraints:
                    primaryKey: true
              - column:
                  name: legal_name
                  type: VARCHAR(250)
              - column:
                  name: national_id
                  type: VARCHAR(50)
              - column:
                  name: birth_date
                  type: DATE
              - column:
                  name: criminal_record
                  type: BOOLEAN
              - column:
                  name: under_surveillance
                  type: BOOLEAN


        - createTable:
            tableName: cyberware
            columns:
              - column:
                  name: id
                  type: BIGINT
                  autoIncrement: true
                  constraints:
                    primaryKey: true
              - column:
                  name: name
                  type: VARCHAR(200)
              - column:
                  name: type
                  type: VARCHAR(50)
              - column:
                  name: version
                  type: VARCHAR(50)


        - createTable:
            tableName: implant_session
            columns:
              - column:
                  name: id
                  type: BIGINT
                  autoIncrement: true
                  constraints:
                    primaryKey: true
              - column:
                  name: civilian_id
                  type: BIGINT
              - column:
                  name: cyberware_id
                  type: BIGINT
              - column:
                  name: installed_at
                  type: DATE
              - column:
                  name: installed_by
                  type: VARCHAR(255)
        - addForeignKeyConstraint:
            baseTableName: implant_session
            baseColumnNames: civilian_id
            referencedTableName: civilian
            referencedColumnNames: id
            constraintName: fk_civilian
        - addForeignKeyConstraint:
            baseTableName: implant_session
            baseColumnNames: cyberware_id
            referencedTableName: cyberware
            referencedColumnNames: id
            constraintName: fk_cyberware

Let’s create another changelog 002-insert-sample-data.yaml to populate the DB with test data. Don’t forget to reference this file in the main changelog:

databaseChangeLog:
  - include:
      file: db/changelog/001-create-tables.yaml
  - include:
      file: db/changelog/002-insert-sample-data.yaml

In this case, we will use the change type “insert.” The whole file:

databaseChangeLog:
  - changeSet:
      id: 002
      author: cyberjar
      changes:
        - insert:
            tableName: civilian
            columns:
              - column: { name: legal_name, value: "Aelita Fang" }
              - column: { name: national_id, value: "IZ-0965437-BM" }
              - column: { name: birth_date, value: "1999-08-30" }
              - column: { name: criminal_record, value: "FALSE" }
              - column: { name: under_surveillance, value: "FALSE" }


        - insert:
            tableName: cyberware
            columns:
              - column: { name: name, value: "OptiSight X3" }
              - column: { name: type, value: "ocular" }
              - column: { name: version, value: "SZ 3000" }


        - insert:
            tableName: implant_session
            columns:
              - column: { name: civilian_id, valueNumeric: 1 }
              - column: { name: cyberware_id, valueNumeric: 1 }
              - column: { name: installed_at, valueDate: "2025-06-15" }
              - column: { name: installed_by, value: "MP-129854" }

If you are building the tutorial incrementally, start the application now to apply 001 and 002. A fresh clone already includes five changesets. The repository also has changeset 003, which adds the manufacturer column. Connect to the database to verify that Liquibase created and populated the tables.

DB schema

You will also see two additional tables that Liquibase creates automatically upon the first run: databasechangelog and databasechangeloglock.

Write a Changelog File in SQL

If you work with one database, you don’t have to use database-agnostic formats and can write changelogs in SQL. The file almost doesn’t differ from the schema docs you are used to writing, except for the formatted comments. The first non-empty line should be

--liquibase formatted sql

This header tells Liquibase to parse this file as a “formatted SQL” changelog, not just raw SQL. This way, Liquibase reads and recognizes special comment directives like --changeset author:id, --rollback, --preconditions, --context, --labels, etc.

As for the changes, they are described in the Postgres-specific SQL, so nothing new here:

--liquibase formatted sql
--changeset cyberjar:001


CREATE TABLE civilian (
  id BIGINT PRIMARY KEY,
  legal_name VARCHAR(250),
  national_id VARCHAR(50),
  birth_date DATE,
  criminal_record BOOLEAN,
  under_surveillance BOOLEAN
);


CREATE TABLE cyberware (
  id BIGINT PRIMARY KEY,
  name VARCHAR(200),
  type VARCHAR(50),
  version VARCHAR(50)
);


CREATE TABLE implant_session (
  id BIGINT PRIMARY KEY,
  civilian_id BIGINT,
  cyberware_id BIGINT,
  installed_at DATE,
  installed_by VARCHAR(255)
);


ALTER TABLE implant_session
  ADD CONSTRAINT fk_civilian  FOREIGN KEY (civilian_id)  REFERENCES civilian(id);


ALTER TABLE implant_session
  ADD CONSTRAINT fk_cyberware FOREIGN KEY (cyberware_id) REFERENCES cyberware(id);

Use Preconditions in Liquibase Changelogs

What are Preconditions?

Preconditions are special tags that enable you to specify requirements for updating the database. For instance, you can check that changes are applied to a specific database or by a particular user. Preconditions can be applied to the whole changelog or to individual changesets.

With XML, JSON, and YAML you can use all preconditions supported by Liquibase. The SQL-based changelogs support only some of them.

Preconditions Syntax

It is possible to use one preCondition tag per changelog/changeset, but you can specify several requirements using the AND, OR, NOT tags. If no tag is specified, the default AND tag is used under the hood.

For instance, in the following changelog, we verify that the database type is Postgres and the user applying changes is dbadmin:

databaseChangeLog:
  - changeSet:
      id: 001
      author: cyberjar
      preConditions:
        - dbms:
            type: postgresql
        - runningAs:
            username: dbadmin
      changes:

In the following changelog, the database type should be PostgreSQL or MySQL:

databaseChangeLog:
  - changeSet:
      id: 001
      author: cyberjar
      preConditions:
        - or:
            - dbms:
                type: postgresql
            - dbms:
                type: mysql
      changes:

You can find the whole list of available preConditions in the documentation.

This is all very well, but how can we handle the unsatisfactory check results?

How to Handle Precondition Errors and Failures

There are two types of unsatisfactory results:

  • Failures (handled by the onFail attribute) mean that the check has failed. For instance, a column exists when it mustn’t.
  • Errors (handled by the onError attribute) are exceptions thrown during the execution of the check.

You can set the onFail and onError attributes on the preConditions block independently and specify what to do in each case using the following values:

  • CONTINUE: Liquibase will not execute this changeset, but it will continue with the changelog and try to execute this changeset during the next run.
  • HALT: Liquibase stops the execution of the whole changelog.
  • MARK_RAN: Liquibase will not execute this changeset, but will mark it as executed.
  • WARN: Liquibase will continue executing the changeset/changelog, but will issue a warning.

Note that the CONTINUE and MARK_RAN can only be applied to a changeset.

For instance, the changeset below includes a precondition that verifies the column doesn’t exist. If the check fails, this changeset will not be applied, but marked as run, meaning that Liquibase won’t attempt to execute it during the next update:

databaseChangeLog:
  - changeSet:
      id: 004
      author: cyberjar
      preConditions:
        - onFail: MARK_RAN
        - not:
            columnExists:
              tableName: cyberware
              columnName: type
      changes:
        - addColumn:
            tableName: cyberware
            columns:
              - column:
                  name: type
                  type: VARCHAR(50)

Perform Rollbacks with Liquibase

Suppose something went wrong and the changes you introduced to the database brought some issues. In this case, you can use the Liquibase rollback functionality supported in CLI and Maven.

The rollback script returns the database to a specified point (see the Liquibase documentation on rollbacks for the details). There are three types of rollback:

  • A simple rollback performed with the rollback command reverts changes to the database after a specified tag.
  • A rollback to some period with the rollback-to-date command reverts changes from the current date to a specified date and time.
  • A rollback by a number of changesets with the rollback-count command rolls back a specified number of changesets.

However, writing rollback scripts is difficult, and using rollbacks is associated with risks. Therefore, you should use this feature cautiously and always validate potential changes before they are applied. You can do that with the help of these commands:

  • update-testing-rollback tests the rollbacks by deploying pending changesets, rolling them back, and running the update again.
  • future-rollback-sql generates a SQL that will be used to perform the rollback.

Let’s see how we can perform a rollback using Maven. For that, we need to add a new plugin, liquibase-maven-plugin and configure the database connection and the path to the main changelog there:

<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>5.0.4</version>
    <configuration>
        <changeLogFile>src/main/resources/db/changelog/db.changelog-master.yaml</changeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://localhost:5432/neurowatch</url>
        <defaultSchemaName />
        <username>${env.DB_USERNAME}</username>
        <password>${env.DB_PASSWORD}</password>
    </configuration>
</plugin>

The plugin reads DB_USERNAME and DB_PASSWORD from the environment. Set DB_USERNAME to neurowatch_user for the demo and use the same password you configured earlier. Maven copies the included changelogs into target/classes during process-resources.

Now, let’s create a new rollback changelog where we will add a new column to the civilian table. The rollback will drop this column:

databaseChangeLog:
  - changeSet:
      id: 005
      author: cyberjar
      preConditions:
        - dbms:
            type: postgresql
        - not:
            columnExists:
              tableName: civilian
              columnName: temp_notes
      changes:
        - addColumn:
            tableName: civilian
            columns:
              - column:
                  name: temp_notes
                  type: VARCHAR(255)
                  defaultValue: 'N/A'
      rollback:
        - dropColumn:
            columnName: temp_notes
            tableName: civilian

Save this as 005-test-rollbacks.yaml and append this include to the databaseChangeLog list in the master file. The cloned project already includes it.

  - include:
      file: db/changelog/005-test-rollbacks.yaml

Start the application and check that civilian.temp_notes exists. If you stop the application, run docker compose up -d before using the Maven plugin so PostgreSQL is available.

Run the liquibase:rollback Maven goal specifying the number of changesets you want to roll back with rollbackCount:

./mvnw process-resources liquibase:rollback -Dliquibase.rollbackCount=1

In the console, you will see that the rollback was applied. Verify in your database tool that the column indeed disappeared.

The rollback removes the entry for 005 from DATABASECHANGELOG. The next startup applies 005 again if it is still eligible. If the changeset remains applied in another environment, use a new corrective changeset to preserve its recorded history and checksum.

Generate Changelogs with Liquibase diff

Comparing Two Database Versions with Liquibase diff

Liquibase can compare two databases or compare a database with JPA mappings through the Hibernate extension. The CLI diff command reports differences, and diff-changelog writes them to a changelog. The Maven example below writes a file by setting diffChangeLogFile.

The diff command can be very useful in some situations but harmful in others, so it should not be used for everything. Let’s briefly discuss when it is a sagacious advisor and when it is a little drunk elf that desperately wants to help but has no idea what it is doing.

Benefits and Drawbacks of Diffs

The Liquibase diff command can be helpful when

  • You want to integrate Liquibase into an existing project. In this case, diff-changelog can compare the existing and empty databases and generate changesets describing how to recreate the current state of the database.
  • The diff command can help you detect changes that weren’t documented. Although the best practice for developers is to introduce all changes through migrations rather than directly to the database, this command can still be used for a quick sanity check.

On the other hand, there are some caveats with diffs that you must take into consideration:

  • The diffs show syntactical differences but not semantical ones. For example, if a column was renamed, the generated changelog will tell you that one column should be dropped and another added, leading to data loss.
  • The diffs can’t track the data changes efficiently because they can’t handle the expected differences between environments.

Therefore, this command should be used with caution and a complete understanding of what and why one is doing. Always validate the generated changelogs before applying them and adjust them manually if needed. You can also use additional tools, such as JPA Buddy, to validate the potential changes.

Use the Liquibase Hibernate Plugin with Maven to Generate Diffs

The demo already has a difference to inspect. The Cyberware entity maps serialNumber, while the active changelog leaves out serial_number. The sample diff contains the missing column and sits outside the master changelog.

Let’s first add the dependency to Liquibase Hibernate that will enable you to generate the changelog file comparing the current database schema and JPA entities:

<dependency>
    <groupId>org.liquibase.ext</groupId>
    <artifactId>liquibase-hibernate7</artifactId>
    <version>5.0.4</version>
</dependency>

Next, we need to add some additional configurations to the Liquibase Maven plugin:

  • diffChangeLogFile points to the changelog file that will be generated as a result of the database comparison. The name of the file contains a timestamp for easier version tracking. ${maven.build.timestamp} is a built-in Maven property holding the build start time (UTC); its default format contains colons, which Windows doesn't allow in file names, so set your own format:
<properties>
    <maven.build.timestamp.format>yyyyMMdd-HHmmssSSS</maven.build.timestamp.format>
</properties>
  • referenceURL pointing to the package in the application where our JPA entities reside. The dialect parameter is required because the referenceUrl performs the package scanning.
  • The naming strategies make the comparison use the same table and column names as Spring Boot.
<plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>5.0.4</version>
    <configuration>
        <changeLogFile>src/main/resources/db/changelog/db.changelog-master.yaml</changeLogFile>
        <diffChangeLogFile>src/main/resources/db/changelog/${maven.build.timestamp}_changelog.yaml</diffChangeLogFile>
        <driver>org.postgresql.Driver</driver>
        <url>jdbc:postgresql://localhost:5432/neurowatch</url>
        <defaultSchemaName />
        <username>${env.DB_USERNAME}</username>
        <password>${env.DB_PASSWORD}</password>
        <referenceUrl>hibernate:spring:dev.cyberjar?dialect=org.hibernate.dialect.PostgreSQLDialect
            &amp;hibernate.physical_naming_strategy=org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy
            &amp;hibernate.implicit_naming_strategy=org.springframework.boot.hibernate.SpringImplicitNamingStrategy
        </referenceUrl>
    </configuration>
</plugin>

Finally, we can run the liquibase:diff command to generate a changelog. Note that this command uses compiled classes from the target directory for comparison, so we need to rebuild the project:

./mvnw clean compile liquibase:diff

As a result, the changelog will be generated in the db/changelog directory.

Run Database Migrations in CI/CD with Liquibase and GitHub Actions

Let’s discuss this pipeline step by step.

The workflow YAML is configured to run whenever changes are pushed to the main branch of the application. It defines one job, “Verify and Migrate DB with Liquibase”:

name: Liquibase CI Pipeline

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  liquibase-verify:
    name: Verify and Migrate DB with Liquibase

Before conducting any steps within the job, the workflow declares the services it needs to spin up, in this case, a PostgreSQL service. The database password comes from a GitHub Actions secret, DB_PASSWORD, which you create in the repository settings (Settings → Secrets and variables → Actions). But in a real setup, you should store credentials as GitHub Secrets and reference them in the password fields.

The service also exposes the required ports and includes health-check options, so the job continues only after the database reports itself as ready.

runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:18
        env:
          POSTGRES_DB: neurowatch
          POSTGRES_USER: neurowatch_user
          POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }}
        ports:
          - 5432:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5

The workflow checks out the repository and runs Maven resource processing. The Liquibase actions read the source files through searchPath, so they can resolve the changelog without the Maven step.

    steps:
      - name: Checkout repository
        uses: actions/checkout@v7


      - name: Process Resources
        shell: bash
        run: |
          ./mvnw process-resources

The interesting part starts with validation. Here, the workflow uses the Liquibase GitHub Action to validate the change sets. It specifies the path to the main changelog file along with the URL, username, and password needed to reach the database. The changelog path is relative to the search path, src/main/resources, so the CLI resolves the included files the same way Spring Boot does from the classpath. Note that the URL does not point to localhost. Instead, it targets the service name we brought up, postgres.

      - name: Validate changesets
        uses: liquibase-github-actions/[email protected]
        with:
          changelogFile: db/changelog/db.changelog-master.yaml
          searchPath: src/main/resources
          url: "jdbc:postgresql://postgres:5432/neurowatch"
          username: neurowatch_user
          password: ${{ secrets.DB_PASSWORD }}
          headless: true
          logLevel: INFO

Next comes Preview Update SQL, a dry run of the SQL that would be executed. It uses the update-sql Liquibase GitHub Action, which prints the SQL without applying it, and repeats the same configuration details: the changelog file, the database URL, and the credentials.

  - name: Preview updateSQL (dry run)
        uses: liquibase-github-actions/update-[email protected]
        with:
          changelogFile: db/changelog/db.changelog-master.yaml
          searchPath: src/main/resources
          url: "jdbc:postgresql://postgres:5432/neurowatch"
          username: neurowatch_user
          password: ${{ secrets.DB_PASSWORD }}
          headless: true
          logLevel: INFO

The checked-in workflow calls rollback-count-sql before update. This command previews rollback SQL for changesets already applied to the database. On a fresh CI database there are none. Move the preview after update to inspect the SQL for changeset 005. To test the rollback itself, execute it against a disposable database and check that temp_notes disappears.

      - name: Preview rollbackSQL (dry run of last changeSet)
        uses: liquibase-github-actions/rollback-count-[email protected]
        with:
          changelogFile: db/changelog/db.changelog-master.yaml
          searchPath: src/main/resources
          count: 1
          url: "jdbc:postgresql://postgres:5432/neurowatch"
          username: neurowatch_user
          password: ${{ secrets.DB_PASSWORD }}
          headless: true
          logLevel: INFO

The update step applies the migrations to the PostgreSQL service for this job.

      - name: Apply Liquibase migration
        uses: liquibase-github-actions/[email protected]
        with:
          changelogFile: db/changelog/db.changelog-master.yaml
          searchPath: src/main/resources
          url: "jdbc:postgresql://postgres:5432/neurowatch"
          username: neurowatch_user
          password: ${{ secrets.DB_PASSWORD }}
          headless: true
          logLevel: INFO

Next, the workflow sets up Liberica JDK 25 for Maven. The earlier Liquibase actions run in Docker with their own Java. Setting JAVA_HOME on the runner first would pass a host path into those containers. The integration tests use a separate Testcontainers PostgreSQL database and exercise Spring Boot's startup migrations. The final deployment step is a placeholder.

      - name: Set up JDK 25
        uses: actions/setup-java@v6
        with:
          distribution: liberica
          java-version: '25'
          cache: maven

      - name: Run tests
        run: ./mvnw test


      - name: Deploy app
        if: success()
        run: echo "Deploy your app here..."

The workflow uses Liquibase CLI 4.27.0 through the legacy individual actions, while the app uses Liquibase 5.0.4. For a workflow that uses the same CLI version as the app, select it with liquibase/setup-liquibase. The complete demo workflow is on GitHub.

FAQ

What is Liquibase used for in Spring Boot?

Liquibase versions your database schema alongside your code. On startup, Spring Boot runs eligible migrations and Liquibase records their history.

Which is better, Flyway or Liquibase?

Flyway is a good fit for versioned SQL scripts. Liquibase is useful when you want declarative changelogs, preconditions, or per-changeset rollback definitions. See our guide to Flyway with Spring Boot.

Is Liquibase free?

Liquibase Community is free to use, including in production. Since version 5.0, it is licensed under the Functional Source License (FSL): you can use and modify it, but not commercialize it in a way that competes with Liquibase, and each release converts to Apache 2.0 after two years. Liquibase Secure is the paid commercial edition.

How does Liquibase detect changes?

Liquibase identifies each changeset by its ID, author, and changelog path and compares that identity with DATABASECHANGELOG. A changed checksum on an applied changeset normally fails validation. With runOnChange, a changed checksum can trigger another run.

Can Liquibase be used with Spring Boot 4?

Yes. Add spring-boot-starter-liquibase: in Spring Boot 4, liquibase-core alone doesn't activate the auto-configuration.

Conclusion

In this article, we examined using Liquibase with Spring Boot and Maven for reliable database migrations both locally and in the CI/CD pipeline. On Spring Boot 4, add spring-boot-starter-liquibase to enable the integration.

Don’t forget to subscribe to our newsletter for a monthly digest of our articles and videos on Java development and news in the Java world!

 

Subcribe to our newsletter

figure

Read the industry news, receive solutions to your problems, and find the ways to save money.

Further reading