Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Fireinthebellyy/ftb-web/llms.txt
Use this file to discover all available pages before exploring further.
Migration Management
FTB Hustle uses Drizzle Kit for database schema migrations. Drizzle Kit provides a type-safe way to manage database schema changes and keeps your database in sync with your TypeScript schema definitions.Overview
The migration workflow involves:- Modifying the schema in
lib/schema.ts - Generating migration files from schema changes
- Applying migrations to the database
- Version controlling migration files
Configuration
The Drizzle configuration is defined indrizzle.config.ts:
Configuration Options
schema: Path to your schema file(s)out: Directory where migration files are generateddialect: Database type (PostgreSQL)dbCredentials.url: Database connection string from environment variables
Available Commands
The following npm scripts are available for migration management:Generate Migrations
- Compares your current schema (
lib/schema.ts) with the existing migrations - Creates SQL migration files in the
migrations/directory - Names migrations automatically (e.g.,
0001_blue_shooting_star.sql)
- After adding new tables or columns
- After modifying existing table structures
- After adding/removing indexes or constraints
Always review generated migration files before applying them to ensure they match your intended changes.
Push Schema Changes
- Useful for rapid development and prototyping
- Applies changes directly from
lib/schema.ts - Does not create migration files
- Local development and experimentation
- Quick schema iterations
- Non-production environments
Pull Schema from Database
- Connects to your database
- Reads the existing schema structure
- Generates Drizzle schema definitions
- When working with an existing database
- To sync schema definitions with database state
- After manual database changes (not recommended)
Migration Workflow
Development Workflow
-
Modify Schema
Edit
lib/schema.tsto add/modify tables: -
Generate Migration
This creates a new migration file like
migrations/0035_new_table.sql: - Review Migration Check the generated SQL to ensure it matches your intentions.
-
Apply Migration
For development, you can use:
Or apply the migration through your deployment pipeline.
-
Commit Changes
Production Workflow
- Test Locally Always test migrations on a local or staging database first.
-
Review Migration Files
Ensure migrations are safe and won’t cause data loss:
- Check for potentially destructive operations
- Verify foreign key constraints
- Test rollback procedures if needed
- Backup Database Before applying migrations to production, create a database backup.
- Apply Migrations Run migrations through your deployment pipeline or CI/CD process.
- Verify Check that the database structure matches expectations and the application works correctly.
Migration Files
Migration files are stored in themigrations/ directory:
File Structure
- SQL files: Contain the actual migration SQL statements
- meta/ directory: Contains metadata about migrations
- _journal.json: Tracks migration history
- Snapshot files: Store schema snapshots for comparison
Migration Naming
Drizzle Kit automatically names migrations using a pattern:0034_brave_squadron_sinister.sql
- Sequence number ensures ordered execution
- Random name provides uniqueness and readability
Common Operations
Adding a New Table
-
Define the table in
lib/schema.ts: -
Generate migration:
Adding a Column
-
Add the column to the table definition:
-
Generate migration:
Renaming a Column
Drizzle may interpret column renames as drop + add operations. For safe renames:-
Generate the migration:
-
Manually edit the migration file to use
ALTER TABLE ... RENAME COLUMN:
Adding an Enum
-
Define the enum in
lib/schema.ts: -
Use it in a table:
-
Generate migration:
Adding a Foreign Key
-
Add the reference in the schema:
-
Generate migration:
Adding an Index
-
Add the index in the table definition:
-
Generate migration:
Best Practices
Do’s
- Version control migrations: Always commit migration files to Git
- Review generated SQL: Check migrations before applying them
- Test locally first: Test migrations on development databases
- Use meaningful schema changes: Make atomic, logical changes
- Backup production data: Always backup before production migrations
- Use transactions: Most Drizzle migrations run in transactions automatically
Don’ts
- Don’t modify existing migrations: Once applied, don’t change migration files
- Don’t delete migrations: Keep migration history intact
- Don’t skip migrations: Apply migrations in sequence
- Don’t use
dz:pushin production: Always use generated migrations - Don’t make breaking changes without planning: Plan data migrations for breaking schema changes
Troubleshooting
Migration Generation Fails
Problem:npm run dz:generate doesn’t detect changes
Solution:
- Ensure schema changes are saved in
lib/schema.ts - Check for TypeScript errors in the schema file
- Verify Drizzle Kit is properly installed
Connection Errors
Problem: “Cannot connect to database” Solution:- Check
DATABASE_URLin.env.local - Verify database is running
- Ensure network connectivity to database
Type Errors
Problem: TypeScript errors after schema changes Solution:- Ensure schema types are properly exported
- Restart TypeScript server
- Check for circular dependencies
Migration Conflicts
Problem: Multiple developers create migrations simultaneously Solution:- Communicate schema changes with team
- Merge migration files carefully
- Regenerate migrations if needed after merge
Environment Variables
Required environment variables for migrations:Related Resources
- Database Schema - Complete schema reference
- Drizzle ORM Documentation - Official Drizzle docs
- Drizzle Kit Documentation - Drizzle Kit guide
- PostgreSQL Documentation - PostgreSQL reference