Database Migrations Guide
Database Migrations Guide
This project uses Alembic for database schema migrations.
Overview
Alembic provides a way to version control database schema changes, making it easier to:
- Track schema evolution over time
- Apply migrations consistently across environments
- Rollback changes if needed
- Collaborate on schema changes
Setup
- Install dependencies:
pip install -r requirements.txtThis will install Alembic and SQLAlchemy (required for Alembic).
-
Configure database URL: The migration system uses
DATABASE_URLfromconfig.py(loaded from environment variables).Ensure your
.envfile contains:DATABASE_URL=postgresql://user:password@host:port/database -
Important for existing databases:
If your database already has tables (production), you need to mark the baseline migration as applied:
alembic stamp headThis tells Alembic that the current database state matches the baseline migration, without actually running it.
If your database is empty, you can run:
alembic upgrade head
Running Migrations
Check Migration Status
alembic current
Shows the current revision of the database.
alembic history
Shows all available migrations and their revision chain.
Apply Migrations
alembic upgrade head
Applies all pending migrations up to the latest version.
alembic upgrade +1
Applies the next migration only.
Rollback Migrations
alembic downgrade -1
Rolls back the last migration.
alembic downgrade base
Rolls back all migrations (⚠️ DESTRUCTIVE - use with caution).
Creating New Migrations
Auto-generate Migration
alembic revision --autogenerate -m "Description of changes"
This will analyze the current database state and generate a migration file. Always review the generated migration before applying it.
Manual Migration
alembic revision -m "Description of changes"
Creates an empty migration file that you can fill in manually.
Migration Files
Migrations are stored in alembic/versions/ with the format:
001_initial_schema.py- Baseline migration002_add_feature_x.py- Feature-specific migrations- etc.
Each migration file contains:
revision: Unique identifier for this migrationdown_revision: The previous migration (forms a chain)upgrade(): Function that applies the migrationdowngrade(): Function that rolls back the migration
Best Practices
- Always test migrations on a development/staging database first
- Review auto-generated migrations - Alembic may not detect all changes correctly
- Keep migrations small - One logical change per migration
- Never edit applied migrations - Create a new migration to fix issues
- Backup before major migrations - Especially when dropping tables or columns
- Use transactions - Alembic wraps migrations in transactions by default
Integration with Bot
The bot can check migration status on startup. To enable automatic migration checks:
- Add migration check to
bot.pystartup hook - Use
/migrate statuscommand to check status manually - Use
/migratecommand (admin only) to apply migrations
Troubleshooting
Migration conflicts
If migrations are out of sync:
# Check current state
alembic current
# See what's pending
alembic heads
# If needed, mark current state manually
alembic stamp head
Database connection issues
Ensure DATABASE_URL is set correctly:
export DATABASE_URL="postgresql://user:pass@localhost/dbname"
Migration fails mid-way
If a migration fails partway through:
- Check the error message
- Fix the migration file if needed
- Manually fix the database state if necessary
- Use
alembic stampto mark the correct revision
Migration Workflow
- Development: Create migration locally
alembic revision --autogenerate -m "Add new feature" -
Review: Check the generated migration file
- Test: Apply migration on dev database
alembic upgrade head -
Commit: Add migration file to git
- Deploy: Apply migrations on production
alembic upgrade head
Current Schema
The baseline migration (001_initial_schema.py) includes:
bot_settings- Guild-specific bot configurationsettings_history- Audit trail for settings changesreminders- Reminder system dataonboarding- User onboarding responsesguild_onboarding_questions- Onboarding question definitionsguild_rules- Guild rulessupport_tickets- Support ticket systemfaq_entries- FAQ entriesfaq_search_logs- FAQ search analyticsaudit_logs- Command usage analyticshealth_check_history- Health check trend data