Migrations cause some of the strangest local bugs: a restart that re-runs them, two instances racing, an API that boots before the table exists. Ten rules that avoid most of it.
- Run migrations as their own step. Not inside the API start script.
- Order it. After Postgres is ready, before the API starts.
- Wait for readiness. A started container is not a database that accepts connections.
- Make migrations idempotent. Running twice must be safe.
- Run once, not per instance. Two API copies must not both migrate.
- Keep them in version control. Numbered, reviewed, never edited after release.
- Fail loudly. A failed migration should stop the start, with a clear log line.
- Warn on the anti-pattern.
flisk doctorwarns when a service runs a migration from its own start script. - Test from an empty database. Fresh clone, empty volume, full start.
- Test the rollback plan. Know what you do when one fails halfway.
Check
Delete the database volume and start again. If the stack comes up with the right schema, your order is right.
