Guard manager data migrations for Alembic offline (--sql) mode
- Dominant language
- Python
- Stars
- 670
- Forks
- 183
- Avg merge
- 21h 49m
- Merged PRs (30d)
- 404
Description
### Scope
Manager only: `src/ai/backend/manager/models/alembic/versions/`.
### Problem
In Alembic offline mode (`alembic upgrade --sql`), `op.get_bind()` returns a MockConnection instead of a real SQLAlchemy Connection. Data migrations that call `.execute(...).fetchall()`, use `sa.inspect(bind)`, or branch on queried data will either crash or silently emit wrong SQL during dry-run, making `--sql` output unreliable for pre-deploy review.
### Approach
- Identify manager migrations that call `op.get_bind()` with data-dependent logic (`execute/fetch/inspect`).
- Wrap data-dependent blocks with `if not context.is_offline_mode():` — for the offline branch, either emit equivalent raw SQL/DDL or skip with a comment explaining why the step is online-only.
- Prefer idempotent DDL (`IF NOT EXISTS / IF EXISTS`) for schema-only paths so dry-run output is safe to apply.
### Acceptance Criteria
- `alembic upgrade head --sql` on the manager DB runs end-to-end from base without error.
- Each manager data migration either outputs equivalent SQL in offline mode or is explicitly and visibly skipped.
- Online `alembic upgrade head` behavior is unchanged.
JIRA Issue: BA-6086
Contributor guide
Research direction
Start in src/ai/backend/manager/models/alembic/versions/ and identify migrations using op.get_bind() with execute, fetchall(), inspect, or queried-data branches. Run alembic upgrade head --sql on the manager database from base to reproduce the offline failure, then compare with online alembic upgrade head behavior. Done means the offline run completes and each data migration emits equivalent SQL or visibly explains an online-only skip without changing online behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlalchemy
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100