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(status/doctor): query configured DB when no local wiki.db exists (#1320)
404 Ameyyee06338f630a
diff --git a/packages/cli/src/repowise/cli/commands/doctor_cmd/repo_checks.py b/packages/cli/src/repowise/cli/commands/doctor_cmd/repo_checks.pyindex b2e1d1d5f..066df8e04 100644--- a/packages/cli/src/repowise/cli/commands/doctor_cmd/repo_checks.py+++ b/packages/cli/src/repowise/cli/commands/doctor_cmd/repo_checks.py@@ -9,6 +9,7 @@ from rich.table import Tablefrom repowise.cli.helpers import (console,+ db_configured,get_db_url_for_repo,get_repowise_dir,load_state,@@ -67,9 +68,10 @@ def _run_repo_checks(# 3. Database connectable?db_path = repowise_dir / "wiki.db"+ probed = db_path.exists() or db_configured()db_ok = Falsepage_count = 0- if db_path.exists():+ if probed:try:async def _check_db():@@ -99,7 +101,7 @@ def _run_repo_checks(checks.append(_check("Database", False, str(e)))if db_ok:checks.append(_check("Database", True, f"{page_count} pages"))- elif not db_path.exists():+ elif not probed:checks.append(_check("Database", False, "wiki.db not found"))# 4. state.json valid?diff --git a/packages/cli/src/repowise/cli/commands/status_cmd.py b/packages/cli/src/repowise/cli/commands/status_cmd.pyindex 5afc61579..85bd8dcc4 100644--- a/packages/cli/src/repowise/cli/commands/status_cmd.py+++ b/packages/cli/src/repowise/cli/commands/status_cmd.py@@ -12,6 +12,7 @@ from rich.table import Tablefrom repowise.cli.helpers import (CommandTarget,console,+ db_configured,get_db_url_for_repo,get_repowise_dir,load_state,@@ -90,7 +91,7 @@ def _query_repo_counts(repo_path: Path) -> tuple[int, int]:await engine.dispose()db_path = get_repowise_dir(repo_path) / "wiki.db"- if not db_path.exists():+ if not db_path.exists() and not db_configured():return 0, 0try:return run_async(_query())@@ -133,7 +134,7 @@ def _query_page_count(repo_path: Path) -> int:await engine.dispose()db_path = get_repowise_dir(repo_path) / "wiki.db"- if not db_path.exists():+ if not db_path.exists() and not db_configured():return 0try:return run_async(_query())@@ -150,7 +151,7 @@ def _query_health_line(repo_path: Path) -> str | None:Health: 7.4 (avg) · 6.2 (hotspots) · 2.1 (worst: payments/processor.ts)"""db_path = get_repowise_dir(repo_path) / "wiki.db"- if not db_path.exists():+ if not db_path.exists() and not db_configured():return Noneasync def _q() -> dict | None:@@ -423,8 +424,8 @@ def status_command(path: str | None, workspace: bool, no_workspace: bool) -> Non# Page counts from DBdb_path = repowise_dir / "wiki.db"- if not db_path.exists():- console.print("[yellow]Database not found.[/yellow]")+ if not db_path.exists() and not db_configured():+ console.print(f"[yellow]Database not found at {db_path}.[/yellow]")returnasync def _query_pages():diff --git a/packages/cli/src/repowise/cli/helpers.py b/packages/cli/src/repowise/cli/helpers.pyindex 0c8e9f07e..3754257c4 100644--- a/packages/cli/src/repowise/cli/helpers.py+++ b/packages/cli/src/repowise/cli/helpers.py@@ -182,6 +182,17 @@ def get_db_url_for_repo(repo_path: Path) -> str:return resolve_db_url(repo_path)+def db_configured() -> bool:+ """True when ``REPOWISE_DB_URL`` or ``REPOWISE_DATABASE_URL`` is set.++ The DB may still be the repo-local ``wiki.db`` default — the file+ existence check the callers pair this with decides that.+ """+ from repowise.core.persistence import get_configured_db_url++ return get_configured_db_url() is not None++# ---------------------------------------------------------------------------# State file# ---------------------------------------------------------------------------diff --git a/tests/integration/test_cli.py b/tests/integration/test_cli.pyindex 6a2055c55..73043797c 100644--- a/tests/integration/test_cli.py+++ b/tests/integration/test_cli.py@@ -286,6 +286,110 @@ class TestDoctorAfterInit:assert "repowise Doctor" in result.output+class TestStatusDoctorWithEnvDb:+ """Regression guard for #1274: with REPOWISE_DB_URL set, status and doctor+ must query the configured DB even when no repo-local wiki.db exists.++ The env DB lives outside the repo (like a Postgres container), so the+ pre-fix file-existence guards bailed out before ever resolving the URL.+ """++ @pytest.fixture+ def env_db_repo(self, tmp_path, sample_repo_path, monkeypatch):+ """sample_repo copy with REPOWISE_DB_URL pointing at an external DB."""+ dest = tmp_path / "repo"+ shutil.copytree(sample_repo_path, dest)+ db_path = tmp_path / "external.db"+ monkeypatch.setenv("REPOWISE_DB_URL", f"sqlite+aiosqlite:///{db_path}")+ return dest++ def test_status_queries_env_db_without_local_wiki_db(self, runner, env_db_repo):+ result = runner.invoke(+ cli,+ ["init", str(env_db_repo), "--provider", "mock", "--yes"],+ catch_exceptions=False,+ )+ assert result.exit_code == 0, result.output+ # The scenario that used to fail: data in the env DB, no local wiki.db.+ assert not (env_db_repo / ".repowise" / "wiki.db").exists()++ result = runner.invoke(+ cli,+ ["status", str(env_db_repo)],+ catch_exceptions=False,+ )+ assert result.exit_code == 0, result.output+ assert "Sync State" in result.output+ assert "Database not found" not in result.output+ assert "Pages by Type" in result.output++ def test_doctor_reports_db_ok_with_env_db(self, runner, env_db_repo):+ runner.invoke(+ cli,+ ["init", str(env_db_repo), "--provider", "mock", "--yes"],+ catch_exceptions=False,+ )+ assert not (env_db_repo / ".repowise" / "wiki.db").exists()++ result = runner.invoke(+ cli,+ ["doctor", str(env_db_repo)],+ catch_exceptions=False,+ )+ assert result.exit_code == 0, result.output+ assert "wiki.db not found" not in result.output++ def test_status_still_reports_not_found_with_no_db_configured(+ self, runner, tmp_path, sample_repo_path, monkeypatch+ ):+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)+ dest = tmp_path / "repo"+ shutil.copytree(sample_repo_path, dest)+ # A .repowise/ dir with state but no DB — the "Database not found"+ # branch requires an initialized repo to get past the earlier guard.+ (dest / ".repowise").mkdir()+ (dest / ".repowise" / "state.json").write_text("{}", encoding="utf-8")++ result = runner.invoke(cli, ["status", str(dest)], catch_exceptions=False)+ assert result.exit_code == 0, result.output+ assert "Database not found" in result.output++ def test_doctor_still_reports_fail_with_no_db_configured(+ self, runner, tmp_path, sample_repo_path, monkeypatch+ ):+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)+ dest = tmp_path / "repo"+ shutil.copytree(sample_repo_path, dest)++ result = runner.invoke(cli, ["doctor", str(dest)], catch_exceptions=False)+ assert result.exit_code == 0, result.output+ assert "wiki.db not found" in result.output++ def test_doctor_corrupt_local_db_shows_one_fail_row(+ self, runner, tmp_path, sample_repo_path, monkeypatch+ ):+ """A local wiki.db that exists but cannot be opened must report the real+ connection error — not an extra, contradictory "wiki.db not found" row+ (regression guard for the reviewer finding on #1274)."""+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)+ dest = tmp_path / "repo"+ shutil.copytree(sample_repo_path, dest)+ (dest / ".repowise").mkdir()+ (dest / ".repowise" / "state.json").write_text("{}", encoding="utf-8")+ # Corrupt: not a valid SQLite file.+ (dest / ".repowise" / "wiki.db").write_text("this is not a database", encoding="utf-8")++ result = runner.invoke(cli, ["doctor", str(dest)], catch_exceptions=False)+ assert result.exit_code == 0, result.output+ # Exactly one Database row — the real connection error, not a second+ # contradictory "wiki.db not found" row.+ assert "file is not a" in result.output+ assert "wiki.db not found" not in result.output++class TestSearchFulltext:def test_returns_results_or_no_error(self, runner, work_repo):runner.invoke(diff --git a/tests/unit/cli/test_helpers.py b/tests/unit/cli/test_helpers.pyindex 1254c0639..61e639d9c 100644--- a/tests/unit/cli/test_helpers.py+++ b/tests/unit/cli/test_helpers.py@@ -128,6 +128,20 @@ class TestDbUrl:assert url == f"sqlite+aiosqlite:///{expected_path}"assert (tmp_path / ".repowise").exists()+ def test_configured_db_url_none_without_env(self, monkeypatch):+ from repowise.core.persistence import get_configured_db_url++ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)+ assert get_configured_db_url() is None++ def test_configured_db_url_returns_normalized_env_url(self, monkeypatch):+ from repowise.core.persistence import get_configured_db_url++ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)+ monkeypatch.setenv("REPOWISE_DB_URL", "postgresql://user:pass@host:5432/db")+ assert get_configured_db_url() == "postgresql+asyncpg://user:pass@host:5432/db"+class TestResolveReasoning:def test_flag_wins_over_env(self, monkeypatch):