Schema Migration with Liquibase

Learn how to use the Liquibase to manage your schema migrations.

1. Getting Started

In this guide, we will create a Python application built with Pyronaut.

2. What you will need

To complete this guide, you will need the following:

  • Some time on your hands

  • GraalPy installed and the Pyronaut CLI available locally

3. Solution

We recommend that you follow the instructions in the next sections and create the application step by step. However, you can go right to the completed example.

Create an application using the Pyronaut CLI (Command Line Interface) or Pyronaut Launch

mn create-app \
   example.micronaut.micronautguide \
   --features=data-jdbc,postgres,liquibase \
   --build=pyronaut \
   --lang=python \

The previous command creates a Pyronaut application with the default package example.micronaut in a directory named micronautguide.

If you use Micronaut Launch, select "Micronaut Application" as application type and add postgres, data-jdbc, and liquibase as features.

3.1. Create Entity

Create a dataclass annotated with @MappedEntity to save persons:

src/example/micronaut/person.py
from dataclasses import dataclass
from typing import Annotated

from jakarta.validation.constraints import NotBlank
from micronaut.data.annotation import GeneratedValue, Id, MappedEntity, Version
from micronaut.serde.annotation import Serdeable


@dataclass
@Serdeable
@MappedEntity  (1)
class Person:
    id: Annotated[int | None, Id, GeneratedValue] = None   (2) (3)
    version: Annotated[int | None, Version] = None  (4)
    name: Annotated[str | None, NotBlank] = None
    age: int | None = None
1 Annotate the class with @MappedEntity to map the class to the table defined in the schema.
2 Specifies the ID of an entity
3 Specifies that the property value is generated by the database and not included in inserts
4 Annotate the field with @Version to enable optimistic locking for your entity.

3.2. Database Migration with Liquibase

We need a way to create the database schema. For that, we use Micronaut integration with Liquibase.

Add the following snippet to include the necessary dependencies:

Configure the database migrations directory for Liquibase in application.toml.

config/application.toml
[liquibase.datasources.default]
change-log = "classpath:db/liquibase-changelog.xml"

Create the following files with the database schema creation:

config/db/liquibase-changelog.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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <include file="changelog/01-create-person.xml"
             relativeToChangelogFile="true"/>

    <include file="changelog/02-nullable-age.xml"
             relativeToChangelogFile="true"/>

</databaseChangeLog>
config/db/changelog/01-create-person.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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <changeSet id="01" author="sdelamo">

        <createTable tableName="person"
                     remarks="A table to contain persons">

            <column name="id" type="BIGINT">
                <constraints nullable="false"
                             unique="true"
                             primaryKey="true"
                             primaryKeyName="personPK"/>
            </column>

            <column name="version" type="BIGINT">
                <constraints nullable="false"/>
            </column>

            <column name="age" type="INT">
                <constraints nullable="false"/>
            </column>

        </createTable>

    </changeSet>

</databaseChangeLog>

During application startup, Liquibase executes the SQL file and creates the schema needed for the application.

If you check the database schema, there are three tables:

  • databasechangelog

  • databasechangeloglock

The tables databasechangelog and databasechangeloglock are used by Liquibase to keep track of database migrations.

The person table looks like:

Column Nullable

id

NO

version

NO

name

NO

age

NO

3.3. Drop Not Null Constraint

Applications change. The Python dataclass uses int | None for the optional age field.

Add a new changeset to drop the null constraint:

config/db/liquibase-changelog.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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <include file="changelog/01-create-person.xml"
             relativeToChangelogFile="true"/>

    <include file="changelog/02-nullable-age.xml"
             relativeToChangelogFile="true"/>

</databaseChangeLog>
config/db/changelog/02-nullable-age.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"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

    <changeSet id="02" author="sdelamo">
        <dropNotNullConstraint tableName="person"
                               columnName="age"/>
    </changeSet>

</databaseChangeLog>

After the changeset, the person table looks like:

Column Nullable

id

NO

version

NO

name

NO

age

YES

4. Liquibase

To enable the Liquibase endpoint, add the management dependency on your classpath.

Enable the Liquibase endpoint:

config/application.toml
[endpoints.liquibase]
enabled = true
sensitive = false

5. Test Resources

When the application is started locally, either under test or while running locally, resolution of the datasource URL is detected and the Test Resources service will start a local PostgreSQL docker container, and inject the properties required to use this as the datasource.

For more information, see the JDBC section of the Test Resources documentation.

5.1. Test

Create a test that invokes the Liquibase endpoint

tests/example/micronaut/test_liquibase_endpoint.py
import pytest
import requests

from pyronaut.test import MicronautTest, micronaut_test_fixture


@pytest.fixture
def my_context(request):
    fixture = micronaut_test_fixture(
        request,
        MicronautTest(environments=["test"], transactional=False),  (1)
    )
    yield fixture
    fixture.stop()


@pytest.fixture
def client(my_context):
    return requests.with_context(my_context)  (2)


def test_migrations_are_exposed_via_an_endpoint(client):
    response = client.get("/liquibase")

    assert response.status_code == 200

    liquibase_reports = response.json()
    assert len(liquibase_reports) == 1

    liquibase_report = liquibase_reports[0]
    assert len(liquibase_report["changeSets"]) == 2
1 Create a Micronaut test fixture for the application under test.
2 Use requests.with_context(my_context) to call the application under test with a context-bound HTTP client.

5.2. Running the application

Although the URL is configured automatically via Test Resources, we must configure the PostgreSQL driver and dialect:

config/application.toml
[datasources.default]
(1)
driver-class-name = "org.postgresql.Driver"
(2)
dialect = "POSTGRES"
(3)
schema-generate = "NONE"
1 Use PostgreSQL driver.
2 Configure the PostgreSQL dialect.
3 You handle database migrations via Liquibase

To run the application, use the pyronaut dev command, which starts the application on port 8080.

You can run a cURL command to test the application:

curl http://localhost:8080/liquibase

You will see information about migrations.

You can run a cURL command to test the application:

curl http://localhost:8080/liquibase

You will see information about migrations.

6. Next Steps

Explore more features with Micronaut Guides.

Check Micronaut Liquibase integration.

7. License

All guides are released with an Apache License 2.0 for the code and a Creative Commons Attribution 4.0 license for the writing and media (images).