Migration Files
Migrations are stored inapps/api/prisma/migrations/ with the following structure:
- Timestamp prefix (YYYYMMDDHHMMSS)
- Descriptive name
migration.sqlfile with SQL statements
Development Workflow
Creating a New Migration
When you modify the Prisma schema (apps/api/prisma/schema.prisma), create a migration:
Create the migration
Run the migration command:This will:
- Generate SQL for the schema changes
- Create a new migration file
- Apply the migration to your local database
- Regenerate the Prisma client
Review the migration
Check the generated SQL in
apps/api/prisma/migrations/<timestamp>_add_middle_name/migration.sql:Applying Migrations
In development, migrations are applied automatically when you run:The
prisma migrate deploy command is automatically run in the Docker container startup (see apps/api/package.json:7).Production Migrations
Docker Deployment
Migrations are applied automatically when the API container starts:- Apply pending migrations (
prisma migrate deploy) - Generate Prisma client (
prisma generate) - Start the API server
Standalone Deployment
For standalone installations, migrations are applied during the update process:start script includes prisma migrate deploy.
Common Migration Tasks
Adding a Field
Making a Field Required
Adding a Relation
Creating an Enum
Migration Best Practices
Use descriptive migration names
Use descriptive migration names
Good migration names:
add_citizen_middle_namecreate_custom_fields_tableupdate_officer_callsign_template
updatefixchanges
Test migrations locally first
Test migrations locally first
Always test migrations in your local development environment before deploying:
Backup before production migrations
Backup before production migrations
Always backup your database before applying migrations in production:See Backup & Restore for more details.
Don't modify existing migrations
Don't modify existing migrations
Never edit migration files that have been applied to production. Create a new migration instead:
Handle data migrations carefully
Handle data migrations carefully
When migrations require data transformation, consider:
- Add new field as optional
- Migrate data in application code
- Make field required in a later migration
$executeRaw for complex data migrations.Troubleshooting
Migration Failed
If a migration fails duringmigrate deploy:
Schema Out of Sync
If the database schema doesn’t match the Prisma schema:Migration Lock
If you see “Another migration is already running”:- Check if another process is running migrations
- If stuck, remove the lock manually:
Rolling Back Migrations
Prisma Migrate doesn’t support automatic rollbacks. To rollback:- Restore database from backup
- Or manually revert changes:
Migration Commands Reference
| Command | Description |
|---|---|
prisma migrate dev | Create and apply migration in development |
prisma migrate deploy | Apply pending migrations in production |
prisma migrate status | Check migration status |
prisma migrate reset | Reset database and reapply all migrations |
prisma migrate resolve --applied | Mark migration as applied |
prisma migrate resolve --rolled-back | Mark migration as rolled back |
prisma generate | Regenerate Prisma Client |
prisma validate | Validate schema syntax |
Advanced Topics
Custom Migration SQL
You can add custom SQL to generated migrations:Baseline Existing Database
If you’re adopting Prisma Migrate on an existing database:Related Documentation
Database Schema
Explore the database structure
Backup & Restore
Database backup procedures
Troubleshooting
Common deployment issues
Prisma Docs
Official Prisma Migrate docs