# What Is a Database Migration? How a Schema Changes Without Breaking Everything

To put it simply, a database migration is a small, numbered, reversible change to a database's shape — the controlled way a schema grows up without breaking the app that depends on it.

Here is the whole job in one paragraph: you never edit a live database by hand. You write the change as a script, the script runs in order, every server gets the same result, and if anything goes wrong you can run it backwards.

A database schema is a promise. When your app says "every user has a name and an email", the schema is the thing that makes that true. But apps grow. One day the product team wants nicknames. Another day an email column needs to be longer, or a table needs to be split in two, or a column that nobody uses any more needs to disappear. The promise has to change — and it has to change on a database that is already full of real people's data, while the app is still running, with no downtime and no lost rows.

Think of renovating a house while a family still lives in it. You cannot knock down the kitchen while dinner is cooking, and you cannot rip out the bathroom on a Monday and hope a new one appears by Tuesday. You do it in small steps, in an order that always leaves the house liveable: build the new bathroom first, move everyone over, and only then remove the old one. A migration is the same discipline applied to a database. Small steps, in order, always leaving the app working.

![A database migration at a glance](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/ef1574c4-a433-4580-af4d-c6ce72111123.png align="center")

## A migration is a script, not a mouse click

The temptation, especially early on, is to open a database tool and change the table by hand. Add a column here, rename one there. It feels fast. It is fast — once. Then the change exists in exactly one place: the database you clicked on. Your teammate's database does not have it. The test server does not have it. Production does not have it, or has a slightly different version of it, done on a Friday afternoon from memory. The app works on your machine and breaks everywhere else, and nobody can say why, because there is no record of what changed.

A migration fixes that by making the change a file. The file has a name, a number, and a date, and it lives in the code repository next to the app. When the app starts, a migration tool checks which numbered files have already run and runs the new ones, in order, on every database it touches. Your machine, your teammate's machine, the test server, production — all of them end up with the same schema, because all of them ran the same files. The schema becomes part of the code, reviewed in pull requests like any other change.

Here is the smallest version that works. The product team wants nicknames, so you add a column:

```sql
-- V2__add_nickname_to_users.sql
ALTER TABLE users ADD COLUMN nickname VARCHAR(50);
```

That is all it does. It adds one column, called `nickname`, to the `users` table, holding up to fifty characters, and it leaves every existing row untouched — the old rows simply have no nickname yet. The filename matters as much as the SQL: the `V2` means it runs second, after `V1`, on every database, in the same order, forever. Tools like Flyway and Prisma Migrate read these files and run them for you. The habit of naming and numbering is the migration. The SQL is just the payload.

![Adding a nickname column, step by step](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/f9fe4403-b199-4cc2-82d4-e6d77e6be988.png align="center")

## Every migration needs a way back

The rule that separates careful teams from lucky ones is reversibility. Before you run a migration, you should know how to undo it. Sometimes the undo is another migration file that reverses the change:

```sql
-- The way back, if the nickname idea fails
ALTER TABLE users DROP COLUMN nickname;
```

That is the easy case. The hard cases are the destructive ones, and the honest truth is that some changes cannot be undone — dropping a column deletes its data, and no script brings it back. That is why careful teams treat destructive migrations as a two-step dance across two releases. First, stop using the column but keep it: the app stops reading it, the column stays in the database, full of data, doing no harm. Then, a release or two later, once everyone is sure nothing reads it any more, a second migration removes it. The gap between the steps is the safety net. It is slow, it is boring, and it has saved more weekends than any clever trick I know.

There is one more trap, and it is the one that bites in production specifically: locks. Changing a table's shape on a small database takes a blink. On a table with ten million rows, adding a column can lock the whole table while the database rewrites it, and every request that touches that table waits. Users see a frozen app. The fix is the same as the rest of this post: small steps. Add the column without a default value first — that is instant on most databases — then fill the values in separately, in batches, and only then add the constraint. PostgreSQL and MySQL both reward this patience, and punish the alternative in ways that show up at 2 a.m.

## The order that keeps everything running

Migrations have an order problem that catches every team once. The app code and the database change together, but they do not deploy together. The database changes first, while the old app is still running. Then the new app deploys. That means the migration has to be compatible with the old code for a little while — and that is why the expand-then-contract pattern exists.

It works like this. Expanding: add the new column, the new table, the new index, without removing anything the old code needs. The old app keeps running happily, ignoring the new things. Then you deploy the new app, which starts using them. Contracting: a later migration removes the old column, the old table, the things nobody needs any more. Two releases, three steps, zero downtime. It feels like overkill on a side project with one user. On a system with real traffic it is the difference between a boring Tuesday deploy and an incident.

I learned this one the expensive way, on a project small enough that I thought the rules did not apply. One rename, done by hand, on a Friday, while the app was running. The column was called `user_name`, the code wanted `username`, and for about forty minutes every login failed while I tried to remember exactly what I had clicked. The fix took ten minutes. The lesson lasted: never rename a live column in one step, never change a schema by hand, and never deploy a database change on a Friday.

![Expand, then contract](https://cdn.hashnode.com/uploads/covers/6a12efab551486ce6cffb0f8/cc353928-52af-46de-b075-d1660067f7e8.png align="center")

A schema is a living thing, and a migration is how it grows without breaking. Write the change down, number it, make it reversible, and deploy it in steps small enough that any one of them can fail safely. Do that, and your database will still be standing when your app is ten times bigger than it is today.

## References and further reading

- [Schema migration — Wikipedia](https://en.wikipedia.org/wiki/Schema_migration)
- [Flyway documentation — Getting started](https://documentation.red-gate.com/flyway)
- [Prisma Migrate — Prisma documentation](https://www.prisma.io/docs/orm/prisma-migrate)
- [ALTER TABLE — PostgreSQL documentation](https://www.postgresql.org/docs/current/sql-altertable.html)

— Min SiThu
Software Engineer & Tech Content Creator
https://minsithu.org
