We use tracking cookies to understand how you use the product and help us improve it. For more information on how we store cookies, read our  privacy policy.

Database Migrations

Learn how to run database migrations in production

Learn how to run database migrations safely in production.

Goal

By the end of this recipe, you'll know how to create and apply database migrations in production.

Prerequisites

  • A working Plainform installation
  • Prisma configured
  • Production database access

Steps

Create Migration Locally

Create a migration for your schema changes:

npx prisma migrate dev --name add_new_field

This creates a migration file in prisma/migrations/.

Always test migrations locally before applying to production.

Commit Migration Files

Commit the migration files to Git:

git add prisma/migrations/
git commit -m "feat: add new database field"
git push origin main

Apply Migration in Production

After deployment, run the migration:

Option 1: Automatic (Recommended)

Add to your package.json:

package.json
{
  "scripts": {
    "postinstall": "prisma generate && prisma migrate deploy"
  }
}

Vercel will run migrations automatically on each deployment.

Option 2: Manual

Run manually via Vercel CLI:

vercel env pull .env.production
npx prisma migrate deploy

Verify Migration

Check that the migration applied successfully:

npx prisma studio

Or query your database directly to verify schema changes.

Migration Best Practices

Before Running Migrations

  • Backup your database before major schema changes
  • Test locally with production-like data
  • Review SQL in migration files for correctness

Safe Migration Patterns

Adding columns:

model User {
  id    String @id
  email String
  name  String? // Make new fields optional initially
}

Renaming columns:

  1. Add new column
  2. Copy data from old to new
  3. Remove old column (in separate migration)

Dropping columns:

  1. Remove from application code first
  2. Deploy application
  3. Drop column in separate migration

Avoid breaking changes in migrations. Add new fields as optional, then make required in a later migration.

Common Issues

Migration Fails in Production

  • Check database connection string
  • Verify database user has migration permissions
  • Review migration SQL for syntax errors

Schema Out of Sync

Reset and reapply migrations:

npx prisma migrate reset
npx prisma migrate deploy

migrate reset drops all data. Only use in development.

Migration Conflicts

If multiple developers create migrations:

npx prisma migrate resolve --applied <migration_name>

Rollback Strategy

To rollback a migration:

  1. Revert the migration commit in Git
  2. Create a new migration that undoes changes
  3. Deploy the rollback migration

Prisma doesn't support automatic rollbacks. Always create a new migration to undo changes.

Next Steps

How is this guide ?

Last updated on