Schema Migration with Flyway

Learn how to use Flyway to manage your schema migrations.

1. Getting Started

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

You use Flyway:

Flyway is an open-source database migration tool. It strongly favors simplicity and convention over configuration.

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,mysql,flyway \
   --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 mysql, data-jdbc, and flyway 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 Flyway

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

Flyway automates schema changes, significantly simplifying schema management tasks, such as migrating, rolling back, and reproducing in multiple environments.

Add the following snippet to include the necessary dependencies:

We will enable Flyway in the Micronaut configuration file and configure it to perform migrations on one of the defined data sources.

config/application.toml
[flyway.datasources.default]
(1)
enabled = true
1 Enable Flyway for the default datasource.
Configuring multiple data sources is as simple as enabling Flyway for each one. You can also specify directories that will be used for migrating each data source. Review the Micronaut Flyway documentation for additional details.

Flyway migration will be automatically triggered before your Pyronaut application starts. Flyway will read migration commands in the resources/db/migration/ directory, execute them if necessary, and verify that the configured data source is consistent with them.

Create the following migration files with the database schema creation:

config/db/migration/V1__create-person.sql
CREATE TABLE person(
    id   bigint primary key not null,
    name varchar(255)       not null,
    age  int                not null
)

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

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

  • person

  • flyway_schema_history

Flyway uses the table flyway_schema_history 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 migration to drop the null constraint:

config/db/migration/V2__nullable-age.sql
ALTER TABLE person MODIFY age int default null;

After the migration, the person table looks like:

Column Nullable

id

NO

version

NO

name

NO

age

YES

4. Flyway endpoint

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

Enable the Flyway endpoint:

config/application.properties
endpoints.flyway.enabled=true
endpoints.flyway.sensitive=false

4.1. Test

Create a test that invokes the Flyway endpoint.

tests/example/micronaut/test_flyway_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("/flyway")

    assert response.status_code == 200

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

    flyway_report = flyway_reports[0]
    assert len(flyway_report["migrations"]) == 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. 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 MySQL docker container, and inject the properties required to use this as the datasource.

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

6. Testing the Application

To run the tests:

pyronaut install
pyronaut validate-config
pyronaut test

7. Running the Application

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/flyway

You will see information about migrations.

8. Next Steps

Explore more features with Micronaut Guides.

Check Micronaut Flyway integration.

Learn more about Flyway.

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