What is Baselining?
Baselining is the process of initializing a migration history for a database that:- Already exists in production
- Has existing tables and data
- Was not previously managed by Prisma Migrate
When to Baseline
You need to baseline when:- Adding Prisma Migrate to an existing project
- Migrating from another migration tool (Flyway, Liquibase, etc.)
- Moving from
prisma db pushtoprisma migrate - Your database schema was created manually
Baselining Workflow
This is the complete workflow for adding Prisma Migrate to an existing production database.Introspect Your Database
Start by pulling the current database schema:This creates a Prisma schema that matches your existing database:
Create Initial Migration
Generate a migration that represents your current schema:Output:This creates:
Review the Migration
The generated migration contains SQL to create all tables:
prisma/migrations/20201231000000_init/migration.sql
Baseline Production Database
Now baseline your production database. Switch to the production database URL:Mark the migration as already applied without executing it:Output:This creates the
_prisma_migrations table in production and records the baseline migration as applied.The migrate resolve Command
prisma migrate resolve is used to mark migrations as applied or rolled back without executing them.
Mark as Applied
Record a migration as successfully applied:- Baselining an existing database
- A migration was applied manually (hotfix)
- Recovering from migration table corruption
Mark as Rolled Back
Record a migration as rolled back:- A migration failed and was manually rolled back
- Recovering from a failed migration
Checking Migration Status
Useprisma migrate status to identify if baselining is needed:
When Baselining is Needed
You’ll see this output:Multiple Environments
When you have multiple environments (dev, staging, production), baseline each separately:Development (Already Done)
Staging
Production
Complete Example: SQLite
Here’s a complete baselining example with SQLite:Baselining After Failed Migrations
If a migration fails in production:Diverged Migration History
If local and database migration histories diverge:- Pull the missing migrations from version control
- Delete the diverged migration from the database
- Re-baseline if the divergence is too complex
Best Practices
Test First
Always test the baselining process in a staging environment first.
Backup Production
Create a full database backup before baselining production.
Document Process
Document your baselining steps for team reference.
Verify Status
Always run
migrate status after baselining to confirm success.Common Issues
Issue: Tables Already Exist
migrate resolve --applied to baseline instead of migrate dev.
Issue: Migration Not Found
prisma/migrations/.
Issue: Multiple Developers
If multiple developers created migrations before baselining:- Agree on a single baseline migration
- Delete local migrations
- Pull the agreed baseline from git
- Baseline all environments
Next Steps
Migration Workflows
Learn production migration workflows
Resolve Failed Migrations
Handle failed migrations in production