UAT: init_database() in models.py uses Base.metadata.create_all() bypassing Alembic migrations — schema divergence risk #5451

Open
opened 2026-04-09 06:53:28 +00:00 by HAL9000 · 1 comment
Owner

Bug Report

Feature Area: Database / Migrations — Database Initialization
Severity: Backlog — risk of schema divergence
Found by: UAT Testing (database-migrations worker)


Summary

The init_database() function in models.py (line 3330-3341) uses Base.metadata.create_all(engine) to initialize the database schema. This bypasses Alembic migrations entirely, creating tables directly from the ORM model definitions. This can cause schema divergence between databases initialized via init_database() and those initialized via Alembic migrations.

Evidence

init_database() in models.py (lines 3330-3341):

def init_database(database_url: str = "sqlite:///.cleveragents/db.sqlite") -> Any:
    """Initialize the database."""
    engine = create_engine(database_url, echo=False)
    Base.metadata.create_all(engine)  # ← Bypasses Alembic!
    return engine

Correct approachUnitOfWork.init_database() (unit_of_work.py:155-172):

def init_database(self) -> None:
    """Initialize database schema via Alembic migrations."""
    self._ensure_database_initialized(force=True)  # ← Uses MigrationRunner

Impact

  1. Any code that calls models.init_database() instead of UnitOfWork.init_database() will create a database without Alembic version tracking
  2. The resulting database will have alembic_version table missing, so subsequent init_or_upgrade() calls will try to stamp it at head (treating it as a pre-Alembic legacy database)
  3. Schema differences between ORM models and migration scripts (e.g., server_default values, CHECK constraints added in later migrations) may not be applied correctly
  4. The function uses a different default path (.cleveragents/db.sqlite) than the application's actual default (~/.cleveragents/cleveragents.db)

Expected Behavior

init_database() in models.py should either:

  1. Be removed (it's a legacy function superseded by UnitOfWork.init_database())
  2. Be updated to use MigrationRunner instead of create_all()
  3. Have a deprecation warning added

Code Locations

  • src/cleveragents/infrastructure/database/models.py:3330-3341init_database() using create_all()
  • src/cleveragents/infrastructure/database/unit_of_work.py:155-172 — correct init_database() using migrations

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

## Bug Report **Feature Area**: Database / Migrations — Database Initialization **Severity**: Backlog — risk of schema divergence **Found by**: UAT Testing (database-migrations worker) --- ## Summary The `init_database()` function in `models.py` (line 3330-3341) uses `Base.metadata.create_all(engine)` to initialize the database schema. This bypasses Alembic migrations entirely, creating tables directly from the ORM model definitions. This can cause schema divergence between databases initialized via `init_database()` and those initialized via Alembic migrations. ## Evidence **`init_database()` in models.py** (lines 3330-3341): ```python def init_database(database_url: str = "sqlite:///.cleveragents/db.sqlite") -> Any: """Initialize the database.""" engine = create_engine(database_url, echo=False) Base.metadata.create_all(engine) # ← Bypasses Alembic! return engine ``` **Correct approach** — `UnitOfWork.init_database()` (unit_of_work.py:155-172): ```python def init_database(self) -> None: """Initialize database schema via Alembic migrations.""" self._ensure_database_initialized(force=True) # ← Uses MigrationRunner ``` ## Impact 1. Any code that calls `models.init_database()` instead of `UnitOfWork.init_database()` will create a database without Alembic version tracking 2. The resulting database will have `alembic_version` table missing, so subsequent `init_or_upgrade()` calls will try to stamp it at `head` (treating it as a pre-Alembic legacy database) 3. Schema differences between ORM models and migration scripts (e.g., `server_default` values, CHECK constraints added in later migrations) may not be applied correctly 4. The function uses a different default path (`.cleveragents/db.sqlite`) than the application's actual default (`~/.cleveragents/cleveragents.db`) ## Expected Behavior `init_database()` in `models.py` should either: 1. Be removed (it's a legacy function superseded by `UnitOfWork.init_database()`) 2. Be updated to use `MigrationRunner` instead of `create_all()` 3. Have a deprecation warning added ## Code Locations - `src/cleveragents/infrastructure/database/models.py:3330-3341` — `init_database()` using `create_all()` - `src/cleveragents/infrastructure/database/unit_of_work.py:155-172` — correct `init_database()` using migrations --- **Automated by CleverAgents Bot** Supervisor: UAT Testing | Agent: uat-tester
HAL9000 added this to the v3.5.0 milestone 2026-04-09 06:59:58 +00:00
Author
Owner

Label compliance fix applied:

  • Added missing labels and/or milestone to bring issue into compliance with CONTRIBUTING.md

Automated by CleverAgents Bot
Supervisor: Backlog Grooming | Agent: backlog-groomer

Label compliance fix applied: - Added missing labels and/or milestone to bring issue into compliance with CONTRIBUTING.md --- **Automated by CleverAgents Bot** Supervisor: Backlog Grooming | Agent: backlog-groomer
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.

Reference
cleveragents/cleveragents-core#5451
No description provided.