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(uninstall): list a configured database as a kept row (#3397)
Aman88ac8d0e2f13
diff --git a/packages/cli/src/repowise/cli/uninstall/inventory.py b/packages/cli/src/repowise/cli/uninstall/inventory.pyindex 6bd7f9397..8eb39ca5a 100644--- a/packages/cli/src/repowise/cli/uninstall/inventory.py+++ b/packages/cli/src/repowise/cli/uninstall/inventory.py@@ -29,7 +29,8 @@ class Group(StrEnum):AGENTS = "agents"#: The managed blocks the generators write into the repo's own files.REPO_FILES = "repo-files"- #: The repo's ``.repowise/`` directory.+ #: The repo's ``.repowise/`` directory, and the row for a configured+ #: database when one holds the index instead. That row is always kept.INDEX = "index"#: ``~/.repowise/``, which is machine-wide.GLOBAL = "global"@@ -429,6 +430,53 @@ def _global_items() -> list[Item]:]+def _store_item() -> Item | None:+ """The configured database, as a row that is listed and never touched.++ Under ``REPOWISE_DB_URL`` the index is not in ``.repowise/`` at all, so the+ row built from the local path alone reports nothing while the largest thing+ repowise owns sits elsewhere. The database is shared and may hold other+ repositories, so this row is always *blocked*: it shows up, says why, and+ the runner reports it as kept. There is deliberately no way to remove it+ from here. That is a database task, not an uninstall one.++ Named by dialect, host and database only. The URL can carry a password and+ this row is printed.+ """+ from repowise.core.persistence.database import get_configured_db_url++ raw = get_configured_db_url()+ if raw is None:+ return None+ dialect, locator = "database", "configured database"+ try:+ from sqlalchemy.engine import make_url++ url = make_url(raw)+ dialect = url.get_backend_name()+ if dialect == "sqlite":+ locator = url.database or ":memory:"+ else:+ locator = url.host or "unknown-host"+ if url.port:+ locator = f"{locator}:{url.port}"+ if url.database:+ locator = f"{locator}/{url.database}"+ except Exception:+ # A label must never stop the inventory from being printed.+ pass+ return Item(+ group=Group.INDEX,+ path=Path(locator),+ label=f"index in the configured {dialect} database",+ exists=True,+ blocked=(+ "the database is shared and may hold other repositories, so uninstall "+ "never deletes from it; removing rows is a database task"+ ),+ )++def _advisories(repo_path: Path) -> list[str]:"""Lines that are true whatever the user picks."""lines: list[str] = []@@ -506,6 +554,9 @@ def build_plan(repo_path: Path) -> Plan:blocked=_index_blocked(repo_path) if index_path.exists() else None,))+ store_item = _store_item()+ if store_item is not None:+ plan.items.append(store_item)plan.items.extend(_global_items())plan.advisories.extend(_advisories(repo_path))return plandiff --git a/tests/unit/cli/test_uninstall_cmd.py b/tests/unit/cli/test_uninstall_cmd.pyindex 9aca8f0d7..42d8ca899 100644--- a/tests/unit/cli/test_uninstall_cmd.py+++ b/tests/unit/cli/test_uninstall_cmd.py@@ -38,6 +38,9 @@ def repo(tmp_path_factory, monkeypatch) -> Path:monkeypatch.setenv("HOMEPATH", str(home)[len(home.drive) :])monkeypatch.setattr(Path, "home", lambda: home)monkeypatch.delenv("REPOWISE_SKIP_EDITOR_SETUP", raising=False)+ # A developer's own database must not change what these tests see.+ monkeypatch.delenv("REPOWISE_DB_URL", raising=False)+ monkeypatch.delenv("REPOWISE_DATABASE_URL", raising=False)(repo_path / ".repowise").mkdir()(repo_path / ".repowise" / "wiki.db").write_text("not really a database", encoding="utf-8")@@ -570,6 +573,64 @@ def test_the_package_and_the_plugin_are_named_rather_than_touched(repo: Path) ->assert "print-config" in advisories+def _store_rows(items: list[dict]) -> list[dict]:+ return [i for i in items if i["label"].startswith("index in the configured")]+++def test_a_configured_database_is_listed_as_kept_with_a_reason(repo: Path, monkeypatch) -> None:+ import shutil++ shutil.rmtree(repo / ".repowise")+ monkeypatch.setenv("REPOWISE_DB_URL", "postgresql://alice:[email protected]:5432/wiki")++ result = _invoke(["uninstall", str(repo), "--all", "--dry-run", "--format", "json"])+ payload = json.loads(result.output)++ rows = _store_rows(payload["plan"]["items"])+ assert len(rows) == 1+ assert rows[0]["group"] == "index"+ assert "db.example:5432/wiki" in rows[0]["path"]+ assert "shared" in rows[0]["blocked"] and "other repositories" in rows[0]["blocked"]+ assert "s3cret" not in result.output+ assert "alice" not in result.output+++def test_a_real_run_keeps_the_database_and_exits_with_leftovers(+ repo: Path, tmp_path: Path, monkeypatch+) -> None:+ db_file = tmp_path / "shared.db"+ db_file.write_bytes(b"other repositories live here")+ monkeypatch.setenv("REPOWISE_DB_URL", f"sqlite:///{db_file}")++ result = _invoke(["uninstall", str(repo), "--all", "--format", "json"])+ payload = json.loads(result.output)++ kept = [r for r in payload["results"] if r["label"].startswith("index in the configured")]+ assert len(kept) == 1+ assert kept[0]["action"] == "kept"+ assert kept[0]["reason"]+ assert result.exit_code == 3+ assert db_file.read_bytes() == b"other repositories live here"+ # The local directory is still removed: only the database is declined.+ assert not (repo / ".repowise").exists()+++def test_uninstall_offers_no_flag_to_remove_the_database() -> None:+ help_text = _invoke(["uninstall", "--help"]).output.lower()+ assert "database" not in help_text+ assert "--db" not in help_text+++def test_no_configured_database_means_no_store_row(repo: Path) -> None:+ payload = _payload(["uninstall", str(repo), "--all", "--dry-run"])++ assert _store_rows(payload["plan"]["items"]) == []+ index_rows = [i for i in payload["plan"]["items"] if i["group"] == "index"]+ assert len(index_rows) == 1+ assert index_rows[0]["exists"] is True+ assert index_rows[0]["blocked"] is None++# ---------------------------------------------------------------------------# the shared file# ---------------------------------------------------------------------------