Groovy / Gradle

Schema Migration with Liquibase

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

Sergio del Amo
On this guide
In this section

Getting Started

In this guide, we will create a Micronaut application written in Groovy.

What you will need

To complete this guide, you will need the following:

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 Micronaut Command Line Interface or with Micronaut Launch.

mn create-app \
   example.micronaut.micronautguide \
   --features=data-jdbc,postgres,liquibase \
   --build=gradle \
   --lang=groovy \
Note
If you don’t specify the --build argument, Gradle with the Kotlin DSL is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.
If you don’t specify the --test argument, JUnit is used for Java and Kotlin, and Spock is used for Groovy.

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

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

Create Entity

Create a @MappedEntity to save persons. Initially, consider name and age required. Use int primitive for the age.

groovy/src/main/groovy/example/micronaut/Person.groovy

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:

build.gradle
implementation("io.micronaut.liquibase:micronaut-liquibase")

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

src/main/resources/application.properties
liquibase.datasources.default.change-log=classpath\:db/liquibase-changelog.xml

Create the following files with the database schema creation:

src/main/resources/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>
src/main/resources/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

Drop Not Null Constraint

Applications change. Make age optional:

groovy/src/main/groovy/example/micronaut/Person.groovy
@Nullable
final Integer age

Person(@NonNull String name,
       @Nullable Integer age) {
    this.name = name
    this.age = age
}

Add a new changeset to drop the null constraint:

src/main/resources/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>
src/main/resources/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

Liquibase

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

build.gradle
implementation("io.micronaut:micronaut-management")

Enable the Liquibase endpoint:

src/main/resources/application.properties
endpoints.liquibase.sensitive=false

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.

Test

Create a test that invokes the Liquibase endpoint

groovy/src/test/groovy/example/micronaut/LiquibaseEndpointSpec.groovy

Running the application

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

src/main/resources/application.properties

To run the application, use the ./gradlew run 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.

Next Steps

Explore more features with Micronaut Guides.

Check Micronaut Liquibase integration.

Help with the Micronaut Framework

The Micronaut Foundation sponsored the creation of this Guide. A variety of consulting and support services are available.

License

Note
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).