Automating Schema Migrations with Flyway

Galaxy Glossary

How do I automate schema migrations with Flyway?

Automating schema migrations with Flyway means using Flyway’s version-controlled SQL or Java scripts and repeatable commands to apply database schema changes consistently across environments without manual intervention.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Table of Contents

Flyway is an open-source database migration tool that manages schema changes through versioned scripts stored alongside application code. By integrating Flyway into your CI/CD pipeline you can apply, verify, and track schema alterations automatically every time you deploy—eliminating fragile manual steps, reducing production incidents, and guaranteeing that every developer, staging, and production database shares the same structure.

How Flyway Works

At its core Flyway follows a simple but powerful principle: migrations are just files. Each migration is a SQL (or Java/Kotlin) script named following the pattern V<version>>__<description>.sql (e.g., V2__add_users_table.sql). Flyway keeps a table called flyway_schema_history in the target database to track which versions have already been executed. When invoked, Flyway scans the migration directory, calculates what’s new, and runs the pending scripts inside a single transaction (where supported) in incremental order.

Key Concepts

  • Versioned migrations (V): Immutable, ordered changes applied exactly once.
  • Repeatable migrations (R): Re-run whenever their checksum changes, perfect for views, stored procedures, or reference data.
  • Baselines: Tell Flyway where to start when onboarding an existing database that already has objects.
  • Placeholders: Token substitution (${schema}) for environment-specific values.

Why Automate Schema Migrations?

Manual DDL scripts break easily: someone forgets to run a patch, ordering is wrong, or production hot-fixes diverge from development. Automating migrations with Flyway provides:

  • Determinism: The same script version yields the same schema everywhere.
  • Idempotency: Flyway records each change, preventing duplicate execution.
  • Auditability: flyway_schema_history becomes an authoritative log of who changed what, when, and why.
  • Seamless rollbacks: Pair each forward migration with a down script or keep prior database snapshots.
  • CI/CD integration: Schemas evolve at the same speed as application code.

Step-by-Step Automation Guide

1. Install Flyway

Flyway ships as a standalone CLI, Docker image, Gradle/Maven plugin, or as part of Spring Boot. Pick the distribution that aligns with your build system. For example:

# macOS
brew install flyway

# Docker
docker pull flyway/flyway

2. Create a Migration Directory

Within your project repository add a db/migration folder. Place each new script here with the versioned naming convention:

V1__create_products_table.sql
V2__add_price_column.sql
R__refresh_reporting_views.sql

3. Configure Flyway

Provide connection settings via flyway.conf, environment variables, or the command line:

# flyway.conf
flyway.url=jdbc:postgresql://localhost:5432/shop
flyway.user=app_user
flyway.password=${DB_PASSWORD}
flyway.locations=filesystem:db/migration

4. Baseline Existing Databases

If production already contains objects, prevent Flyway from replaying initial DDL by running:

flyway baseline -baselineVersion=1.0 -baselineDescription="Production init"

5. Run Migrations Locally

Developers should execute migrations on startup:

flyway migrate

This creates flyway_schema_history and applies outstanding scripts.

6. Add Flyway to CI/CD

Insert a migration stage in your pipeline after the application artefact is built but before deployment. Example GitHub Actions step:

- name: Flyway migrate
uses: red-gate/flyway-github-action@v2
with:
url: ${{ secrets.DB_URL }}
user: ${{ secrets.DB_USER }}
password: ${{ secrets.DB_PASS }}
locations: filesystem:db/migration
clean: false

Failures fail the build, blocking deployments that would leave the schema inconsistent.

7. Governance & Rollbacks

  • Enable flyway.validateOnMigrate=true to guarantee previously applied scripts haven’t been edited retroactively.
  • Generate SQL dry-run output (flyway -outputFile migrate.sql migrate) for manual review in regulated environments.
  • Tag releases by schema version to align database snapshots with application builds.

Real-World Example: Adding an Orders Feature

Suppose your e-commerce platform introduces an orders table. You’d:

  1. Create V3__create_orders_table.sql with DDL.
  2. Commit it and push.
  3. Pipeline runs flyway validateflyway migrate.
  4. All environments adopt the new table before the API code referencing it is deployed.

Integrating Flyway with Galaxy

Galaxy’s lightning-fast SQL editor is an excellent place to draft and test Flyway migration scripts. Because Galaxy supports parameterization and version control comments, you can:

  • Prototype DDL in Galaxy’s editor against a dev database.
  • Use Galaxy’s AI Copilot to optimize indexes or suggest constraints.
  • Copy the validated SQL into your db/migration folder for execution by Flyway.
  • Leverage Galaxy Collections to share approved migration scripts with teammates, reducing review friction.

Remember, Galaxy runs ad-hoc SQL; the actual automation still occurs when Flyway executes those scripts in the pipeline.

Best Practices

  • One change per migration: Keep scripts focused so they’re easy to reason about and rollback.
  • Keep them immutable: Never edit a versioned script once it’s been applied; create a new version instead.
  • Use transactions: Wrap DDL inside BEGIN/COMMIT for databases that allow transactional schema changes.
  • Version lock application code: Fail fast if the application requires a later schema version than is present.
  • Test on realistic data volumes: Flyway won’t detect performance regressions; you must benchmark.

Common Misconceptions

  1. “Flyway is only for greenfield projects.” False—flyway baseline makes it trivial to adopt for legacy databases.
  2. “I can delete old scripts to tidy up.” Never delete versioned migrations; they’re your historical record and needed for new database setups.
  3. “Just re-running migrations is a rollback.” Flyway has no built-in down command. You must write compensating scripts or restore from backups.

Monitoring & Troubleshooting

  • Query flyway_schema_history to confirm versions.
  • Enable -X for verbose logging.
  • Lock the schema (flyway repair) if a migration failed and was partially applied.

Next Steps

Automating migrations is the first milestone. Advanced teams layer in Platform Engineering concepts such as database branching, ephemeral test databases, and automated data masking—each fully compatible with Flyway’s repeatable, predictable workflow.

Why Automating Schema Migrations with Flyway is important

Modern applications iterate rapidly, and the database schema must evolve in lock-step with code. Manual SQL patches are error-prone, environment-specific, and rarely audited. Automating schema migrations with Flyway bakes version control, testing, and deployment into your delivery pipeline, ensuring safe, reproducible database changes that scale with team size and release frequency.

Automating Schema Migrations with Flyway Example Usage


SELECT version, description, success FROM flyway_schema_history ORDER BY installed_on DESC LIMIT 5;

Automating Schema Migrations with Flyway Syntax



Common Mistakes

Frequently Asked Questions (FAQs)

Is Flyway free to use?

Yes. The Community Edition covers the majority of use cases. Teams that need undo scripts, advanced authentication, or Oracle PL/SQL parsing can upgrade to Flyway Teams.

How do I roll back a bad migration?

Flyway itself is forward-only. Create a new versioned script that reverses the change, or restore from a pre-migration backup. Flyway Teams offers undo scripts as an additional feature.

Can I use Galaxy SQL editor to author Flyway migration scripts?

Absolutely. Draft and validate your SQL in Galaxy, then save the final version as a V__ or R__ script in your repository. Galaxy’s AI Copilot can even suggest indexes and constraints before the script enters CI.

What databases does Flyway support?

Flyway works with all major relational databases—including PostgreSQL, MySQL/MariaDB, SQL Server, Oracle, SQLite, Redshift, and Snowflake—ensuring cross-platform consistency.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.