Budgetron uses Drizzle ORM for database management and migrations. This guide covers the migration workflow for local development and production.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/budgetron-org/budgetron/llms.txt
Use this file to discover all available pages before exploring further.
Migration Files Location
All migrations are stored in thedrizzle/migrations/ directory:
Configuration
Migration settings are defined indrizzle.config.ts:
Database Schema Organization
The database schema is defined in TypeScript files:- src/server/db/schema.ts - Central export that combines all schemas
- src/server/db/schemas/ - Individual table definitions:
user.ts,session.ts,account.ts- Authenticationbank-account.ts- Bank accountstransaction.ts- Financial transactionscategory.ts- Categoriesbudget.ts- Budgetscurrency-rate.ts- Exchange ratesgroup.ts,group-user.ts- Groupsuser-settings.ts- User settingsfeature-flags.ts- Feature flagsenums.ts- Shared enums
Common Migration Commands
Apply Migrations
Run pending migrations against your database:- Connects to the database specified in
DB_URL - Checks the
__drizzle_migrationstable - Applies any pending migrations in order
Generate New Migration
After modifying schema files insrc/server/db/schemas/, generate a migration:
- Compare your schema to the current database state
- Generate SQL migration files
- Update the metadata in
drizzle/migrations/meta/
Push Schema (Development Only)
For rapid prototyping, push schema changes directly without migrations:Open Drizzle Studio
Explore your database with Drizzle Studio (a visual database browser):Development Environment Workflow
If you maintain separate.env and .env.development files:
Using Development Environment
dotenv-cli to load variables from .env.development instead of .env.
Creating Migrations
Step-by-Step Workflow
-
Modify the schema
Edit files in
src/server/db/schemas/, for exampletransaction.ts: -
Generate the migration
Drizzle Kit will prompt you for a migration name or auto-generate one.
-
Review the migration
Check the generated SQL in
drizzle/migrations/[number]_[name].sql: -
Apply the migration
-
Commit both the schema and migration files
Patching Migrations
For advanced scenarios, you can patch migrations programmatically:scripts/db-migrations-patch.ts, which can modify migration files before they’re applied.
Production Migrations
Docker Entrypoint
The Dockerfile includes an entrypoint script that automatically runs migrations on container start:Manual Production Migrations
If you need to run migrations manually in production:Migration Tracking
Drizzle tracks applied migrations in the__drizzle_migrations table:
| Column | Type | Description |
|---|---|---|
| id | serial | Auto-incrementing ID |
| hash | text | Migration file hash |
| created_at | timestamp | When migration was applied |
Best Practices
Do:
- Always generate migrations for schema changes in production
- Review generated SQL before applying migrations
- Test migrations on a copy of production data
- Commit migrations with schema changes in the same PR
- Use descriptive migration names when prompted
- Back up production database before major migrations
Don’t:
- Don’t use
db:pushin production - it bypasses migration history - Don’t edit applied migrations - create a new migration instead
- Don’t skip migrations - apply them in order
- Don’t commit schema changes without migrations
Troubleshooting
Migration Already Applied
If you see “Migration already applied”:- Check
__drizzle_migrationstable - Verify the migration hash matches
- If developing locally, consider resetting your database
Migration Failed
If a migration fails mid-execution:- Check PostgreSQL logs for errors
- Verify your database user has sufficient permissions
- Manually fix the database state if needed
- Remove the failed entry from
__drizzle_migrations - Re-run the migration
Schema Out of Sync
If your schema doesn’t match the database:- Run
pnpm run db:generateto see the diff - Apply with
pnpm run db:migrate - Or use
pnpm run db:pushif in development
Related Resources
- Drizzle ORM Documentation
- Drizzle Kit Commands
- Project Structure - Understanding the database layer