Overview
Prisma Migrate enables version-controlled, type-safe database schema management. All schema changes are defined inpackages/api/prisma/schema.prisma and tracked as migration files.
Migrations are stored in
packages/api/prisma/migrations/ and should be committed to version control.Prisma Schema
The schema defines your database structure using Prisma’s DSL:schema.prisma
Common Migration Workflows
Creating a New Migration
When you modifyschema.prisma, create a migration:
1
Edit Schema
Modify
packages/api/prisma/schema.prisma:2
Create Migration
Generate migration files:This will:
- Generate SQL migration file
- Apply migration to database
- Regenerate Prisma Client with new types
3
Review Migration
Check the generated SQL in
prisma/migrations/TIMESTAMP_add-product-discount/migration.sql:4
Commit Changes
Applying Existing Migrations
When pulling changes from git or deploying:- Development
- Production
- Docker
Resetting the Database
- Drop the database
- Create a new database
- Apply all migrations
- Run seed script (if configured)
Prisma Client Generation
After schema changes, regenerate the Prisma Client:This updates TypeScript types to match your schema. Run after pulling schema changes from git.
Database Seeding
PC Fix includes a seed script for populating test data:prisma/seed.ts
Prisma Studio
Explore and edit database records with Prisma Studio:For Docker:
docker exec -it pcfix-api npx prisma studioCommon Prisma Commands
Migration Best Practices
1
Test Locally First
Always test migrations on local database before deploying:
2
Use Descriptive Names
Name migrations clearly:
3
Review Generated SQL
Check migration files before committing:
4
Backup Production Data
Before running migrations in production:
Handling Complex Migrations
Data Migrations
For migrations that require data transformation, edit the generated SQL:migration.sql
Breaking Changes
For breaking schema changes:-
Deploy in stages:
- Add new fields (optional)
- Update application code to use new fields
- Make fields required
- Remove old fields
-
Use database transactions:
-
Plan rollback strategy:
Troubleshooting
Migration failed to apply
Migration failed to apply
If a migration fails:
Schema and database out of sync
Schema and database out of sync
Prisma Client outdated
Prisma Client outdated
Migration conflicts after git merge
Migration conflicts after git merge
CI/CD Integration
Add migration checks to your CI pipeline:.github/workflows/ci.yml
Next Steps
Database Schema
Explore the complete database schema
Backend Architecture
Learn about the API structure
Environment Setup
Configure database connection
Testing
Test database operations