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 prismaor 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
Modify your
schema.prismafile: Define your data model changes. For example:model User { id Int @id @default(autoincrement()) name String email String @unique age Int? @default(30) }Run the Prisma migration tool:
bashnpx prisma migrate dev --name "add_age_column"This command will create and apply migrations to the local development database.
Verify SQL migration files:
Test your changes locally: Use a local PostgreSQL instance or containerized setup via Docker.
bashdocker 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
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.
Use Neon branch for schema testing: Point Prisma to the Neon branch by updating the
DATABASE_URLin.env.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
Include migration files in your PR: Ensure the
migrationsfolder is pushed to version control.Setup Neon in CI/CD pipelines: Create preview branches to test migrations automatically. Example GitHub Actions configuration:
yamljobs: 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 deployAutomate 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
Merge pull requests: This action triggers production pipelines.
Run migrations in production: Use
prisma migrate deployin the CI pipeline:bashnpx prisma migrate deployVerify 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.