Flyway migration conventions
Flyway uses the naming conventionV{version}__{description}.sql:
- Version number sets the execution order
- Double underscore separates the version number from the description.
sql/ directory. Starting with Flyway version 12 an explicit location must be configured instead.
Each migration runs exactly once. Flyway tracks applied versions in a flyway_schema_history table it creates in your database, and skips script versions that are already recorded there.
Run your first migration
Start a PostgreSQL container
Start a PostgreSQL container on a shared Docker network to perform your testing on. Flyway will later use the database name, user, and password set here to connect to PostgreSQL:
This example does not mount a volume so data will be lost when the container is removed. For a persistent setup, add
-v pgdata:/var/lib/postgresql/data to the docker run command.Create a working directory
Create a working directory with a
sql subdirectory. Next we will configure Flyway to look for migration scripts there:Configure the database connection
Create JDBC (Java Database Connectivity) is Java’s standard API for database connections. The JDBC URL specifies the driver, host, port, and database name.
flyway-demo/flyway.conf with the connection details matching the container above:flyway-demo/flyway.conf
Create a migration script
Create the first migration script at
flyway-demo/sql/V1__Create_orders_table.sql:flyway-demo/sql/V1__Create_orders_table.sql
Fix a PostgreSQL collation version mismatch with Flyway
When a PostgreSQL image is upgraded to a version built with a newer glibc collation library, Postgres may log a collation version mismatch warning. Flyway wraps the fix commands in a versioned migration for a tracked, auditable record that runs exactly once per environment.See Collation version mismatch in Postgres for background on the warning and what causes it.
Overview
The fix requires two commands handled separately:ALTER DATABASE demo REFRESH COLLATION VERSIONupdates the collation version in PostgreSQL’s internal metadata to match the OS. This clears the warning but does not fix index ordering issues.REINDEX DATABASE demorebuilds the indexes to match the new collation, completing the fix.
REINDEX DATABASEis not compatible with transactions so it cannot be put in a regular Flyway migration file. Instead, it needs to run separately, either in a different migration marked as non-transactional or manually outside of Flyway entirely.
Steps
Create the migration script
Save the following to Number the version as appropriate for your environment (
flyway-demo/sql/V2__Fix_collation_version.sql:V2 is just an example).JDK compatibility
The Minimus Flyway image is built on JDK 21. JDK 21 is an LTS release and is suitable for production deployments.JDK 21 sets the compatibility floor for this image. Ensure your application runtime targets JDK 21 or is compatible with it. If your app is built for an older JDK (e.g. JDK 11 or 17), it may not be compatible with this image.