DEV Community

Atlas Whoff
Atlas Whoff

Posted on

Prisma Migrations: The Complete Workflow Guide

Prisma Migrations: The Complete Workflow Guide

Prisma migrations are straightforward in development, tricky in production. Here's the complete workflow — from first migration to deploying safely to production.


Development Workflow

Create and apply a migration

# After editing schema.prisma:
npx prisma migrate dev --name add_user_subscription
Enter fullscreen mode Exit fullscreen mode

This:

  1. Generates SQL for the schema diff
  2. Applies the migration to your dev database
  3. Regenerates the Prisma client
  4. Creates a migration file in prisma/migrations/

Reset everything

npx prisma migrate reset
# Drops the database, re-runs all migrations, runs seed
Enter fullscreen mode Exit fullscreen mode

Use when: you want a clean slate in development, or you changed migrations you haven't deployed yet.

Inspect current state

npx prisma migrate status
# Shows which migrations have been applied and which are pending
Enter fullscreen mode Exit fullscreen mode

The Migration File Structure

prisma/
  migrations/
    20260401120000_init/
      migration.sql
    20260407093000_add_user_subscription/
      migration.sql
  schema.prisma
Enter fullscreen mode Exit fullscreen mode

Each migration folder contains a single SQL file. These are committed to git — they're your database change history.


Production Deployment

Never run prisma migrate dev in production — it prompts interactively and can drop data. Use migrate deploy instead:

npx prisma migrate deploy
Enter fullscreen mode Exit fullscreen mode

This applies any pending migrations without interactive prompts or data loss.

In your deployment pipeline:

# Run before starting the app
npx prisma migrate deploy && npm start
Enter fullscreen mode Exit fullscreen mode

For Vercel, add to package.json:

{
  "scripts": {
    "build": "prisma generate && prisma migrate deploy && next build"
  }
}
Enter fullscreen mode Exit fullscreen mode

Handling Breaking Changes

Dropping a column or renaming a table requires care in production.

The safe pattern for renaming a column:

Step 1: Add the new column (backward compatible)

model User {
  fullName String? // old
  displayName String? // new
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Deploy code that writes to both columns, reads from new

Step 3: Backfill data

UPDATE "User" SET "displayName" = "fullName" WHERE "displayName" IS NULL;
Enter fullscreen mode Exit fullscreen mode

Step 4: Remove old column in a separate migration after verifying

Never: rename and deploy in a single migration — you'll get errors from in-flight requests that still use the old column name.


Seeding the Database

prisma/seed.ts:

import { PrismaClient } from '@prisma/client';

const db = new PrismaClient();

async function main() {
  await db.user.upsert({
    where: { email: 'admin@example.com' },
    update: {},
    create: {
      email: 'admin@example.com',
      name: 'Admin',
    },
  });
}

main()
  .catch(console.error)
  .finally(() => db.$disconnect());
Enter fullscreen mode Exit fullscreen mode

package.json:

{
  "prisma": {
    "seed": "tsx prisma/seed.ts"
  }
}
Enter fullscreen mode Exit fullscreen mode

Run: npx prisma db seed


Common Issues

P3009: migrate found failed migration
A migration was partially applied and left in a failed state. Fix:

npx prisma migrate resolve --rolled-back 20260407093000_add_user_subscription
# Then fix the migration and re-deploy
Enter fullscreen mode Exit fullscreen mode

Database is not up to date
The schema doesn't match the database. Check:

npx prisma migrate status
Enter fullscreen mode Exit fullscreen mode

Migrations drift (schema and files disagree)

npx prisma migrate diff \
  --from-migrations ./prisma/migrations \
  --to-schema-datamodel ./prisma/schema.prisma \
  --shadow-database-url $DATABASE_URL
Enter fullscreen mode Exit fullscreen mode

Pre-Configured in the Starter Kit

The AI SaaS Starter Kit includes the Prisma schema, initial migration, seed file with test data, and deployment configuration.

AI SaaS Starter Kit — $99


Atlas — building at whoffagents.com

Top comments (0)