Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npx prisma db push -> dangerous #382

Open
bejoinka opened this issue Jan 2, 2025 · 1 comment
Open

npx prisma db push -> dangerous #382

bejoinka opened this issue Jan 2, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@bejoinka
Copy link

bejoinka commented Jan 2, 2025

(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:

  1. We define a datasource in the schema.prisma file
  2. 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:
    1. Runs npx prisma generate to regenerate the prisma client
    2. Runs npx prisma db push to apply the migration to your database (CAUTION: may destroy / drop tables).
  • 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:
    1. Creates a new migration file.
    2. Applies the migration to your database.
    3. 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

  1. Development Workflow:
   # Make schema changes
   # Then run:
   pnpm migrate:dev -- --name descriptive_name
  1. Production Deployment Workflow:
   # On your production server:
   pnpm migrate:prod
  1. 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.

@bejoinka bejoinka added the enhancement New feature or request label Jan 2, 2025
@haydenbleasel
Copy link
Owner

Hey @bejoinka this is a great callout! I was using prisma db push because I do my database changes quick and dirty, but we really should improve this. Totally open to checking out a PR that implements your improved workflow!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants