How to Write Safe Database Migrations from a Schema Diff
A practical guide to comparing two SQL schemas, spotting renames and destructive changes, and writing up and down migrations for PostgreSQL and MySQL.

How to Write Safe Database Migrations from a Schema Diff
Most database incidents during a release are not caused by clever bugs. They come from ordinary migration mistakes: a column renamed in the code but dropped and re-added in the database, a NOT NULL column added to a table with millions of rows, or an index removed that a hot query depended on. A schema diff turns these risks into a short, readable list before anything runs. This guide explains how to use one well, with the free schema diff and migration generator.
Start from the real schema
The "current" side of a diff must describe what is actually running, not what someone remembers. Export it from the database itself: pg_dump --schema-only dbname for PostgreSQL or mysqldump --no-data dbname for MySQL. The "target" side is the schema your new code expects, often a schema.sql file in the branch or the output of a design tool. Comparing two text files means you never have to give a tool access to production.
Read the change list before the SQL
A good review starts with the list of differences, not the script. Group them in your head:
- Additive changes (new tables, nullable columns, new indexes) are usually safe.
- Changing changes (type, nullability, defaults, keys) need a look at existing data.
- Removing changes (dropped columns or tables) lose data and are rarely urgent.
If the list contains something nobody on the team expected, stop there. It usually means the target file is out of date or the database drifted.
Renames deserve special attention
A diff tool cannot know your intent. When name disappears and full_name appears with the same type, the literal difference is "drop one column, add another", and running that would empty the column. Treat such pairs as renames so the script uses RENAME COLUMN. The same applies to tables: a removed table and a new table with mostly the same columns is almost always a rename.
Order matters
A migration script is a sequence, and the order decides whether it runs at all. Foreign keys that will change must be dropped before the columns they use, new tables must exist before anything references them, and new foreign keys should be created last. A sensible order is: drop changing constraints and indexes, apply renames, create tables, add and alter columns, drop old columns and tables, then add primary keys, indexes and foreign keys.
Watch the classic traps
NOT NULL without a default. Adding created_at timestamptz NOT NULL to a table with rows fails. Give it a default such as now(), or add it as nullable, back-fill, and then set NOT NULL in a second step.
Type changes. Widening varchar(120) to varchar(255) is cheap. Changing text to integer needs every value to convert, and on large tables may rewrite the whole table while holding a lock. PostgreSQL needs a USING clause for many conversions.
Transactions. PostgreSQL can run DDL inside a transaction, so a failed migration rolls back cleanly. MySQL commits each DDL statement on its own, so a half-applied migration is possible and a backup matters more.
Always write the down migration
The rollback script is easiest to write at the same moment as the forward one, because you still remember every decision. Generating it by comparing the schemas in the opposite direction, with renames reversed, gives you a tested way back if a release has to be undone. Keep both files together in your migrations folder.
A short checklist
- Export the current schema from the real database.
- Diff it against the target and read every change.
- Mark renames.
- Read the warnings about data loss and NOT NULL columns.
- Keep drops commented out until the release that truly needs them.
- Run the script on a restored copy of production data and time it.
- Store the up and down scripts in version control.
Limits
A schema diff compares structure only. It does not move or transform data, so splitting a name column into first and last names still needs your own UPDATE statements. Server-side objects that the parser skips, such as functions, triggers, partitions and collations, must be reviewed separately. Testing on a copy of real data is the only reliable check of how long a migration takes.



