Create Database Migration
Generate new migration files for database schema changes.
Usage
make migrate-create NAME=add_user_preferences
Replace add_user_preferences with a descriptive name for your migration.
What Gets Created
Two files in migrations/ directory:
- •
Up migration:
YYYYMMDDHHMMSS_add_user_preferences.up.sql- •Contains SQL to apply the change
- •Executed when migrating forward
- •
Down migration:
YYYYMMDDHHMMSS_add_user_preferences.down.sql- •Contains SQL to revert the change
- •Executed when rolling back
Migration Naming Conventions
Use descriptive names that indicate the change:
✅ Good names:
- •
add_email_to_users - •
create_categories_table - •
add_index_on_transactions_date - •
remove_deprecated_columns
❌ Bad names:
- •
migration1 - •
update - •
fix - •
changes
Example Migration Content
Up Migration (*_add_user_preferences.up.sql)
-- Add user preferences column
ALTER TABLE users ADD COLUMN preferences TEXT DEFAULT '{}';
-- Create index for faster lookups
CREATE INDEX idx_users_preferences ON users(preferences);
Down Migration (*_add_user_preferences.down.sql)
-- Remove index DROP INDEX IF EXISTS idx_users_preferences; -- Remove column ALTER TABLE users DROP COLUMN preferences;
Migration Best Practices
- •Atomic changes: One logical change per migration
- •Always reversible: Write down migration that undoes up migration
- •Test both directions: Verify up and down migrations work
- •Safe operations: Consider impact on existing data
- •Backup first: Use
/db-backupbefore applying migrations
Automatic Execution
Migrations run automatically:
- •On app startup: Both
make run-localandmake docker-up - •In order: Sorted by timestamp prefix
- •Once only: Tracks applied migrations to avoid re-running
Migration Workflow
- •
Create migration files:
bashmake migrate-create NAME=add_budget_categories
- •
Edit up migration: Add SQL to apply changes in
migrations/*_add_budget_categories.up.sql - •
Edit down migration: Add SQL to revert changes in
migrations/*_add_budget_categories.down.sql - •
Test locally:
bashmake run-local # Applies migrations automatically
- •
Verify in database:
bashmake sqlite-shell sqlite> .schema # Check schema changes
- •
Commit to version control:
bashgit add migrations/ git commit -m "Add budget categories migration"
SQLite-Specific Considerations
SQLite has some limitations compared to PostgreSQL:
- •No DROP COLUMN: Use table recreation pattern
- •Limited ALTER TABLE: Some operations require table rebuild
- •No concurrent DDL: Migrations lock the database
Example table recreation pattern:
-- Create new table with desired schema
CREATE TABLE users_new (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL,
-- new column
preferences TEXT DEFAULT '{}'
);
-- Copy data
INSERT INTO users_new (id, email)
SELECT id, email FROM users;
-- Swap tables
DROP TABLE users;
ALTER TABLE users_new RENAME TO users;
Troubleshooting
Migration fails
- •Check SQL syntax in migration files
- •Verify SQLite compatibility
- •Check logs:
make docker-logsor console output - •Restore from backup:
/db-backup
Migration already applied
Migrations are tracked in schema_migrations table. Don't modify applied migrations.
Need to rollback
Currently, down migrations are not automatically executed. To rollback:
- •Backup:
/db-backup - •Manually run down SQL in
/db-shell - •Remove migration record from
schema_migrations
See Also
- •
/db-shell- Execute SQL directly - •
/db-backup- Backup before migrations - •
make sqlite-stats- View database statistics