Developer Guides

Prisma ORM: PostgreSQL Migrations from Dev to Production

Learn how to efficiently manage database migrations with Prisma ORM, enabling smooth transitions from development to production in PostgreSQL environments.

4 min read

Learn how to seamlessly manage database migrations with Prisma ORM in PostgreSQL, transitioning from development to production while maintaining schema consistency.

Prerequisites

To follow this Prisma ORM tutorial and successfully migrate PostgreSQL databases, ensure the following are in place:

prerequisites

  • PostgreSQL installed and running locally or remotely.
  • Prisma installed via npm install prisma or globally (npm install -g prisma).
  • Connection to a PostgreSQL database configured in your Prisma project.
  • Familiarity with version control systems like Git.

Development Workflow

In development, you will modify your Prisma schema, generate migrations, and test changes locally.

steps

  1. Modify your schema.prisma file: Define your data model changes. For example:

    model User {
      id         Int     @id @default(autoincrement())
      name       String
      email      String  @unique
      age        Int?    @default(30)
    }
  2. Run the Prisma migration tool:

    bash
    npx prisma migrate dev --name "add_age_column"

    This command will create and apply migrations to the local development database.

  3. Verify SQL migration files:

  4. Test your changes locally: Use a local PostgreSQL instance or containerized setup via Docker.

    bash
    docker run --name postgres -e POSTGRES_PASSWORD=password -d -p 5432:5432 postgres

Using Neon Branches During Development

Neon provides branching capabilities for database development, offering isolated environments for schema changes.

steps

  1. Create a database branch in Neon: Neon lets you create branches for development or testing purposes. Branch snapshots can be populated from production data for realistic testing.

  2. Use Neon branch for schema testing: Point Prisma to the Neon branch by updating the DATABASE_URL in .env.

  3. Reset branches as needed: In the Neon console, reset development branches to restore their original state.

Creating Pull Requests and CI/CD Setup

Integrating Prisma migrations with CI/CD ensures your database schema evolves predictably and consistently.

steps

  1. Include migration files in your PR: Ensure the migrations folder is pushed to version control.

  2. Setup Neon in CI/CD pipelines: Create preview branches to test migrations automatically. Example GitHub Actions configuration:

    yaml
    jobs:
      deploy-preview:
        runs-on: ubuntu-latest
        steps:
          - name: Check out the code
            uses: actions/checkout@v2
          - name: Set up Node.js
            uses: actions/setup-node@v2
          - name: Create Neon branch
            uses: neon/action-create-branch@v1
            with:
              branch-name: preview-${{ github.head_ref }}
              project-id: ${{ secrets.NEON_PROJECT_ID }}
          - name: Run migrations
            run: npx prisma migrate deploy
  3. Automate cleanup workflows: Clean up preview branches after PRs are merged to avoid unnecessary database clutter.

Production Deployment

Deploying migrations to production requires careful handling to avoid data loss or downtime.

steps

  1. Merge pull requests: This action triggers production pipelines.

  2. Run migrations in production: Use prisma migrate deploy in the CI pipeline:

    bash
    npx prisma migrate deploy
  3. Verify deployments: Manually test key functionality after the migrations are applied.

Key Workflow Differences Between Development and Production

Understanding the differences between Prisma migrate dev and deploy can prevent costly mistakes.

comparison

Development Workflow

  • Command: prisma migrate dev
  • Creates migrations and applies them interactively.
  • Destructive changes are acceptable.

Production Workflow

  • Command: prisma migrate deploy
  • Applies pre-generated migrations as defined in version control.
  • Designed for CI/CD environments and guarantees non-interactive execution.

FAQ

What does prisma migrate dev do?

prisma migrate dev is used in development environments. It creates and applies new migration files to a local database interactively.

Can I use prisma migrate dev for production?

No, prisma migrate dev is destructive and should never be used in production. Instead, use prisma migrate deploy for applying migrations in production.

How do Neon branches improve development workflow?

Neon branches let you create isolated database environments for testing changes, enabling safe development without affecting production data.

Should I include Prisma migrations in version control?

Yes, tracking migrations in version control is essential. It ensures all team members and CI/CD pipelines operate with consistent schema changes.


Official reference: Prisma documentation.