Migration Failure Recovery

Migration failure recovery is the process of diagnosing, repairing, and safely resuming database changes after a migration applies only partially or fails. A Java backend engineer should treat the migration state, database contents, and application compatibility as separate concerns.

A migration can fail after executing some statements, leaving the database in a partially changed state. The first step is to determine exactly which statements ran, what the migration tool recorded as applied, and whether the database transaction was rolled back or committed partially.

Recovery depends heavily on the database and migration framework. Transactional DDL can allow an entire migration to roll back, while non-transactional or mixed DDL may leave durable changes behind. Never assume that a failed migration means the database is unchanged.

A safe repair starts with a backup or verified recovery point when the environment warrants it. Inspect the schema and migration metadata, identify the incomplete operation, and choose between rollback, a corrective migration, or manual repair followed by updating the migration history according to the framework's documented procedure.

After repair, rerun or resume migrations in a controlled environment and verify both schema state and application behavior. Production recovery should also consider concurrent application instances, locks, data transformations, constraints, and whether an application release expects the old or new schema.

For Java services, tools such as Flyway and Liquibase commonly manage migration history, but the recovery mechanics are framework- and database-specific. The key engineering principle is to make the resulting database state explicit and reproducible rather than simply marking a failed migration as successful.

Key points

Interview questions

What would you do if a production database migration failed halfway through?

First, I would stop blindly retrying it and determine exactly what executed. I would inspect the migration framework's history together with the actual database schema and relevant database logs. Then I would decide whether the correct recovery is rollback, manual repair followed by a controlled retry, or a new corrective migration. Before resuming application traffic, I would validate the resulting schema and consider compatibility with currently running application versions.

Why can a failed migration leave the database partially modified?

Not every database treats DDL as fully transactional. Some statements may commit independently or may not participate in the same transaction as other migration statements. Therefore, a later failure does not necessarily undo earlier schema changes. The exact behavior depends on the database, statement types, and migration framework configuration.

When is a corrective migration safer than editing migration history?

A corrective migration is generally preferable when the database contains changes that need to be brought to a known target state. It creates an explicit, versioned record of the repair that can be applied consistently to other environments. Editing history can hide what actually happened and may cause future environments to diverge. The exact procedure should still follow the migration framework's recovery guidance.

How would you design migrations to make failures easier to recover from?

I would favor small, independently understandable migrations and avoid combining risky schema changes with large data transformations when practical. I would use an expand-and-contract approach for changes that must coexist with multiple application versions. Each migration should be tested against realistic data volumes and operational constraints such as locks and execution time. Deployment automation should also make migration status observable and provide a documented recovery procedure.