Flyway versioned undo scripts vs. repeatable migrations for zero-downtime rollback
0 reputation · 28 Sept 2026, 11:55 UTC
0 reputation · 28 Sept 2026, 11:55 UTC
Our team uses Flyway 10.x with PostgreSQL 15 in a blue-green deployment pipeline. We need to guarantee that a failed schema change can be rolled back within 30 seconds without data loss or manual intervention. The current baseline uses versioned migrations (V1__..., V2__...) with manually written undo scripts (U2__...).
Versioned undo scripts provide explicit control but require maintaining inverse DML/DDL for every change, increasing review burden and drift risk. Repeatable migrations (R__...) auto-reapply on checksum change but offer no built-in rollback mechanism—Flyway simply re-executes them forward, which is unsafe for destructive changes like column drops.
The constraint is a hard rollback SLA: the database must return to a known-good state automatically after a failed health check, without operator decisions.
undo command in 10.x support conditional execution based on migration type, or must we gate rollback logic in a wrapper script?33775 reputation · 28 Sept 2026, 17:41 UTC
Yes – you can keep repeatable migrations limited to additive, idempotent changes (indexes, views) and use versioned migrations with matching undo scripts for destructive DDL. Flyway’s undo command only works on versioned migrations; it has no built‑in logic for repeatable ones, so the rollback must be orchestrated by an external wrapper script that first runs flyway undo and then re‑applies repeatable migrations if their checksum changed.
flyway undo and give explicit rollback for destructive changes.undo for repeatable migrations; any such logic must be handled outside Flyway.R__* to additive, idempotent changes; place any destructive DDL (DROP, ALTER, data‑loss) in V__* with a matching U__*.# 1. Identify the last applied versioned migration (optional, flyway undo defaults to latest)
flyway info | grep Applied | tail -1 | awk '{print $1}'
# 2. Undo that migration (and any subsequent versioned migrations if multiple failed)
flyway undo
# 3. Re‑apply repeatable migrations if their checksum changed during the rollback window
flyway migrate
# 4. Validate the schema state
flyway validate
flyway undo, run flyway info to confirm the last successful versioned migration is marked Success and no destructive changes remain.flyway validate to ensure the current schema matches the expected baseline.To fine‑tune the rollback script, could you confirm whether your health‑check mechanism can identify the exact migration that caused the failure (e.g., via a database flag or CI/CD log)? This would allow the script to target only that migration instead of a blanket flyway undo.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.