2.8 KiB
2.8 KiB
Migrations And Seeders Backend
Purpose
Schema creation and reference-data seeding run through a small Umzug runner (it replaced
sequelize-cli during the TypeScript/ESM migration). This doc covers the developer mechanism:
the runner, file conventions, and how to add a migration or seeder. For VM/PM2 operational use
(when migrate/seed run on deploy, db:reset recovery), see ../../docs/deployment-vm.md.
Files
- Runner:
src/db/umzug.ts— builds twoUmzuginstances (migrator,seeder) and a small CLI. - Reset:
src/db/reset.ts— drops every table in thepublicschema, then re-runsmigrator.up()+seeder.up(). - Schema snapshot:
src/db/initial-schema.ts— DDL snapshot derived from the Sequelize models; the models remain the source of truth. - Migrations:
src/db/migrations/*.ts— currently20260610000000-initial-schema.ts(creates the full schema from the snapshot). - Seeders:
src/db/seeders/*.ts—admin-user,user-roles,product-campuses,content-catalog(+ payloads underseeders/content-catalog-data/).
Mechanism
migratorglobsmigrations/*.{ts,js}; history is tracked in the defaultSequelizeMetatable viaSequelizeStorage.seederglobsseeders/*.{ts,js}; history is tracked in a separateSequelizeDatatable.- Each file is ESM TypeScript with a default export
{ up, down }, each taking(queryInterface, Sequelize). The runner accepts either adefaultexport or top-levelup/down. - Tracked names strip the
.ts/.jsextension, so history is stable whether the runner is executed viatsx(dev,.ts) or compiled (prod,dist/.../*.js). - The
globaccepts both.tsand.js, so already-applied entries are not re-run after a build.
CLI And Scripts
src/db/umzug.ts exposes: migrate:up, migrate:down, migrate:pending, seed:up,
seed:down. npm scripts wrap them:
- Dev (via
tsx):db:migrate(migrate:up),db:migrate:undo(migrate:down),db:migrate:pending,db:seed(seed:up),db:seed:undo(seed:down),db:reset(tsx src/db/reset.ts). - Prod (compiled, no
tsx):db:migrate:prod(node dist/db/umzug.js migrate:up),db:seed:prod(node dist/db/umzug.js seed:up).
Authoring A New Migration / Seeder
- Add
src/db/migrations/<timestamp>-<name>.ts(orseeders/...) exportingexport default { up, down }with typed(queryInterface, Sequelize)signatures. - Run
npm run db:migrate(ordb:seed) in dev; verify withdb:migrate:pending. - Regenerate
database-schema.mdafter any schema change (it is generated from the models).
Tests
None yet.
Related
database-schema.md(the generated schema reference),backend-architecture.md(DAL layer),../../docs/deployment-vm.md(operational migrate/seed on the VM).