Skip to content

Prisma Guide (Development & Production)

Development Workflow

Daily Operations

Terminal window
# Get latest changes
git pull
# Sync with database
npx prisma db pull
# Generate client
npx prisma generate

Schema Changes

Terminal window
# After modifying schema.prisma
npx prisma migrate dev --name description_of_changes
# Check through GUI
npx prisma studio

Database Reset (Dev Only)

Terminal window
# Removes all data and reapplies migrations
npx prisma migrate reset

Seed Data

Terminal window
# Populate database with test data
npx prisma db seed

Production Operations

Applying Migrations

Terminal window
# PRODUCTION ONLY - applies existing migrations
npx prisma migrate deploy

Status Check

Terminal window
# Check migration status
npx prisma migrate status

Schema Management

Schema Validation

Terminal window
# Format schema.prisma
npx prisma format
# Validate schema
npx prisma validate

Database Synchronization

Terminal window
# Get current schema from database
npx prisma db pull
# Push schema to database (dev only!)
npx prisma db push

Team Collaboration

Handling New Changes

Terminal window
git pull
npx prisma generate

Creating Changes

Terminal window
# 1. Create branch
git checkout -b feature/name
# 2. Modify schema.prisma
# 3. Create migration
npx prisma migrate dev --name description
# 4. Commit ALL files
git add prisma/schema.prisma
git add prisma/migrations/*
git commit -m "feat: description"

Important Rules

Production Restrictions

❌ Never use in production:

  • prisma migrate dev
  • prisma migrate reset
  • prisma db push

Development Restrictions

❌ Never in development:

  • Manually delete migration files
  • Modify existing migrations
  • Make direct database changes

Troubleshooting

Migration Conflicts

Terminal window
# 1. Save changes
git stash
# 2. Get latest changes
git pull
# 3. Reset database
npx prisma migrate reset
# 4. Restore changes
git stash pop
# 5. Create new migration
npx prisma migrate dev

Schema Drift

Terminal window
# Check current schema
npx prisma db pull
# Fix differences
npx prisma migrate dev

Project Structure

prisma/
├── schema.prisma # Main schema
├── migrations/ # Migration files
└── seed.ts # Test data script

Environment Variables

Development (.env)

DATABASE_URL="postgresql://user:password@localhost:5432/dbname"

Production (.env.production)

DATABASE_URL="postgresql://prod_user:prod_pass@prod.host:5432/prod_db"

Best Practices

✅ Always:

  • Test migrations in dev before production
  • Commit all migration files
  • Use clear migration names
  • Backup before applying production migrations
  • Coordinate major schema changes with the team