Prisma Migrations
All schema changes go through PrismaPrismaThe TypeScript ORM HiveCFM uses to talk to Postgres. The schema lives at packages/database/schema.prisma. migrations. The workflow is: edit schema.prisma, then run a command that generates SQL and applies it. Never hand-edit tables in production with ALTER TABLE — the migration file is the durable record of the change.
Where things live
hivecfm-core/packages/database/
├── schema.prisma # Source of truth for the schema
├── migration/ # One folder per migration, oldest first (SINGULAR)
│ ├── 20260131161101_add_whatsapp_sms_channels/
│ ├── 20260303000000_add_novu_integration_type/
│ ├── 20260304000000_campaign_novu_refactor/
│ ├── 20260307000000_add_tenant_license/
│ └── 20260405000000_add_under_review_status/
├── src/ # Seed scripts, generators
├── dist/ # Build output: generated client + migration runner
└── package.json # db:migrate:dev, db:migrate:deploy, db:seedEach migration folder contains a migration.sql file. The folder name is <timestamp>_<snake_case_description>; timestamps are generated when you run prisma migrate dev.
Adding a field — the normal workflow
Say you want to add nickname to the User model:
1. Edit schema.prisma
model User {
id String @id @default(cuid())
email String @unique
nickname String? // <- new field
// …
}2. Generate and apply the migration
cd hivecfm-core
pnpm --filter @hivecfm/database db:migrate:devThis command:
- Builds
@hivecfm/database(vite build). - Runs
prisma generateto regenerate the typed client. - Runs the repo’s own
apply-migrations.js, which diffs the schema against the database, creates a new migration folder undermigration/, and applies it.
The script will prompt for a migration name if one is not supplied via --name. Pick a short snake_case description — it becomes the folder suffix and shows up in every git log forever.
3. Commit the new migration folder
git add packages/database/schema.prisma packages/database/migration/
git commit -m "feat(db): add User.nickname"The schema.prisma change and the generated migration/<timestamp>_add_user_nickname/ folder both belong in the same commit. Code review verifies the SQL matches the schema diff.
migration/ is singular. The plural directory is Prisma’s staging area, and authoring into it loses your work.
packages/database/migration/ (singular) is the authored, reviewed source: 204 tracked folders at origin/development, and the only migration path in that package’s package.json files array.
packages/database/migrations/ (plural) is PRISMA_MIGRATIONS_DIR (migration-runner.ts:36). The runner empties it — rm -rf ${PRISMA_MIGRATIONS_DIR}/* (:43) — then copies each singular migration into it (fs.cp, :163) so prisma migrate deploy finds migrations where Prisma expects them, next to schema.prisma. It is generated, every run, from the singular directory.
So a migration that exists only in plural is deleted on the next run and was never packaged. That is the failure mode, and it has caused a login outage before: the schema declared columns whose only DDL lived in plural, the deploy went green because db:migrate:deploy ran and found nothing, and every query selecting those columns returned Postgres 42703 → Prisma P2022.
Two things make this hard to spot. The directory is gitignored (.gitignore:55) yet 6 folders are tracked inside it anyway — force-added past the rule, and .gitignore does not un-track what is already tracked. And those 6 are byte-identical duplicates of folders already under migration/, so the rm -rf deletes them and the copy step recreates them identically on every run — which is why they have been harmless for months, and why git status shows phantom deletions after an interrupted run.
The guard is therefore not “does the plural directory exist” — the runner mkdirs it and every dev machine has one. It is whether any path under it is git-tracked:
git ls-files packages/database/migrations/Anything listed that is not also present under migration/ is unshipped work. Write migrations into migration/.
Migration workflow in production
Production does not run db:migrate:dev. It runs the deploy variant:
pnpm --filter @hivecfm/database db:migrate:deployDifferences:
db:migrate:dev | db:migrate:deploy | |
|---|---|---|
| Generates new migrations | Yes (from schema diff) | No — applies only what exists in migration/ |
| Regenerates the client | Yes | No (client ships with the built image) |
| Interactive | Asks for a migration name | Non-interactive |
| Uses | DATABASE_URL | MIGRATE_DATABASE_URL if set, else DATABASE_URL |
MIGRATE_DATABASE_URL lets ops run migrations through a privileged user while runtime uses a less-privileged one.
Resetting local dev — nuclear option
If your local DB is wedged and you do not care about the data:
cd hivecfm-core/packages/database
pnpm dlx prisma migrate resetThis drops the database, re-applies every migration, and re-runs the seed. Never run this against a shared or production database.
The friendlier alternative for a “I just want to iterate on the schema without making a migration yet” situation:
pnpm --filter @hivecfm/database db:pushdb:push does a direct ALTER TABLE that matches schema.prisma, skipping migration files entirely. Use it for spikes, never commit the change until you have generated a real migration.
Common pitfalls
- Forgetting to rebuild after a schema change.
db:migrate:devtakes care of it; if you edited the schema but used a different script, runpnpm --filter @hivecfm/database buildbefore calling the Prisma client. - Editing an applied migration. Do not. Add a new migration that amends the prior one instead.
- Shadow database errors.
prisma migrate devneeds a “shadow” database to diff against. In local dev the postgres container creates it automatically; if you hit “shadow database access denied”, the configured user is missingCREATE DATABASEgrant. - Stale client after
git pull. If TypeScript suddenly complains that a field does not exist, re-runpnpm --filter @hivecfm/database db:migrate:dev— it both migrates and regenerates the client.
.NET analogy
If you have used EF Core migrations, the flow is similar but inverted:
- EF: change C# model, run
dotnet ef migrations add, EF diffs model vs snapshot and writes SQL. - Prisma: change schema file, run
prisma migrate dev, Prisma diffs schema vs live DB and writes SQL.
There is no ModelSnapshot.cs — the migrations folder and the live schema are the full history.
Troubleshooting
See the Migration stuck runbook for the most common production failure modes.