Migration Files
File Structure
Migrations are stored in the output directory (default:drizzle/) with the following structure:
SQL Migration File
Generated SQL files contain DDL statements with optional breakpoints:--> statement-breakpoint):
- Enable sequential execution of statements
- Required for MySQL, SQLite, and SingleStore (no multi-statement transactions)
- Optional for PostgreSQL (supports multi-statement transactions)
Migration Metadata
Journal File (meta/_journal.json)
Tracks all migrations in order:
string
Drizzle Kit metadata format version
string
Database dialect:
postgresql, mysql, sqlite, etc.array
List of migration entries in chronological order
number
Sequential migration index
string
Migration filename (without
.sql)number
Unix timestamp (milliseconds) when migration was created
boolean
Whether migration uses statement breakpoints
Snapshot File (meta/XXXX_snapshot.json)
Complete schema snapshot at migration time:
- Detecting schema changes between migrations
- Generating accurate diff SQL
- Validating migration consistency
Migration Workflow
1. Generate Migration
Detect schema changes and create migration:- Reads current Drizzle schema from
schemafiles - Compares with latest snapshot in
meta/ - Generates SQL diff statements
- Creates new migration file with timestamp/index
- Updates
_journal.jsonand creates new snapshot
2. Review Migration
Inspect generated SQL before applying:- Correct DDL statements
- Data migration needs
- Potential data loss (DROP statements)
- Index creation order
3. Apply Migration
Execute migrations against database:- Connects to database using
dbCredentials - Creates migrations table if not exists
- Queries applied migrations from database
- Applies pending migrations in order
- Records each migration in migrations table
4. Verify Changes
Open Drizzle Studio to verify:Programmatic API
Use migration functions directly in your application.PostgreSQL
MySQL
SQLite
Turso (LibSQL)
Migration Options
string
required
Path to migrations directory
string
default:"__drizzle_migrations"
Custom migrations tracking table name
string
default:"drizzle"
PostgreSQL: Custom schema for migrations table
Custom Migrations
Generate Empty Migration
Create migration file for manual SQL:Data Migrations
Combine DDL and data changes:Migration Prefixes
Control migration file naming:Index (default)
Timestamp
Unix Timestamp
Supabase Format
No Prefix
Migration Management
Drop Migration
Remove the last migration:- Removes migration SQL file
- Removes snapshot file
- Updates
_journal.json - Does NOT revert applied migrations in database
Check Migrations
Validate migration consistency:- Migration files exist for all journal entries
- Snapshot files are valid JSON
- No duplicate migration indices
- SQL syntax is parseable
Upgrade Migrations
Update migration format after Drizzle Kit upgrades:- Metadata version in snapshots
- Journal format
- Snapshot schema structure
- SQL migration files (unchanged)
- Migration order
- Applied migrations in database
Best Practices
1. Version Control
Commit all migration files:2. Review Before Apply
Always review generated SQL:3. One-Way Migrations
Migrations are forward-only. To undo:4. Test Migrations
Test on development database first:5. Backup Before Migration
Always backup production before migrating:6. Handle Data Migration
For complex data transformations:7. Use Named Migrations
Descriptive names for clarity:Troubleshooting
Migration Already Applied
Error: Migration has already been applied Solution: Check migrations table:Conflicting Migrations
Error: Migration conflict detected Solution:- Pull latest migrations from version control
- Regenerate migration with latest schema
- Resolve conflicts manually
Breakpoint Errors (MySQL/SQLite)
Error: Multi-statement execution not supported Solution: Enable breakpoints:Snapshot Mismatch
Error: Schema snapshot doesn’t match database Solution:- Ensure all migrations are applied:
drizzle-kit migrate - Regenerate snapshot:
drizzle-kit generate - Or pull fresh schema:
drizzle-kit pull