-
Notifications
You must be signed in to change notification settings - Fork 591
Description
(EDIT: First of all, I love this repo. Nice work! 🎉 )
Problem
Is your feature request related to a problem? Please describe.
pnpm migrate
is a wonderful first step, and prisma db push
is just fine when you're first developing a project. however, it becomes dangerous quite quickly, and as you are probably aware should never be used in any sort of CI pipeline.
Solution
Describe the solution you'd like
here's a snippet i've used in a repository at my company. i've modified some pieces of it to fit this repo.
Overview
This document outlines the procedures and best practices for managing database schema migrations using Prisma ORM within the [company repo].
Making Updates to the Database Schema
There may be times you need to update the database schema (the Prisma schema). A couple of items of note:
- We define a datasource in the
schema.prisma
file - Running
pnpm migrate:local
may cause destructive actions
Prisma Commands
pnpm db:gen
(runs npx prisma generate under the hood)
- Use when: You've made changes to your Prisma schema that only affect the TypeScript types.
- What it does: Updates the Prisma Client types in your
node_modules
. - No database changes are made.
- Example: Adding JSDoc comments or changing field names in your schema.
pnpm migrate:local
- Use when: You're in local and making schema changes that affect the database.
- What it does:
- Runs
npx prisma generate
to regenerate the prisma client - Runs
npx prisma db push
to apply the migration to your database (CAUTION: may destroy / drop tables).
- Runs
- Best for: Local environments or environments where you don't care about data integrity.
- Example: Quickly iterating or standing up a new project
pnpm migrate:dev
- Use when: You're in development and making schema changes that affect the database.
- What it does:
- Creates a new migration file.
- Applies the migration to your database.
- Regenerates Prisma Client.
- Best for: Development environments.
- Example: Adding new tables, columns, or relationships.
npx migrate:prod
- Use when: Deploying to production or staging environments.
- What it does: Applies pending migrations to the database.
- Does NOT create new migrations.
- Best for: Production environments.
- Example: Deploying your app to staging/production servers.
Best Practices
- Development Workflow:
# Make schema changes
# Then run:
pnpm migrate:dev -- --name descriptive_name
- Production Deployment Workflow:
# On your production server:
pnpm migrate:prod
- Type-Only Changes:
# After updating comments or type-only changes:
pnpm db:gen
Comparing prisma db push
to prisma migrate
Functionality
Feature | prisma db push |
prisma migrate dev / deploy |
---|---|---|
Fast & simple | ✓ | ? |
Good for prototyping | ✓ | ? |
Creates migration history | ✘ | ✓ |
Safe for production | ✘ | ✓ |
Can be rolled back | ✘ | ✓ |
More setup required | ✘ | ✓ |
Slower development cycle | ✘ | ✓ |
Long story short, be careful with pnpm migrate:local
and prisma db push
.
Next steps
Maybe you don't want to go this far, but given you bill this repo as "production-ready" I thought this may be helpful. LMK if you'd like me to put together a PR with these changes. I'm not sure if you're trying to do something for any OS, but i'd also be open to starting a scripts folder to help with this sort of thing.