Migration System Overview
The migration system uses:- Drizzle Kit: CLI tool for generating migrations from schema files
- PostgreSQL: Target database (local or production)
- TypeScript: Migration scripts with type safety
- Version Control: Migration history tracked in
__drizzle_migrationstable
Configuration Files
The project has separate configuration files for development and production:Development Config
Production Config
The production config uses a glob pattern to load all schema files, while development uses a single export file.
Environment Variables
Set these in your.env file:
Generating Migrations
Basic Generation
After modifying schema files, generate a migration:- Scans schema files in
src/database/schemas/ - Compares with the current database state
- Generates SQL migration files
- Updates the migration journal
Generation Process
The generation script (scripts/db/db.generate.ts) automatically:
- Validates schema files - Ensures all schema files exist and are not empty
- Checks for missing imports - Prompts to add unamported schema files to
schemas.ts - Creates migration directory - Auto-creates
src/database/migrations/meta/if needed - Initializes journal - Creates
_journal.jsonif it doesn’t exist - Detects new migrations - Shows which migration files were created
Interactive Import Management
If you add new schema files, the generator will prompt you:Production Generation
To generate migrations for production:Migration Files
Migrations are stored insrc/database/migrations/:
Migration File Format
Each migration is a.sql file with SQL statements:
Migration Journal
The_journal.json file tracks migration history:
Running Migrations
Apply Pending Migrations
Run all pending migrations:scripts/db/db.migrate.ts):
- Connects to the database
- Creates
__drizzle_migrationstable if needed - Checks which migrations have been applied
- Runs pending migrations in order
- Records each migration in the tracking table
Migration Output
Production Migrations
To run migrations on production:CONFIRM_PROD=oui to skip the prompt:
No Pending Migrations
If all migrations are already applied:Migration Tracking
Migrations are tracked in the__drizzle_migrations table:
Database Reset (Development)
To completely reset the database and rerun all migrations:- Prompts for confirmation
- Drops all tables in the public schema
- Clears the migration history
- Reruns all migrations from scratch
Migration Best Practices
1. Always Generate Before Migrating
2. Version Control
Commit migration files to git:3. Never Modify Applied Migrations
Once a migration has been applied (especially in production), never modify it. Instead:- Create a new migration to fix the issue
- Generate it with
npm run db:generate - Apply it with
npm run db:migrate
4. Test Migrations Locally First
Always test migrations in development before production:5. Handle Schema Changes Carefully
- Adding Columns
- Removing Columns
- Renaming Columns
- Adding Constraints
Safe - Add new nullable columns or columns with defaults:
6. Use Transactions
The migration runner automatically wraps each statement in a transaction:- If a migration fails, it rolls back
- The migration is marked as failed
- Fix the issue and rerun
7. Handle Duplicate Objects Gracefully
The migration system ignores duplicate object errors:Common Migration Scenarios
Adding a New Table
1
Create schema file
Create
src/database/schemas/my_table.schema.ts:2
Export from schemas.ts
Add to
src/database/schemas.ts:3
Generate migration
4
Apply migration
Modifying Existing Table
1
Edit schema file
Modify the schema definition in
src/database/schemas/:2
Generate migration
3
Apply migration
Adding Foreign Key
1
Update schema
2
Generate and apply
Data Migration
For complex migrations involving data transformation:1
Generate schema migration
2
Edit SQL file
Open the generated migration file and add data transformation:
3
Apply migration
Troubleshooting
Migration Fails
If a migration fails:- Check the error message - It will show which statement failed
- Fix the schema - Correct the schema definition
- Delete the failed migration - Remove the generated
.sqlfile - Regenerate - Run
npm run db:generateagain - Reapply - Run
npm run db:migrate
Schema Out of Sync
If your database schema doesn’t match the code:Missing Migration Journal
If_journal.json is missing:
Schema File Not Found
If generation fails with “schema file not found”:- Check
src/database/schemas.tsexists - Verify it exports all schema files
- Ensure schema files use correct imports
Production Migration Failed
If a production migration fails:- Don’t panic - Migrations use transactions and roll back
- Check logs - Identify the failing statement
- Create fix migration - Generate a corrective migration
- Test locally - Apply fix to local database first
- Apply to production - Run fix migration on production
Next Steps
Schema Reference
View complete database schema documentation
Relationships
Learn about table relationships and foreign keys