Migration Configuration
Migrations are configured indrizzle.config.ts:
out: Where migration files are generatedschema: Source schema file locationdialect: PostgreSQL databasecasing: Automatic snake_case conversion for table/column namesdbCredentials: Connection string from environment
Migration Commands
All migration commands are available as npm scripts inpackage.json:
Generate Migrations
- Compares current schema with database state
- Creates SQL migration file in
src/db/migrations/ - Generates snapshot for tracking
Push Changes (Development)
--verbose: Detailed output--strict: Strict mode prevents destructive changes- Ideal for rapid development
- Warning: Not recommended for production
Apply Migrations
- Executes SQL migrations in order
- Updates migration journal
- Safe for production use
Pull Schema
- Syncing with existing databases
- Recovering schema from production
- Reverse engineering schemas
Drizzle Studio
- Visual database browser
- Runs on port 3003
- Query and explore data
- Schema visualization
Migration Workflow
Standard Development Flow
-
Modify Schema - Edit
src/db/schema.ts -
Generate Migration - Run
bun run db:gen -
Review Migration - Inspect generated SQL
-
Apply Migration - Run
bun run db:migrate
Rapid Development Flow
For quick iterations during development:- Modify Schema - Edit
src/db/schema.ts - Push Directly - Run
bun run db:push- Skips migration file generation
- Applies changes immediately
- Faster iteration
Migration File Structure
Migrations are stored insrc/db/migrations/:
Migration File Format
Each migration is a SQL file with a numbered prefix and random name:Statement Breakpoints
Migrations use--> statement-breakpoint to separate SQL statements:
Migration Journal
Themeta/_journal.json file tracks all migrations:
Example Migrations
Initial Auth Schema (0000)
The first migration creates the authentication tables:Rate Limiting Addition (0001)
The second migration adds rate limiting:Migration Best Practices
1. Always Review Generated SQL
Before applying migrations, review the generated SQL:2. Test Migrations Locally
Always test migrations against a local database first:3. Version Control
Commit migration files to version control:4. One-Way Migrations
Drizzle migrations are forward-only. To rollback:- Create a new migration that reverses changes
- Or restore from database backup
5. Production Deployment
Never usedb:push in production. Always use proper migrations:
6. Handle Data Migrations
For data transformations, manually edit generated SQL:7. Strict Mode
Use--strict flag to prevent destructive changes:
- Column drops
- Type changes that lose data
- Constraint removals
Environment Configuration
All commands usedotenvx for environment management:
.env.dev- Local development.env.prod- Production
DATABASE_URL is set in your environment file:
Troubleshooting
Migration Out of Sync
If migrations are out of sync:Failed Migration
If a migration fails:- Check database logs
- Fix the issue
- Create a new migration with the fix
- Apply the corrective migration
