Working with Flyway Migration Files
Learn how to build a migration file for your database using Harness DBOps.
Flyway provides a versioned, convention-driven approach to managing database schema changes. When paired with Harness Database DevOps, Flyway migrations become fully automated, GitOps-driven, and Kubernetes-native. This guide walks you through how to structure your migration files, configure the flyway.toml manifest, and prepare your repository for seamless deployments.
Flyway migration file structure
A typical Flyway-compatible repository looks like this:
.
├── migrations/
│ ├── V1__init.sql
│ ├── V2__create_users.sql
│ ├── V3__add_orders_table.sql
│ └── U1__revert_init.sql
│ └── U2__drop_users.sql
├── flyway.toml
└── README.mdRecommended naming conventions:
migrations/as the folder root.flyway.tomlplaced at the top level.Semantic versioning (V1, V2, V3…) to maintain predictable ordering.
Create versioned migration files
Flyway processes migrations based on its naming convention:
1. Versioned migrations
Executed once and never modified.
Examples:
V1__init.sqlV2__add_users_table.sqlV3__add_indexes.sql
Each file contains SQL statements meant to evolve your schema:
2. Undo migrations
Flyway supports a special class of migrations known as Undo Migrations, which use the U<version>__<description>.sql naming pattern.
Undo migrations allow you to reverse the effects of a corresponding versioned migration. For example:
Forward Migration
Undo Migration
Purpose
V1__init.sql
U1__revert_init.sql
Rolls back schema objects created in V1
V2__create_users.sql
U2__drop_users.sql
Removes objects introduced in V2
Undo migrations are executed when you run the 'DB Schema Rollback' step in your pipeline. This allows for more controlled rollback than relying solely on backups or manual scripts.
3. Create the flyway.toml configuration file (optional)
The flyway.toml file defines how Flyway connects to your database and manages migrations.
A basic configuration looks like this:
You can also define dynamic configurations using profiles. For example:
Best practices to create migrations files
Some best practices to follow when creating Flyway migrations:
Maintain Immutable Versioned Migrations Never modify an existing V1__*.sql file. If you need to correct a change: Create a new version file, e.g.,
V4__fix_users_column.sql.Keep Migrations Small and Incremental. Smaller files ensure better readability and reduce risk during execution.
Enable naming validation in flyway.toml to enforce team-wide consistency:
validateMigrationNaming = trueUse a Single Source of Truth. All schema changes must originate in Git, not from manual DB edits. Harness will ensure schema consistency across environments.
Next steps
Go to Build a Changelog to learn how to generate changelogs for SQL databases.
Last updated
Was this helpful?