Apply database schema migrations (alembic upgrade head, alembic downgrade, resolve diverged heads) for Backend.AI components (manager, accountmgr, appproxy)
Execute Alembic migrations to upgrade or downgrade database schema for Backend.AI components.
When to use:
Safety note: Always check status with /db-status before migrating.
component (optional): Target component
manager (default) - Main manager databaseaccountmgr - Account manager databaseappproxy - Application proxy databasedirection (optional): Migration direction
upgrade (default) - Apply newer migrationsdowngrade - Rollback migrationstarget (optional): Target revision
head (default) - Latest migration<revision_id> - Specific revision (e.g., abc123def456)+1, -1, +2, etc.Before executing migration, verify:
/db-status first)# Check current revision and pending migrations
/db-status --component=<component>
alembic.inialembic-accountmgr.inialembic-appproxy.ini./py -m alembic -c <config> <direction> <target>
Examples:
# Upgrade manager to latest
./py -m alembic -c alembic.ini upgrade head
# Upgrade appproxy to latest
./py -m alembic -c alembic-appproxy.ini upgrade head
# Downgrade manager by 1 revision
./py -m alembic -c alembic.ini downgrade -1
# Upgrade to specific revision
./py -m alembic -c alembic.ini upgrade abc123def456
# Confirm migration success
/db-status --component=<component>
Successful upgrade:
✅ Migration successful
Component: manager
Direction: upgrade
Target: head
Applied migrations:
- abc123def456 → def456ghi789: Add domain fair share
- def456ghi789 → ghi789jkl012: Add session priority
New status: Database is up-to-date
Next steps:
- Verify application functionality
- Restart affected services if needed
Successful downgrade:
✅ Migration successful
Component: manager
Direction: downgrade
Target: -1
Reverted migrations:
- ghi789jkl012 → def456ghi789: Add session priority (reverted)
New status: Database at revision def456ghi789
Warning: Downgrade may have removed data or features.
Verify application compatibility.
Multiple heads detected:
❌ Error: Multiple heads detected
Details: Cannot proceed with migration when heads are diverged
Recommended actions:
1. Check diverged heads: /db-status
2. Resolve heads: /db-rebase
3. Retry migration: /db-migrate
Related skill: /db-rebase
Cannot locate revision:
❌ Error: Cannot locate revision 'abc123'
Details: Revision not found in migration history
Recommended actions:
1. List available revisions:
./py -m alembic -c alembic.ini history
2. Check for typos in revision ID
3. Ensure you're using correct component config
Use /db-status to see current and head revisions.
Foreign key constraint failed:
❌ Error: Foreign key constraint violation
Details: Cannot add/modify column due to existing data
Recommended actions:
1. Check migration script for data migration logic
2. Manually fix data integrity issues
3. Consider modifying migration to handle existing data
4. Rollback: /db-migrate --direction=downgrade --target=-1
This usually requires manual intervention. Review migration file.
Database connection failed:
❌ Error: Cannot connect to database
Details: Connection refused to localhost:5432
Recommended actions:
1. Check PostgreSQL is running: docker ps | grep postgres
2. Verify database configuration
3. Start infrastructure: ./scripts/run-dev.sh
Related skill: /cli-executor
Migration file missing:
❌ Error: Migration file not found
Details: Expected migration at versions/abc123_add_field.py
Recommended actions:
1. Ensure you pulled latest code: git pull
2. Check if migration was committed
3. Verify correct branch
4. Generate migration if needed: /db-create-migration
/db-status - Check status before and after migration/db-rebase - Resolve diverged heads before migration/db-create-migration - Create new migrations/cli-executor - Check infrastructure and start servicesApply pending migrations:
User: "Run database migrations"
Agent:
1. /db-status # Check current status
2. /db-migrate # Apply migrations
3. /db-status # Verify success
Upgrade specific component:
User: "Upgrade appproxy database"
Agent: /db-migrate --component=appproxy
Rollback last migration:
User: "Rollback last database migration"
Agent: /db-migrate --direction=downgrade --target=-1
Upgrade to specific revision:
User: "Upgrade database to revision abc123"
Agent: /db-migrate --target=abc123def456
Typical development workflow:
1. Pull code: git pull origin main
2. Check status: /db-status
3. Apply migrations: /db-migrate
4. Start services: /cli-executor --component=mgr --subcommand=start-server
5. Verify functionality
After creating new migration:
1. Generate migration: /db-create-migration --message="Add new field"
2. Review generated file
3. Test upgrade: /db-migrate
4. Test downgrade: /db-migrate --direction=downgrade --target=-1
5. Re-upgrade: /db-migrate
6. Commit migration file
Resolving merge conflicts:
1. Merge branches: git merge feature-branch
2. Check for diverged heads: /db-status
3. If diverged, resolve: /db-rebase
4. Apply migrations: /db-migrate
5. Verify: /db-status