refactor(db): migrate from create_all() to Alembic-managed schema migrations #1040

Merged
freemo merged 1 commits from refactor/db-alembic-migrations into master 2026-03-18 17:30:22 +00:00

1 Commits

Author SHA1 Message Date
freemo 399939a6db refactor(db): migrate from create_all() to Alembic-managed schema migrations
CI / benchmark-publish (pull_request) Has been skipped
CI / build (pull_request) Successful in 16s
CI / quality (pull_request) Successful in 33s
CI / security (pull_request) Successful in 54s
CI / unit_tests (pull_request) Successful in 3m21s
CI / integration_tests (pull_request) Successful in 3m41s
CI / e2e_tests (pull_request) Successful in 3m56s
CI / coverage (pull_request) Successful in 7m38s
CI / docker (pull_request) Has been skipped
CI / lint (pull_request) Successful in 14s
CI / typecheck (pull_request) Successful in 44s
CI / lint (push) Successful in 13s
CI / build (push) Successful in 18s
CI / quality (push) Successful in 28s
CI / security (push) Successful in 40s
CI / typecheck (push) Successful in 1m0s
CI / benchmark-regression (push) Has been skipped
CI / e2e_tests (push) Successful in 4m12s
CI / unit_tests (push) Successful in 5m28s
CI / coverage (push) Successful in 4m27s
CI / integration_tests (push) Successful in 5m50s
CI / docker (push) Successful in 55s
CI / benchmark-publish (push) Has been cancelled
CI / benchmark-regression (pull_request) Successful in 37m42s
Complete the Alembic migration infrastructure by adding CLI commands,
improving stamp logic, and adding comprehensive lifecycle tests.

Key changes:
- Added agents db CLI command group (db.py) with 5 subcommands:
  migrate (autogenerate), upgrade, downgrade, current, history.
  All delegate to MigrationRunner which wraps Alembic command API.
- Registered the db command group in main.py CLI registration.
- Fixed legacy database stamp logic in MigrationRunner to stamp at
  "head" instead of "001_initial_schema" when pre-Alembic tables are
  detected. This avoids migration failures when create_all-produced
  tables already exist (migrations would try to CREATE TABLE and fail
  with "table already exists").
- Added commit() after stamp to ensure alembic_version is persisted
  before subsequent operations on the same in-memory database.
- Added FakeConnection.commit() method to the mock test infrastructure
  to support the new commit call in the stamp path.
- Added Behave feature (db_migration_lifecycle.feature) with 8
  scenarios covering: forward migration, rollback, round-trip,
  CLI upgrade/current/downgrade, legacy stamp logic, and
  init_database schema validation.
- Added vulture whitelist entries for new CLI commands.

Note: init_database() still uses Base.metadata.create_all() as the
primary schema creation path. Full migration to Alembic-only init is
deferred until ORM model constraints are reconciled with migration
scripts (action_arguments UniqueConstraint mismatch).

ISSUES CLOSED: #941
2026-03-18 17:16:32 +00:00