Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(update): treat a configured database as a store (#2404)

Nehal kanak kuniyil thottathil4f51ee7d4d69
diff --git a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
index 71c3698a1..10dd0a9f1 100644
--- a/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
+++ b/packages/cli/src/repowise/cli/commands/update_cmd/persistence.py
@@ -207,8 +207,10 @@ def stamp_head_commit(repo_path: Any, head: str | None) -> None:
# One stamper for both update paths: delegate to the core implementation
# the workspace updater uses. It touches only head_commit/updated_at on an
# existing row (the old upsert here clobbered url/default_branch with
- # defaults), creates the row when missing from an existing wiki.db, and
- # no-ops when wiki.db itself is absent instead of conjuring an empty DB.
+ # defaults), creates the row when missing from an existing store, and
+ # no-ops when no store exists at all instead of conjuring an empty DB. A
+ # configured database counts as one, which the repo-local file check this
+ # used to make could never see.
from repowise.core.workspace.update import reconcile_repo_head_commit
run_async(reconcile_repo_head_commit(Path(repo_path), head))
@@ -228,8 +230,10 @@ def heal_commit_offsets(repo_path: Any) -> None:
once the column is filled, and no git at all in that case. Best-effort — a
failure here must never turn a clean no-op into an error.
"""
+ from repowise.core.persistence.database import has_db_store
+
root = Path(repo_path)
- if not (root / ".repowise" / "wiki.db").is_file():
+ if not has_db_store(root):
return
async def _run() -> None:
diff --git a/packages/core/src/repowise/core/persistence/database.py b/packages/core/src/repowise/core/persistence/database.py
index ba675492c..315d12385 100644
--- a/packages/core/src/repowise/core/persistence/database.py
+++ b/packages/core/src/repowise/core/persistence/database.py
@@ -229,6 +229,23 @@ def resolve_db_url(repo_path: str | Path | None = None) -> str:
return _default_db_url(repo_path)
+def has_db_store(repo_path: str | Path | None = None) -> bool:
+ """Whether :func:`resolve_db_url` has a store that already exists.
+
+ A configured database counts as existing. It is shared, it is migrated on
+ its own schedule, and the repo-local file it replaces is absent by design,
+ so a caller that gates on the file alone skips every write under one.
+
+ The filesystem defaults count only when the file is really there, which is
+ what keeps a caller from conjuring an empty database where none existed.
+ """
+ if get_configured_db_url() is not None:
+ return True
+ if repo_path is None:
+ return False
+ return (Path(repo_path) / ".repowise" / "wiki.db").is_file()
+
+
def create_engine(
url: str | None = None,
*,
diff --git a/packages/core/src/repowise/core/workspace/update.py b/packages/core/src/repowise/core/workspace/update.py
index 78b2834d2..50469d817 100644
--- a/packages/core/src/repowise/core/workspace/update.py
+++ b/packages/core/src/repowise/core/workspace/update.py
@@ -306,16 +306,20 @@ async def reconcile_repo_head_commit(repo_path: Path, head: str | None) -> None:
advances ``updated_at`` so the freshness time reflects the latest
sync-check — a routine ``repowise update`` that finds nothing to do still
counts as "verified current now". Creates the row when it is missing from
- an existing ``wiki.db`` (self-heals a corrupt/blank store — the policy the
- CLI's ``stamp_head_commit``, now a thin wrapper over this, always had);
- still a no-op when ``wiki.db`` itself is absent, so a stamp can never
- conjure an empty database.
+ an existing store (self-heals a corrupt/blank store — the policy the CLI's
+ ``stamp_head_commit``, now a thin wrapper over this, always had); still a
+ no-op when no store exists at all, so a stamp can never conjure an empty
+ database. A configured database counts as existing: it is shared, and the
+ repo-local file it replaces is absent by design, so gating on the file
+ alone skipped every stamp under one.
This is the single head-commit stamper for both update paths — the CLI
fast paths and the workspace updater used to run two implementations with
different creation semantics.
"""
- if not head or not (repo_path / ".repowise" / "wiki.db").is_file():
+ from ..persistence.database import has_db_store, resolve_db_url
+
+ if not head or not has_db_store(repo_path):
return
from ..persistence import (
create_engine,
@@ -325,7 +329,6 @@ async def reconcile_repo_head_commit(repo_path: Path, head: str | None) -> None:
upsert_repository,
)
from ..persistence.crud import get_repository_by_path
- from ..persistence.database import resolve_db_url
url = resolve_db_url(repo_path)
engine = create_engine(url)
diff --git a/tests/unit/cli/test_update_stamp_configured_db.py b/tests/unit/cli/test_update_stamp_configured_db.py
new file mode 100644
index 000000000..abd59136a
--- /dev/null
+++ b/tests/unit/cli/test_update_stamp_configured_db.py
@@ -0,0 +1,146 @@
+"""A configured database is a store, even with no repo-local ``wiki.db``.
+
+Both head-commit writers guarded on the repo-local file alone and then built
+their engine from ``resolve_db_url``, which prefers ``REPOWISE_DB_URL``. Under a
+shared database that file is absent by design, so the guard returned before the
+engine was ever built: ``repowise update`` left ``head_commit`` and
+``updated_at`` stale in the row that ``/api/repos``, MCP ``_meta`` and the
+health overview read, and the commit-offset heal never ran at all.
+
+These tests pin the three cases the guard has to tell apart: a configured
+database, a repo-local file, and neither.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from repowise.cli.commands.update_cmd.persistence import heal_commit_offsets
+from repowise.core.persistence import create_engine, create_session_factory, get_session
+from repowise.core.persistence.crud import get_repository_by_path
+from repowise.core.persistence.database import has_db_store
+from repowise.core.workspace.update import reconcile_repo_head_commit
+
+HEAD = "a" * 40
+
+
+def _clear_db_env(monkeypatch) -> None:
+ for name in ("REPOWISE_DB_URL", "REPOWISE_DATABASE_URL"):
+ monkeypatch.delenv(name, raising=False)
+
+
+def _configure_shared_db(monkeypatch, tmp_path: Path) -> Path:
+ """Point both env vars at a shared store outside the repo, as Postgres would be."""
+ shared = tmp_path / "shared" / "index.db"
+ shared.parent.mkdir(parents=True, exist_ok=True)
+ _clear_db_env(monkeypatch)
+ monkeypatch.setenv("REPOWISE_DB_URL", f"sqlite:///{shared.as_posix()}")
+ return shared
+
+
+def test_has_db_store_counts_a_configured_database(tmp_path, monkeypatch):
+ _configure_shared_db(monkeypatch, tmp_path)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ assert not (repo / ".repowise" / "wiki.db").exists()
+ assert has_db_store(repo) is True
+
+
+def test_has_db_store_needs_the_file_when_nothing_is_configured(tmp_path, monkeypatch):
+ _clear_db_env(monkeypatch)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ assert has_db_store(repo) is False
+
+ (repo / ".repowise").mkdir()
+ (repo / ".repowise" / "wiki.db").write_bytes(b"")
+ assert has_db_store(repo) is True
+
+
+async def test_stamp_writes_the_row_through_a_configured_database(tmp_path, monkeypatch):
+ shared = _configure_shared_db(monkeypatch, tmp_path)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ await reconcile_repo_head_commit(repo, HEAD)
+
+ engine = create_engine(f"sqlite:///{shared.as_posix()}")
+ try:
+ async with get_session(create_session_factory(engine)) as session:
+ row = await get_repository_by_path(session, str(repo))
+ finally:
+ await engine.dispose()
+
+ assert row is not None, "a configured database is a store; the stamp has to land"
+ assert row.head_commit == HEAD
+
+
+def test_heal_reaches_a_configured_database(tmp_path, monkeypatch):
+ shared = _configure_shared_db(monkeypatch, tmp_path)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ assert not shared.exists()
+
+ heal_commit_offsets(repo)
+
+ # Reaching the store means opening it, and opening it migrates first. A
+ # guard that returns early leaves the shared database untouched.
+ assert shared.exists(), "the heal returned before it ever opened the store"
+
+
+def test_stamp_still_writes_to_the_repo_local_store(tmp_path, monkeypatch):
+ _clear_db_env(monkeypatch)
+ repo = tmp_path / "repo"
+ (repo / ".repowise").mkdir(parents=True)
+ (repo / ".repowise" / "wiki.db").write_bytes(b"")
+
+ import asyncio
+
+ asyncio.run(reconcile_repo_head_commit(repo, HEAD))
+
+ local = repo / ".repowise" / "wiki.db"
+ engine = create_engine(f"sqlite:///{local.as_posix()}")
+
+ async def _read():
+ async with get_session(create_session_factory(engine)) as session:
+ return await get_repository_by_path(session, str(repo))
+
+ try:
+ row = asyncio.run(_read())
+ finally:
+ asyncio.run(engine.dispose())
+
+ assert row is not None
+ assert row.head_commit == HEAD
+
+
+def test_stamp_still_noops_when_there_is_no_store_at_all(tmp_path, monkeypatch):
+ _clear_db_env(monkeypatch)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ import asyncio
+
+ asyncio.run(reconcile_repo_head_commit(repo, HEAD))
+
+ assert not (repo / ".repowise" / "wiki.db").exists(), (
+ "a stamp must never conjure an empty database"
+ )
+
+
[email protected]("head", [None, ""])
+def test_stamp_ignores_an_absent_head(tmp_path, monkeypatch, head):
+ shared = _configure_shared_db(monkeypatch, tmp_path)
+ repo = tmp_path / "repo"
+ repo.mkdir()
+
+ import asyncio
+
+ asyncio.run(reconcile_repo_head_commit(repo, head))
+
+ assert not shared.exists()