database · migrations · reliability · ecowell · deployment
Rehearse Every Database Migration Before It Touches Anything
On our Ecowell project the database is owned and administered by another team. I write the schema changes, but I do not get to run experiments on the shared database. At one point ten migrations were waiting to be applied, and my backend code depended on all of them.
That setup forced a habit I now think every team should have: rehearse each migration where it cannot hurt anything.
The rehearsal
A migration is just SQL. PostgreSQL lets you run SQL inside a transaction and then undo all of it:
BEGIN;
\i 20261124010000_new_migration.sql -- run the change
-- now exercise the real flows: create a booking, an invoice, a return
ROLLBACK; -- nothing was kept
Running the migration is not enough. The valuable part is the line in the middle: use the new schema the way the application will. A migration can apply without errors and still break the code that depends on it.
What the rehearsals caught
| Problem | How it showed up | Fix |
|---|---|---|
| A migration written by one of my coding agents | Every new booking insert would have failed | Rewritten before it reached any real database |
| Booking and invoice numbers | The generator counted rows by creation time. Back-dated legacy rows made it hand out duplicate numbers | Use the highest existing serial instead |
| A snapshot table that is deliberately immutable | Change requests could not edit it | A flag that lives only inside one transaction allows the change |
None of these showed up in unit tests that used a fake database. They only appeared against real PostgreSQL with realistic data.

When another team owns the database
The ten waiting migrations taught the second lesson. I was told clearly not to touch the shared database, so my job was:
- Keep the migrations small, ordered by timestamp and safe to read.
- Write down the exact list that needed applying.
- Do not deploy the code until the other team confirms the migrations are in. Code that expects a column that does not exist is worse than old code.
- After they applied them, check the migration table and deploy.
It is slower than doing everything yourself. It is also why nothing broke when seven more migrations went live on the following days.
Rollback is part of the migration
Before I ship, I write down how to undo it. For a change that added a database trigger, the plan was one line: drop the trigger and redeploy the previous version. If I cannot write the undo step, I split the migration into smaller ones until I can.
The takeaway
Treat a migration as code that runs once, on data you cannot get back. Rehearse it in a transaction you roll back, use the schema the way the app will, and release in order: schema first, code second.