Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(persistence): a data step that moves no rows no longer fails the schema upgrade (#2738)
Raghav Chamadiya5e033adfd6d0
diff --git a/packages/core/src/repowise/core/persistence/database.py b/packages/core/src/repowise/core/persistence/database.pyindex f28f84fd0..3c9b3f24c 100644--- a/packages/core/src/repowise/core/persistence/database.py+++ b/packages/core/src/repowise/core/persistence/database.py@@ -394,6 +394,12 @@ _DATA_STEPS_ON_ADD: dict[str, Callable[[object], object]] = {}+def _run_data_step(step: Callable[[object], object], connection: object) -> None:+ """Run a data step for its effect. Its return value (a row count, often 0)+ is not a statement, so it must never reach ``connection.execute``."""+ step(connection)++def _reconcile_schema(connection: object) -> None:"""Bring an existing database up to ``Base.metadata`` (additive only).@@ -495,7 +501,7 @@ def _reconcile_schema(connection: object) -> None:)data_step = _DATA_STEPS_ON_ADD.get(what)if data_step is not None and not any(name == what for name, _ in failures):- _run(f"{what}:data", lambda data_step=data_step: data_step(connection) and None)+ _run(f"{what}:data", lambda data_step=data_step: _run_data_step(data_step, connection))# --- Indexes ---------------------------------------------------# Only model-declared indexes (i.e. ``Index(...)`` on the tablediff --git a/tests/unit/persistence/test_schema_reconciliation.py b/tests/unit/persistence/test_schema_reconciliation.pyindex 5a91abc25..7ea2f4ed1 100644--- a/tests/unit/persistence/test_schema_reconciliation.py+++ b/tests/unit/persistence/test_schema_reconciliation.py@@ -549,3 +549,28 @@ async def test_adding_owner_line_pct_moves_stale_blame_line_shares(tmp_path: Pat"commits.py": (0.75, None),"old.py": (None, 0.6),}+++async def test_adding_owner_line_pct_with_nothing_to_move_succeeds(tmp_path: Path) -> None:+ """The data step returns how many rows it moved. Zero is the common case+ (every row already right, or no git history at all) and must not be taken+ for a statement to execute."""+ db_path = tmp_path / "wiki.db"+ engine = create_engine(f"sqlite+aiosqlite:///{db_path}")+ try:+ await init_db(engine)+ finally:+ await engine.dispose()++ try:+ _execute(db_path, 'ALTER TABLE "git_metadata" DROP COLUMN "primary_owner_line_pct"')+ except sqlite3.OperationalError as exc:+ pytest.skip(f"SQLite build doesn't support DROP COLUMN: {exc}")++ engine = create_engine(f"sqlite+aiosqlite:///{db_path}")+ try:+ await init_db(engine)+ finally:+ await engine.dispose()++ assert "primary_owner_line_pct" in _table_columns(db_path, "git_metadata")