The workflow
-
Change a
.prismafile inpackages/database/prisma/. -
Create and apply a migration against your development database:
pnpm migratePrisma compares the schema with the database, asks for a name, writes
prisma/migrations/<timestamp>_<name>/migration.sql, applies it and regenerates the client. -
Read the SQL. Check for
DROP, renames and type changes before you commit. -
Commit the schema and the migration folder together.
Never edit a migration that has already been applied anywhere. Fix mistakes with a new migration.
Deploying migrations
Nothing in the build or the Docker image migrates your database. Apply migrations as a release step, before or while the new code rolls out:
DATABASE_URL="postgresql://…production…" pnpm --dir packages/database migrate:deploy
migrate deploy applies pending migrations in order and never prompts or resets. Run it from CI, a release script, or your machine. pnpm doctor --check against the production env shows how many migrations are pending.
Writing safe migrations
Old and new code run side by side during a deploy, so prefer additive changes:
| Change | Safe approach |
|---|---|
| Add a column | Make it nullable or give it a default |
| Add a table or index | Safe |
| Rename a column | Add the new column, backfill, switch the code, drop the old one in a later release |
| Change a type | Same as a rename: new column, backfill, switch, drop later |
| Drop a column or table | Remove the code that uses it first, drop it in a later release |
Add NOT NULL | Backfill first, then add the constraint |
For data changes, write SQL in the migration file or create an empty migration with pnpm --dir packages/database exec prisma migrate dev --create-only --schema prisma and fill it in.
Migrations in a download
Each module owns its migrations. A download without a module does not contain that module's migration folders, and statements a module added to a shared migration are removed. The remaining migrations always produce exactly the schema of your variant; SaaS Forge's nightly build applies them to an empty database and checks for drift.
When an upgrade adds a module, its migrations come with the kit. Run pnpm migrate locally and migrate:deploy in production.
Adding the Organizations module to an existing database
Existing users have no workspace. After migrating, create a personal workspace for each of them:
pnpm --dir packages/database backfill:orgs
Seeding
pnpm seed runs packages/database/src/seed.ts. It needs NEXT_PUBLIC_SAAS_NAME (read from apps/web/.env) and:
- upserts the landing page row named after your product, with the values in
src/constants/; - replaces hero images, features, testimonials, pricing plans, FAQs and footer links;
- upserts every documentation page in
src/constants/docs.tsby slug; - creates the default AI prompts (
chat.assistant,documentation.assistant,cms.assistant) if they do not exist (AI Platform module); - upserts the demo guest user (
DEMO_GUEST_EMAIL, default[email protected]) with theguestrole.
Seeding is safe to repeat in development. In production it overwrites landing sections you edited in the admin panel, so seed production once, at setup.
Resetting
pnpm reset
Drops every table, re-applies all migrations and seeds. Development only.
Troubleshooting
- "Drift detected" or a reset prompt from
pnpm migrate: the database was changed outside migrations. Never accept a reset on a database with data you need. Compare withprisma migrate diff, or pointpackages/database/.envat a fresh database. P3009failed migration in production: fix the cause, thenprisma migrate resolve --applied <name>or--rolled-back <name>and deploy again.type "vector" does not exist: the database lacks pgvector (AI Agents module). Use Neon, Supabase or thepgvector/pgvectorimage.- Types are stale after a schema change:
pnpm generateand restart the TypeScript server.
