3.7 KiB
3.7 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—20260610000000-initial-schema.ts(creates the full schema from the snapshot) and20260610010000-add-role-scope-and-user-campus.ts(adds the NOT-NULLroles.scopeenum and the nullableusers.campusId). Phase 4 adds20260611000000-policy-documents-and-acknowledgments.ts(the unified policy store + per-version acknowledgments),20260611010000-audio-files.ts(the audio library) +20260611060000-audio-files-kinds.ts(thekindenum / nullableurl/recipeJSONB), and20260611040000-add-user-name-prefix.ts(theusers.name_prefixhonorific enum). - Seeders:
src/db/seeders/*.ts—admin-user(the 10 per-role RBAC fixture users),user-roles(the 11 first-class roles, the permission catalog incl. product-feature permissions, the role->permission matrix, role assignment by user id),product-campuses,content-catalog(+ payloads underseeders/content-catalog-data/),rbac-fixtures(the company, campus->org ownership, per-user org/campus links, staff profiles), and20260611050000-policy-documents-seed.ts(3 safety protocols + 4 handbook policies). Shared fixture definitions live insrc/shared/constants/seed-fixtures.ts.
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).