Database Integration with PostgreSQL and PrismaLesson 5.5
Prisma migrations โ managing database schema changes safely
prisma migrate dev, prisma migrate deploy, migration files, schema drift, rolling back, adding columns, prisma db push, production workflow
Managing Schema Changes with Prisma Migrate
Database schemas change over time. Prisma Migrate tracks every schema change as a versioned SQL migration file, giving you a reproducible history of how the database evolved.
Development Workflow
# Make schema changes in schema.prisma, then:
npx prisma migrate dev --name add_post_published_field
# This: generates a SQL migration file
# applies it to your dev database
# regenerates the Prisma Client-- Example generated migration file
ALTER TABLE "Post" ADD COLUMN "published" BOOLEAN NOT NULL DEFAULT false;Production Workflow
# In CI/CD pipeline or deployment script:
npx prisma migrate deploy
# Applies all pending migration files (never generates new ones)
# Safe to run in production โ only applies, does not modify schema.prismaCritical Rules
Never edit migration files after they are committed. Never use prisma migrate dev in production โ use migrate deploy. Use prisma db push only for prototyping (it doesn't create migration files and will cause drift). Before adding a NOT NULL column to a production table with existing rows, always provide a default value or run a data migration first to avoid constraint failures.
