Schema Migration with Flyway

Learn how to use Flyway to manage your schema migrations

Authors: Sergio del Amo

Micronaut Version: 3.5.2

1. Getting Started

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

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

  • A decent text editor or IDE

  • JDK 1.8 or greater installed with JAVA_HOME configured appropriately

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

mn create-app example.micronaut.micronautguide \
   --features=data-jdbc,postgres,flyway \
   --build=maven --lang=kotlin
If you don’t specify the --build argument, Gradle is used as the build tool.
If you don’t specify the --lang argument, Java is used as the language.

The previous command creates a Micronaut 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 flyway as features.

3.1. Enable annotation Processing

If you use Java or Kotlin and IntelliJ IDEA, make sure to enable annotation processing.

annotationprocessorsintellij

3.2. Create Entity

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

src/main/kotlin/example/micronaut/Person.kt
package example.micronaut

import io.micronaut.data.annotation.GeneratedValue
import io.micronaut.data.annotation.Id
import io.micronaut.data.annotation.MappedEntity
import io.micronaut.data.annotation.Version

@MappedEntity (1)
class Person(
    val name: String,

    val age: Int

) {

    @Id (2)
    @GeneratedValue (3)
    var id: Long? = null

    @Version (4)
    var version: Long? = null
}
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.3. 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:

pom.xml
<dependency>
    <groupId>io.micronaut.flyway</groupId>
    <artifactId>micronaut-flyway</artifactId>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-mysql</artifactId>
    <scope>runtime</scope>
</dependency>

We will enable Flyway in the application.yml and configure it to perform migration on one of the defined data sources.

src/main/resources/application.yml
flyway:
  datasources:
    default:
      enabled: true (1)
1 Enable Flyway for the default datasource.
Configuring multiple data sources is as simple as enabling Flyway for each one. You can also specify folders 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 Micronaut 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:

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

Flyway uses the table flyway_scheme_history to keep track of database migrations.

The person table looks like:

Column Nullable

id

NO

version

NO

name

NO

age

NO

3.4. Drop Not Null Constraint

Applications change. Make age optional:

src/main/kotlin/example/micronaut/Person.kt
    val age: Int?

Add a new migration to drop the null constraint:

src/main/resources/db/migration/V2__nullable-age.sql
ALTER TABLE person ALTER COLUMN age DROP NOT 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.

pom.xml
<dependency>
    <groupId>io.micronaut</groupId>
    <artifactId>micronaut-management</artifactId>
    <scope>compile</scope>
</dependency>

Enable the Flyway endpoint:

src/main/resources/application.yml
endpoints:
  flyway:
    enabled: true
    sensitive: false

4.1. Testing with PostgreSQL via TestContainers

We use Test Containers to test against a PostgreSQL database.

Add the following snippet to include the necessary test container dependencies:

pom.xml
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>postgresql</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers</artifactId>
    <scope>test</scope>
</dependency>

Configure the default datasource to use the PostgreSQL database provided by TestContainers for the test environment:

src/test/resources/application-test.yml
datasources:
  default:
    url: jdbc:tc:postgresql:12:///postgres (1)
    driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
1 By using a specially modified JDBC URL, Testcontainers provides a disposable stand-in database that can be used without requiring modification to your application code. See Test Containers JDBC URL.

4.2. Test

Create a test that invokes the Flyway endpoint.

src/test/kotlin/example/micronaut/FlywayEndpointTest.kt
package example.micronaut

import io.micronaut.core.type.Argument
import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpStatus.OK
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import jakarta.inject.Inject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test

@MicronautTest (1)
class FlywayEndpointTest {

    @Inject
    @field:Client("/")
    lateinit var httpClient: HttpClient (2)

    @Test
    fun migrationsAreExposedViaAndEndpoint() {
        val client = httpClient.toBlocking()

        val response = client.exchange(HttpRequest.GET<Any>("/flyway"), Argument.listOf(FlywayReport::class.java))
        assertEquals(OK, response.status())

        val flywayReports = response.body()
        assertNotNull(flywayReports)
        assertEquals(1, flywayReports!!.size)

        val flywayReport = flywayReports[0]
        assertNotNull(flywayReport)
        assertNotNull(flywayReport.migrations)
        assertEquals(2, flywayReport.migrations!!.size)
    }

    internal class FlywayReport {
        var migrations: List<Migration>? = null
    }

    internal class Migration {
        var script: String? = null
            private set

        fun setId(script: String?) {
            this.script = script
        }
    }
}
1 Annotate the class with @MicronautTest so the Micronaut framework will initialize the application context and the embedded server. More info.
2 Inject the HttpClient bean and point it to the embedded server.

4.3. Running the application

After installing Docker, execute the following command to run a PostgreSQL container:

docker run -it --rm \
     -p 5432:5432 \
     -e POSTGRES_USER=dbuser \
     -e POSTGRES_PASSWORD=theSecretPassword \
     -e POSTGRES_DB=postgres \
     postgres:11.5-alpine

Set up the following environment variables to connect to the PostgreSQL database you started with Docker.

src/main/resources/application.yml
datasources:
  default:
    url: jdbc:postgresql://localhost:5432/postgres  (1)
    driverClassName: org.postgresql.Driver  (2)
    dialect: POSTGRES (3)
    schema-generate: NONE (4)
1 The JDBC URL matches the database name you used in the previous command.
2 Use PostgreSQL driver.
3 Configure the PostgreSQL dialect.
4 You handle database migrations via Flyway.
export DATASOURCES_DEFAULT_USERNAME=dbuser
export DATASOURCES_DEFAULT_PASSWORD=theSecretPassword

Configure your default datasource to use the PostgreSQL database you started with Docker:

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

5. Generate a Micronaut Application Native Image with GraalVM

We will use GraalVM, the polyglot embeddable virtual machine, to generate a native image of our Micronaut application.

Compiling native images ahead of time with GraalVM improves startup time and reduces the memory footprint of JVM-based applications.

Only Java and Kotlin projects support using GraalVM’s native-image tool. Groovy relies heavily on reflection, which is only partially supported by GraalVM.

5.1. Native image generation

The easiest way to install GraalVM on Linux or Mac is to use SDKMan.io.

Java 11
sdk install java 22.1.0.r11-grl
If you still use Java 8, use the JDK11 version of GraalVM.
Java 17
sdk install java 22.1.0.r17-grl

For installation on Windows, or for manual installation on Linux or Mac, see the GraalVM Getting Started documentation.

After installing GraalVM, install the native-image component, which is not installed by default:

gu install native-image

To generate a native image using Maven, run:

./mvnw package -Dpackaging=native-image

The native image is created in the target directory and can be run with target/micronautguide.

You can run a cURL command to test the application:

curl http://localhost:8080/flyway

You will see information about migrations.

6. Next steps

Explore more features with Micronaut Guides.

Check Micronaut Flyway integration.

Learn more about Flyway.

7. Help with the Micronaut Framework

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