Migrations

This document describes the database migration strategy for the Poker Engine project using Prisma.

Overview

The project uses Prisma Migrate for database schema management. Migrations are stored in prisma/migrations/ and tracked in version control.

Local Development

Running Migrations

# Apply pending migrations and create a new one if the schema changed
npm run db:migrate

# In production environments
npm run db:migrate:prod

Creating New Migrations

  1. Modify the schema in prisma/schema.prisma
  2. Run migration to generate SQL and apply:
    npm run db:migrate
  3. Prisma will prompt for a migration name
  4. Review the generated SQL in prisma/migrations/<timestamp>_<name>/migration.sql
  5. Commit both the schema changes and migration file

Testing Migrations Locally

Before committing migrations, test that they can be applied and reset:

npm run test:migrations

This script:

  1. Applies all migrations (prisma migrate deploy)
  2. Resets the database (prisma migrate reset --force)
  3. Re-applies all migrations

Requires: DATABASE_URL pointing to a disposable PostgreSQL database. The reset step wipes all data, so never point it at a database you want to keep.

npm run ci:migrations runs the same apply/validate/reset/re-apply cycle against a disposable Docker Postgres container and is the safer local option.

CI/CD Pipeline

Automated Testing

The .github/workflows/migration-test.yml workflow automatically tests migrations on:

  • Pull requests targeting main or master that modify prisma/** or the workflow file
  • Pushes to main or master that modify prisma/** or the workflow file

The CI job:

  1. Spins up a PostgreSQL 15 service container
  2. Applies all migrations
  3. Validates the schema
  4. Resets and re-applies migrations
  5. Verifies the final database state

.github/workflows/test.yml and the release workflow also apply migrations and fail if the migrations and prisma/schema.prisma have drifted apart (see scripts/ci/schema-drift.sh for the local equivalent).

Deployment

In production, migrations are applied using:

npx prisma migrate deploy

This command:

  • Only applies pending migrations
  • Does not generate new migrations
  • Does not prompt for user input
  • Exits with error if migration fails

Migration Best Practices

Writing Safe Migrations

  1. Make migrations reversible: Design changes that can be undone
  2. Avoid data loss: Use ALTER instead of DROP when possible
  3. Split risky migrations: Break large changes into smaller, safer steps
  4. Test with production data: Use a copy of production data for testing

Handling Breaking Changes

For changes that might affect running applications:

  1. Add before remove: First add new column/table, then remove old
  2. Use feature flags: Deploy code that handles both states
  3. Coordinate with deployments: Apply migration during maintenance window

Migration Naming

Use descriptive names that indicate the change:

  • add_user_email_verification
  • create_tournament_tables
  • remove_deprecated_status_field

Troubleshooting

Migration Failed

If a migration fails:

  1. Check the error message in the output
  2. Review the SQL in the migration file
  3. Fix the issue in the schema or SQL
  4. If the migration was partially applied:
    npx prisma migrate resolve --rolled-back <migration-name>

Schema Drift

If the database schema doesn’t match expected state:

# Show what the database is missing compared with the schema
npx prisma migrate diff --from-schema-datasource prisma/schema.prisma --to-schema-datamodel prisma/schema.prisma

Fix drift with a new migration. prisma migrate reset drops all data; only use it on a disposable database.

Baseline an Existing Database

For databases with existing schema not managed by Prisma:

npx prisma migrate diff --from-empty --to-schema-datasource prisma/schema.prisma --script > prisma/migrations/0000_baseline/migration.sql
npx prisma migrate resolve --applied 0000_baseline
CommandDescription
npm run db:migrateCreate and apply migrations in development (prisma migrate dev)
npm run db:migrate:prodApply migrations in production (prisma migrate deploy)
npm run ci:migrationsTest the migration cycle against a disposable Docker database
npm run db:studioOpen Prisma Studio GUI
npm run test:migrationsTest the apply/reset cycle (wipes the DATABASE_URL database)

npm run db:push is intentionally disabled and exits with an error.

Resources

Try the live demo Pricing

Have questions about Poker Engine? We reply fast.