Skip to content
flexcal Documentation

Database migrations

Schema changes go through Prisma migrations so each step is reproducible and reversible. Editing schema.prisma without generating a migration risks data loss, because Prisma has no recorded path from the old shape to the new one.

Development, generates missing migrations and can reset data:

Terminal window
bun run --cwd packages/prisma db-migrate

Production, applies existing migrations only:

Terminal window
bun run --cwd packages/prisma db-deploy

After changing packages/prisma/schema.prisma:

Terminal window
bun run --cwd packages/prisma db-migrate

Prisma asks for a short name describing the change (for example user_add_email_verified). Commit the generated migration alongside the code that depends on it.

Feature flags are seeded through migrations. Create an empty one:

Terminal window
bun run prisma migrate dev --create-only --name seed_<feature_name>_feature

Then fill in the SQL:

INSERT INTO "Feature" ("slug", "enabled", "type", "description", "createdAt", "updatedAt")
VALUES
('your-feature-slug', false, 'OPERATIONAL', 'Description of feature', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON CONFLICT ("slug") DO NOTHING;

Error: the database schema is not empty (P3005)

Section titled “Error: the database schema is not empty (P3005)”

Prisma records applied migrations in a _prisma_migrations table. When that record disagrees with the actual database it refuses to continue:

Error: P3005
The database schema for `localhost:5432` is not empty.

Mark each already-applied migration as resolved:

Terminal window
bun run prisma migrate resolve --applied <migration_name>

When a local database has drifted beyond repair, clear the migration log:

DELETE FROM "_prisma_migrations";

Then re-register every migration in the directory:

Terminal window
ls -1 packages/prisma/migrations/ | grep '^20' | xargs -I{} bun run prisma migrate resolve --applied {}

If TypeScript complains about enum members that clearly exist in the schema, the generated client is stale:

Terminal window
bun run prisma generate