Deploy
Migrations Checklist
Before Deploy
- Migration file uses
import type { Knex } from 'knex'(NOTimport { Knex }) -- Knex is CJS and migrations run with sucrase/ESM. - Any data inserts use check-before-insert for idempotency:
Code
- Migration tested locally:
cd twin-backend/packages/database && npm run migrate - Migration rolls back cleanly:
cd twin-backend/packages/database && npm run migrate:rollback - No hardcoded IDs (resolve dynamically from DB, e.g.,
SELECT id FROM ops_token WHERE address = ? AND network = ?). - Partial unique indexes use
WHERE deleted_at IS NULLfor soft-delete tables.
Deploy with Migrations
Code
Commands to Run in Production
If migrations need to be run manually (e.g., outside the deploy pipeline):
Code
What Can Go Wrong
Column already exists
If a migration tries to add a column that already exists (e.g., from a partial previous run):
Code
Fix: Wrap in a check: if (!(await knex.schema.hasColumn('table', 'xyz'))).
Batch rollback removes too much
migrate:rollback rolls back the entire last batch, not just one migration. If the batch contained multiple migrations, all of them will be rolled back.
Fix: Be intentional about batches. If you need to rollback a single migration, you may need to do it manually.
Lock stuck
If a migration crashes mid-execution, the knex_migrations_lock table may be left in a locked state, preventing future migrations from running.
Code
Fix:
Code
Or:
Code
Post-Migration Checks
- Verify new tables/columns exist in the database.
- Verify seed data was inserted (if applicable).
- Check that the application is using the new schema correctly (no runtime errors).
- Verify indexes were created (especially partial unique indexes).
- Run a quick smoke test of affected endpoints.
Last modified on