What is a database migration?
Migrations are the change log of your database structure. Here is how they work, which changes are safe on a live app, and how to back out of one without losing data.

Key takeaways
- A migration is a small, timestamped SQL script that changes the database structure; they run once each, in order.
- Adding tables and optional columns is safe; renaming, dropping and changing types on live data needs a careful sequence.
- Never edit a migration that already ran. Write a new one that reverses or corrects it.
Think of a database migration as one numbered step in the history of your database's structure: a small script that changes the schema in a specific way, such as creating a table, adding a column or adding an index, recorded so that every copy of the database ends up with the same shape.
Renovating a house works the same way. You do not redraw the whole building each time; you keep numbered work orders: 1, add a porch; 2, move the kitchen door; 3, add a window. Anyone holding the list can bring an identical house up to date by doing the orders they have not done yet.
How migrations work
Each migration is a file whose name starts with a timestamp, for example 20261122093000_add_phone_to_profiles.sql, containing something like `alter table profiles add column phone text;`. The database keeps a small table recording which migrations have run. Applying migrations means running, in order, every file not yet recorded. That is how a fresh test database and the live one stay identical, and how a teammate's change reaches yours.
Safe changes and risky ones
On a live app with real users, changes fall into two groups:
- Usually safe: creating a new table, adding an optional column, adding a column with a default, adding an index (large tables may need it built without locking writes).
- Needs care: renaming a column (the running app still asks for the old name), dropping a column or table (data gone), changing a type (text to number fails on "n/a"), making an existing column required (fails if any row is empty).
Risky changes are done in steps. To rename full_name to display_name: add display_name, copy the data over, switch the app to read the new column, and only in a later migration drop the old one. To make a column required: fill in the empty rows first, then add the rule. Back up before anything destructive.
Undoing a migration
Once a migration has run, treat it as history. Editing the old file changes nothing on databases that already ran it and makes copies drift apart. The fix is a new migration that reverses or corrects the change. Dropped data does not come back from a script, which is why destructive steps come last and only after a backup. This is different from restoring a version of your code: rolling back files does not by itself rewind what is stored in the database (checkpoints and undo).
Migrations when building with Startbase
In a Startbase Cloud project, the agent manages your schema through SQL migrations in the project and normally applies them as part of its work. The Schema Sync panel shows applied and pending migrations and whether a seed file exists, with Apply migrations and Apply seed buttons for when you want to run them yourself (database docs). Daily backups are taken automatically; point-in-time recovery is a paid database add-on. When asking for a structural change to a live app, say that real data exists and must be kept.
The app is live with real customers. Rename the bookings column "note" to "customer_note" without losing data: add the new column, copy existing values, switch the app to the new column, and leave dropping the old column for a separate migration I will approve later. Show me the migration SQL before applying it.
Common confusions
- Schema migration vs data migration. A schema migration changes structure; a data migration moves or transforms rows (also "migrating" from one provider to another). The steps above often mix both.
- Migration vs seed. Seed files load starter or demo data once; migrations change structure.
- Migrations vs backups. A migration history rebuilds the shape, not the rows. Only backups restore data. Background in database schema and from prototype to production.
Frequently asked questions
Why not just edit the database directly?
Manual edits are not recorded, so test and live databases drift apart and changes cannot be repeated or reviewed. Migrations make every structural change reproducible.
Can a migration delete my data?
Yes, if it drops a table or column or changes a type destructively. Do those steps last, after copying data and taking a backup.
How do I undo a migration?
Write a new migration that reverses it. Editing or deleting one that already ran leaves databases out of sync.

