Ordering Schema Migrations: Dependencies and Safer Deployments
A schema migration changes a database through versioned steps. Order those steps by the objects they depend on and by the application versions that must remain compatible—not just by filename. The right sequence for creating a database from scratch may differ from upgrading one already in use. Flyway applies versioned migrations in version order and tracks history and checksums; Alembic uses a revision graph rather than numeric filename order. [Flyway: Versioned migrations](https://documentation.red-gate.com/fd/versioned-migrations-273973333.html) [Alembic: Tutorial](https://alembic.sqlalchemy.org/en/latest/tutorial.html)
Ordering Schema Migrations: Dependencies and Safer Deployments
A schema migration changes a database through versioned steps. Order those steps by the objects they depend on and by the application versions that must remain compatible—not just by filename. The right sequence for creating a database from scratch may differ from upgrading one already in use. Flyway applies versioned migrations in version order and tracks history and checksums; Alembic uses a revision graph rather than numeric filename order. Flyway: Versioned migrations Alembic: Tutorial
Map dependencies first
A migration may add or change a table, column, index, constraint, or data. Record which objects must exist before each change. For example, if orders references customers, a clean install must create the referenced table before adding the foreign key:
create customers → create orders → add the foreign key to customersThis chain illustrates dependencies. It does not mean every arrow needs its own deployment file; depending on the tool and change, several steps can belong in one migration.
Version numbers and dependency graphs are different
Flyway assigns each versioned migration a unique version and applies it in numeric version order. It records applied migrations and checksums. Flyway advises against changing a versioned script after it has reached a permanent downstream environment; make a new script to roll forward a correction. A version sets execution order, but it does not design your code or data dependencies. Flyway documentation
Alembic connects revision scripts through directives such as revision and down_revision. Its documentation says filename order is not the ordering rule. Migrations created on separate branches may form divergent history that must be merged carefully. Confirm how your tool interprets order and agree on branch-merge practices. Alembic documentation
Account for application compatibility
Suppose you add orders.customer_id and a foreign key. On a clean database, create customers, then the order table and column, then the constraint. On a live database, also ask whether the older application can run while the new column exists, whether old rows need a backfill, and whether existing references are valid.
For systems where application and database releases happen at different times, a rollout can use these phases:
Add the new structure in a form the current application can ignore.
Deploy application code that reads or writes the new structure; backfill data if needed.
Remove the old structure in a later change, after no running code depends on it.
This is commonly called an expand-and-contract pattern. It can reduce compatibility problems when deployments or rollbacks are separated, but it is not required for every change. Adding a column first does not automatically solve data conversion, concurrent writes, or rollback concerns.
Resolve parallel work and correct mistakes
List prerequisites, data assumptions, and compatible application versions for each change. Then assign unique versions or revision links using the migration tool’s rules. If one developer creates V12 for customers while another creates a same-numbered migration that references it, a clean build can fail if the team’s ordering rule puts the reference first. Reconcile the version or merge the tool’s dependency graph so every environment has a consistent path.
Do not renumber already-applied migrations without checking the tool and the affected environments. Flyway documents validate as checking differences such as names, types, and checksums between applied and available migrations. If an applied script needs correction, a new forward migration is generally safer than silently editing shared history. Flyway: Validate
Verify both paths
Before production, reproduce a clean database build and an upgrade from the current schema using the same commit. Run the migration tool’s validation command; compare applied versions or revisions with the expected schema; and check transformed row counts, orphan references, and constraints. During rollout, monitor step logs and application compatibility. Use supported recovery or a forward fix if a step fails.
Whether DDL is transactional, causes locks, or needs a long-running backfill depends on the DBMS and operation. Check those details separately, along with backup and restore requirements. The useful rule is: create prerequisites first, coordinate dependent code and data changes at compatible times, and remove old structures last. Validate that rule against the actual tool, database, and deployment path.