Implements a complete migration framework for the AetherMint backend with up/down migration support, transaction wrapping, locking, and status tracking.
Migration runner class (Migrator) providing:
up([count])— runs pending migrations (optionally limited to N) inside individual transactionsdown([count])— rolls back applied migrations in reverse orderstatus()— prints a table of all migrations with their applied/pending/failed state- Advisory lock (
pg_try_advisory_lock) prevents concurrent migration execution - A
_migrationstable tracks history: id, name, executed_at, duration_ms, status, error - Failed migrations are recorded with status
'failed'and do not block subsequent migrations
- Rewrote from knex API to raw PostgreSQL queries via
pg.Pool, consistent with the rest of the project - Preserved all existing functionality including the
helpersobject forcreateInitialVersions,migrateExistingContent, andvalidateMigration - Added
IF NOT EXISTS/IF NOT EXISTSguards for idempotency
- Already uses
pg.Poolwith properup/downexports — no changes needed
Added scripts:
npm run migrate:up— runs all pending migrationsnpm run migrate:down— rolls back the last migrationnpm run migrate:status— shows migration status table
Both migrate:up and migrate:down accept an optional count argument (e.g. npm run migrate:down 2).
Auto-runs pending migrations on startup when AUTO_MIGRATE=true is set in the environment (disabled by default).
Each migration file under backend/migrations/ must export:
exports.up = async function(pool) { /* apply */ };
exports.down = async function(pool) { /* revert */ };where pool is a pg.Pool instance.
cd backend
npm run migrate:status # shows pending migrations
npm run migrate:up # apply all
npm run migrate:status # confirm all applied
npm run migrate:down # roll back last
npm run migrate:down 2 # roll back last 2
AUTO_MIGRATE=true npm run dev # auto-runs on startcloses #131