UAT: new_models.py defines a separate SQLAlchemy Base not imported by Alembic env.py — duplicate ORM models outside migration tracking #3974

Open
opened 2026-04-06 08:09:28 +00:00 by freemo · 0 comments
Owner

Metadata

  • Branch: fix/db-new-models-alembic-migration-tracking
  • Commit Message: fix(db): remove new_models.py to eliminate alembic migration blind spot and duplicate ORM base
  • Milestone: None (Backlog)
  • Parent Epic: #1020

Subtasks

  • Confirm new_models.py is not imported anywhere in the codebase (grep for new_models)
  • Verify alembic/env.py only imports Base from models.py (line 12)
  • Confirm all 4 duplicate models in new_models.py are already present with complete schemas in models.py
  • Delete src/cleveragents/infrastructure/database/new_models.py
  • Run nox -e lint and nox -e typecheck to confirm no regressions
  • Run nox -e unit_tests and nox -e integration_tests to confirm no regressions
  • Run nox -e coverage_report to confirm coverage >= 97%
  • Verify agents db migrate (autogenerate) no longer has a blind spot for the removed models

Definition of Done

  • All subtasks above are checked
  • src/cleveragents/infrastructure/database/new_models.py no longer exists in the repository
  • No import of new_models exists anywhere in the codebase
  • alembic/env.py target_metadata correctly reflects all ORM models via models.py Base
  • agents db migrate autogenerate detects no phantom schema differences from the removed file
  • Commit created with message: fix(db): remove new_models.py to eliminate alembic migration blind spot and duplicate ORM base
  • Branch pushed and PR merged
  • All nox stages pass
  • Coverage >= 97%

Bug Report

Feature Area: Infrastructure / Database Layer — Alembic Migration Tracking
Severity: Medium — causes schema drift risk, autogenerate false negatives, and developer confusion
Found by: UAT tester (code analysis of alembic/env.py and infrastructure/database/)

Related issue: See also #3961 which covers the SQLAlchemy metadata conflict angle of the same file. This issue focuses specifically on the Alembic migration tracking failure.


What Was Tested

Code analysis of src/cleveragents/infrastructure/database/new_models.py, alembic/env.py, and src/cleveragents/infrastructure/database/models.py.

Problem

src/cleveragents/infrastructure/database/new_models.py defines its own Base = declarative_base() that is completely separate from the Base in src/cleveragents/infrastructure/database/models.py. The Alembic env.py only imports Base from models.py:

# alembic/env.py line 12
from cleveragents.infrastructure.database.models import Base
target_metadata = Base.metadata

This means the models defined in new_models.py are NOT included in Alembic's autogenerate target metadata. Any schema changes made only to new_models.py models will not be detected by agents db migrate (autogenerate), and the tables defined there will not be tracked by migrations.

Duplicate Models Outside Migration Tracking

new_models.py defines models for tables that are also defined in models.py:

Class Table In models.py? Schema Match?
ResourceModel resources ✓ Yes No — different column names
ResourceEdgeModel resource_edges ✓ Yes No — missing columns
DecisionModel decisions ✓ Yes No — prompt/decision vs question/chosen_option
DecisionDependencyModel decision_dependencies ✓ Yes No — different column names

The new_models.py versions have simpler, incomplete schemas compared to the authoritative models.py definitions.

Dead Code Confirmation

new_models.py is not imported anywhere in the codebase (confirmed by grep — no file imports from new_models), confirming it is dead code that was never integrated.

Impact

  1. Schema drift risk: If a developer adds columns to new_models.py models, those changes will never appear in Alembic migrations, causing the database schema to diverge from the ORM models silently.
  2. Autogenerate false negatives: Running agents db migrate will not detect schema differences for models defined only in new_models.py.
  3. Developer confusion: Two files defining models for the same tables with different schemas creates ambiguity about which is authoritative.
  4. Dead code: new_models.py is not imported anywhere, so it provides no value while creating confusion and a migration blind spot.

Code Locations

  • src/cleveragents/infrastructure/database/new_models.py — entire file (separate Base, duplicate models, dead code)
  • alembic/env.py line 12 — only imports Base from models.py, missing new_models.py Base
  • src/cleveragents/infrastructure/database/models.py — authoritative models with complete spec-aligned schemas

Fix Required

Option A (Preferred): Delete new_models.py entirely. The models it defines are already present in models.py with more complete, spec-aligned schemas. Verify no code references new_models.py before deletion.

Option B: If new_models.py is intended to be a future replacement for parts of models.py, merge the relevant models into models.py and update alembic/env.py to import from the merged file.


Backlog note: This issue was discovered during autonomous operation
on milestone v3.6.0. It does not block milestone completion and has been
placed in the backlog for human review and future milestone assignment.


Automated by CleverAgents Bot
Supervisor: UAT Testing | Agent: ca-uat-tester

## Metadata - **Branch**: `fix/db-new-models-alembic-migration-tracking` - **Commit Message**: `fix(db): remove new_models.py to eliminate alembic migration blind spot and duplicate ORM base` - **Milestone**: None (Backlog) - **Parent Epic**: #1020 ## Subtasks - [ ] Confirm `new_models.py` is not imported anywhere in the codebase (grep for `new_models`) - [ ] Verify `alembic/env.py` only imports `Base` from `models.py` (line 12) - [ ] Confirm all 4 duplicate models in `new_models.py` are already present with complete schemas in `models.py` - [ ] Delete `src/cleveragents/infrastructure/database/new_models.py` - [ ] Run `nox -e lint` and `nox -e typecheck` to confirm no regressions - [ ] Run `nox -e unit_tests` and `nox -e integration_tests` to confirm no regressions - [ ] Run `nox -e coverage_report` to confirm coverage >= 97% - [ ] Verify `agents db migrate` (autogenerate) no longer has a blind spot for the removed models ## Definition of Done - [ ] All subtasks above are checked - [ ] `src/cleveragents/infrastructure/database/new_models.py` no longer exists in the repository - [ ] No import of `new_models` exists anywhere in the codebase - [ ] `alembic/env.py` `target_metadata` correctly reflects all ORM models via `models.py` `Base` - [ ] `agents db migrate` autogenerate detects no phantom schema differences from the removed file - [ ] Commit created with message: `fix(db): remove new_models.py to eliminate alembic migration blind spot and duplicate ORM base` - [ ] Branch pushed and PR merged - [ ] All nox stages pass - [ ] Coverage >= 97% --- ## Bug Report **Feature Area:** Infrastructure / Database Layer — Alembic Migration Tracking **Severity:** Medium — causes schema drift risk, autogenerate false negatives, and developer confusion **Found by:** UAT tester (code analysis of `alembic/env.py` and `infrastructure/database/`) > **Related issue:** See also #3961 which covers the SQLAlchemy metadata conflict angle of the same file. This issue focuses specifically on the Alembic migration tracking failure. --- ## What Was Tested Code analysis of `src/cleveragents/infrastructure/database/new_models.py`, `alembic/env.py`, and `src/cleveragents/infrastructure/database/models.py`. ## Problem `src/cleveragents/infrastructure/database/new_models.py` defines its own `Base = declarative_base()` that is completely separate from the `Base` in `src/cleveragents/infrastructure/database/models.py`. The Alembic `env.py` only imports `Base` from `models.py`: ```python # alembic/env.py line 12 from cleveragents.infrastructure.database.models import Base target_metadata = Base.metadata ``` This means the models defined in `new_models.py` are **NOT included** in Alembic's autogenerate target metadata. Any schema changes made only to `new_models.py` models will not be detected by `agents db migrate` (autogenerate), and the tables defined there will not be tracked by migrations. ### Duplicate Models Outside Migration Tracking `new_models.py` defines models for tables that are **also** defined in `models.py`: | Class | Table | In `models.py`? | Schema Match? | |-------|-------|-----------------|---------------| | `ResourceModel` | `resources` | ✓ Yes | ❌ No — different column names | | `ResourceEdgeModel` | `resource_edges` | ✓ Yes | ❌ No — missing columns | | `DecisionModel` | `decisions` | ✓ Yes | ❌ No — `prompt`/`decision` vs `question`/`chosen_option` | | `DecisionDependencyModel` | `decision_dependencies` | ✓ Yes | ❌ No — different column names | The `new_models.py` versions have simpler, incomplete schemas compared to the authoritative `models.py` definitions. ### Dead Code Confirmation `new_models.py` is **not imported anywhere** in the codebase (confirmed by grep — no file imports from `new_models`), confirming it is dead code that was never integrated. ## Impact 1. **Schema drift risk**: If a developer adds columns to `new_models.py` models, those changes will never appear in Alembic migrations, causing the database schema to diverge from the ORM models silently. 2. **Autogenerate false negatives**: Running `agents db migrate` will not detect schema differences for models defined only in `new_models.py`. 3. **Developer confusion**: Two files defining models for the same tables with different schemas creates ambiguity about which is authoritative. 4. **Dead code**: `new_models.py` is not imported anywhere, so it provides no value while creating confusion and a migration blind spot. ## Code Locations - **`src/cleveragents/infrastructure/database/new_models.py`** — entire file (separate `Base`, duplicate models, dead code) - **`alembic/env.py` line 12** — only imports `Base` from `models.py`, missing `new_models.py` Base - **`src/cleveragents/infrastructure/database/models.py`** — authoritative models with complete spec-aligned schemas ## Fix Required **Option A (Preferred):** Delete `new_models.py` entirely. The models it defines are already present in `models.py` with more complete, spec-aligned schemas. Verify no code references `new_models.py` before deletion. **Option B:** If `new_models.py` is intended to be a future replacement for parts of `models.py`, merge the relevant models into `models.py` and update `alembic/env.py` to import from the merged file. --- > **Backlog note:** This issue was discovered during autonomous operation > on milestone v3.6.0. It does not block milestone completion and has been > placed in the backlog for human review and future milestone assignment. --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: ca-uat-tester
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Blocks
Reference
cleveragents/cleveragents-core#3974
No description provided.