Change the shape of a live database safely, without breaking the running app.
A schema migration is a planned change to the shape of a database: add a column, rename a table, change a type. The hard part is doing it while the app is live, with real users and several app versions running together.
Your database has a schema: the tables, columns, types and rules. When the product grows you must change it. A tool such as Flyway, Liquibase, Alembic, Rails migrations or Prisma Migrate keeps a numbered list of migration files. Each file says how to move the schema from version N to N+1. The tool records which ones have already run, so every environment ends up the same.
On a toy project you can stop the app, run the migration, and start again. A real service cannot stop. With a rolling deployment there is a time when some servers run the old code and some run the new code. Both talk to the same database. So every schema state in between must be compatible with both versions.
| Change | Safe for old code? | How to do it well |
|---|---|---|
| Add a nullable column | Yes | Usually the easy first step. Old code ignores it. |
| Add a column with a default | Depends | Newer versions of some databases do it instantly. Older ones rewrite the whole table. Check your version. |
| Add a NOT NULL column | No, if old code does not set it | Add it nullable, backfill, then add the constraint. |
| Add an index | Yes, but can lock | Build it online or concurrently (for example CREATE INDEX CONCURRENTLY in PostgreSQL). |
| Rename a column or table | No | Add the new one, write both, backfill, switch reads, drop the old one. |
| Change a column type | Often no | Add a new column of the new type and copy over, same as a rename. |
| Drop a column | No, if any code still uses it | Stop all use first, deploy, wait, and only then drop. |
| Add a foreign key or a unique rule | Can fail or lock | Add it as not validated, clean the data, then validate. |
| Way | How it works | Watch out |
|---|---|---|
| Before the deploy | Run the migration, then roll out the new code | The schema must work for the OLD code. Fits expand steps. |
| After the deploy | Roll out the new code, then run the migration | The NEW code must work on the old schema. Fits contract steps. |
| At app start | Each server runs migrations when it boots | Many servers may race. Use a lock, or run it as one separate job. |
A simplified model: 4 app servers behind a load balancer, an even spread of requests, and a rename done as a single instant change. Real databases differ in how long a change locks a table, so large tables need extra care (batches, online schema tools).
-- 1. Expand (migration 1) ALTER TABLE users ADD COLUMN full_name TEXT NULL; -- 2. Deploy app v2: write name AND full_name, read full_name (fallback to name) -- 3. Backfill, in batches, repeated until 0 rows are updated UPDATE users SET full_name = name WHERE id IN (SELECT id FROM users WHERE full_name IS NULL LIMIT 5000); -- 4. Contract (a later migration, after v1 is gone for good) ALTER TABLE users DROP COLUMN name;
| Term | Meaning in easy words |
|---|---|
| Schema | The structure of a database: tables, columns, types, keys and rules |
| Migration | A versioned script that moves the schema from one version to the next |
| Expand and contract | Add the new thing first (expand), move over, and remove the old thing last (contract) |
| Parallel change | Another name for expand and contract |
| Backfill | Filling a new column with values for rows that already exist |
| Backward compatible | The new schema still works for the old code |
| Rolling deployment | Replacing servers with a new version a few at a time |
| Online schema change | Changing a big table without blocking reads and writes for long |
| Lock | A hold on a table or row that makes other queries wait |
| Down migration | A script that undoes a migration. Often impossible if data was lost |
| Dual write | Writing the same data to the old and the new place during a move |
| Choice | We gain | We lose |
|---|---|---|
| One-step change in a maintenance window | Simple, one release | Planned downtime, and a risky all-at-once change |
| Expand and contract | No downtime, easy undo | More releases and temporary clutter |
| Backfill in small batches | No long locks, less replica lag | A longer total time |
| Backfill in one big update | Fast to write | A long lock, slow queries and possible timeouts |
| Drop the old column at once | A clean schema right away | No way to roll the app back |
| Wait before dropping | A safe rollback path | Extra storage and a task to remember |