Skip to content

fix(workspace): reconcile listed repos with the configured (postgres) DB - #1853

Open
sloemo01 wants to merge 2 commits into
repowise-dev:mainfrom
sloemo01:fix/workspace-db-check
Open

fix(workspace): reconcile listed repos with the configured (postgres) DB#1853
sloemo01 wants to merge 2 commits into
repowise-dev:mainfrom
sloemo01:fix/workspace-db-check

Conversation

@sloemo01

Copy link
Copy Markdown
Contributor

Summary

Fixes #1034.

When the server is configured with a PostgreSQL database (REPOWISE_DB_URL /
REPOWISE_DATABASE_URL), GET /api/workspace and GET /api/workspace/graph
listed repos by walking the root directory and reading a repo-local
.repowise/wiki.db SQLite file. Repos indexed into the configured (postgres)
DB keyed by local_path have no local wiki.db, so they were reported as
needs_index/missing_dir with zero stats — even though they were already
indexed.

Change

_query_repo_stats and _query_top_language in
packages/server/src/repowise/server/routers/workspace.py now:

  • prefer the repo-local SQLite wiki.db when present (unchanged behaviour),
  • otherwise fall back to querying the configured session factory
    (app.state.session_factory) keyed by the repo's absolute local_path,
    aggregating the same tables (repositories, graph_nodes, wiki_pages,
    git_metadata, health_file_metrics) the SQLite path reads.

The helpers became async; both callers (/api/workspace, /api/workspace/graph)
were updated to await them and pass the session factory.

Tests

  • Added test_configured_db_repo_reported_as_indexed covering a repo indexed
    in the configured DB (no local wiki.db) being reported as indexed with
    its repo_id.
  • Updated the existing TestQueryRepoStats unit tests for the now-async
    helpers.

@repowise-bot

repowise-bot Bot commented Aug 22, 2026

Copy link
Copy Markdown

⚠️ Health of changed files: 3.7 → 3.5 (-0.1)
⚠️ Change risk: moderate, riskier than 51% of this repo's commits.

📋 At a glance
2 hotspots touched · 3 new findings introduced · 2 files with recent fix history.

Files & modules (2)
  • packages (1 file)
    • .../routers/workspace.py
  • tests (1 file)
    • .../server/test_workspace_router.py

✅ Health gate: passed

📌 Before you merge

  • Run .../server/test_workspace_sidebar.py: they import the changed files
🔎 More signals (2)

🗺️ Change map

flowchart LR
  subgraph PR ["Changed in this PR (2 with dependents)"]
    f_packages_server_src_repowise_server_routers_workspace_py[".../routers/workspace.py 🔥"]:::changed
    f_tests_unit_server_test_workspace_router_py[".../server/test_workspace_router.py"]:::changed
  end
  f_packages_cli_src_repowise_cli_commands_workspace_cmd_py[".../commands/workspace_cmd.py"]
  f_packages_server_src_repowise_server_routers_workspace_py --> f_packages_cli_src_repowise_cli_commands_workspace_cmd_py
  f_packages_cli_src_repowise_cli_helpers_py[".../cli/helpers.py"]
  f_packages_server_src_repowise_server_routers_workspace_py --> f_packages_cli_src_repowise_cli_helpers_py
  f_packages_core_src_repowise_core_workspace_config_py[".../workspace/config.py"]
  f_packages_server_src_repowise_server_routers_workspace_py --> f_packages_core_src_repowise_core_workspace_config_py
  f_packages_core_src_repowise_core_workspace_contracts_py[".../workspace/contracts.py"]
  f_packages_server_src_repowise_server_routers_workspace_py --> f_packages_core_src_repowise_core_workspace_contracts_py
  f_packages_server_src_repowise_server_schemas_workspace_py[".../schemas/workspace.py"]
  f_tests_unit_server_test_workspace_router_py --> f_packages_server_src_repowise_server_schemas_workspace_py
  more(["+3 more dependents"])
  PR --> more
  t_tests_unit_server_test_workspace_sidebar_py(["✅ .../server/test_workspace_sidebar.py"]):::guard
  t_tests_unit_server_test_workspace_sidebar_py -.-> f_packages_server_src_repowise_server_routers_workspace_py
  classDef changed fill:#dbeafe,stroke:#1d4ed8,color:#1e3a5f
  classDef warn fill:#fef3c7,stroke:#b45309,color:#78350f
  classDef guard fill:#dcfce7,stroke:#15803d,color:#14532d
Loading

Solid arrows: code that imports the changed files (8 direct dependents, from the last indexed snapshot). Dashed: history/tests.

🔥 Hotspots touched (2)

  • .../routers/workspace.py: 15 commits/90d, 9 dependents · primary owner: Raghav Chamadiya (83%)
  • .../server/test_workspace_router.py: 9 commits/90d, 1 dependents · primary owner: Raghav Chamadiya (85%)

👀 Suggested reviewers @RaghavChamadiya


📊 See the full report for this PR
Your repo map with this PR's blast radius lit up, every caller of the contracts it changes, and health before and after. No sign-in. · ⭐ Star Repowise · 📥 Install bot · Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot · Updated 2026-08-22 16:53 UTC (since the last push: 3 new findings)

@akshatmalik-bruh

Copy link
Copy Markdown
Contributor

@sloemo01
Great work on this fix! Tested the branch locally and verified that all tests pass (test_workspace_router.py - 45/45 passed). The Postgres fallback in _query_repo_stats_from_db correctly resolves GET /api/workspace and GET /api/workspace/graph for repos without a local .repowise/wiki.db.

Just a couple of small observations that might be helpful before maintainers review:

  1. Session Architecture vs Dual Path: On issue [Bug] GET /api/workspace lists repos from the current directory, but doesn't check if they've already been indexed in the database when using postgresql #1034, @RaghavChamadiya mentioned preferring routing queries through the shared session factory (resolve_session_factory / get_session) for all backends rather than maintaining separate raw sqlite3.connect() and Postgres paths. Consolidating to SQLAlchemy sessions across both SQLite and Postgres could make the code cleaner and prevent logic drift between the two helpers.

  2. POST /api/workspace/sync: It looks like sync_workspace in workspace.py still checks if not db_path.exists() (looking for .repowise/wiki.db). You might want to update that endpoint as well so that triggering a sync on a Postgres-indexed repo doesn't attempt a redundant first-time re-index.

I may be wrong but you can verify this once !

…gres) DB on sync

sync_workspace gated first-time indexing on whether a local
.repoweise/wiki.db file exists. Under PostgreSQL repos are indexed into the
shared DB with no per-repo wiki.db, so every sync wrongly re-indexed them.
Check the configured session factory for an existing Repository row instead
of the file presence (issue repowise-dev#1034).
@sloemo01

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review, @akshatmalik-bruh — both points are good.

On the POST /api/workspace/sync one: you were right, it was a real bug. It gated first-time indexing on db_path.exists() (looking for a local .repowise/wiki.db), so a Postgres-indexed repo (no per-repo wiki.db) got re-indexed on every sync. I've just pushed a fix to this branch: sync_workspace now checks the configured session factory for an existing Repository row for the resolved path before deciding to index, falling back to the file check for the SQLite/self-hosted layout. 45/45 workspace-router tests still pass.

On the session-architecture consolidation point: agreed it would be cleaner and prevent drift between the sqlite3 and Postgres helpers. That is a larger refactor than this PR should carry, so I have left it out of scope here — but it is worth doing as its own change. Happy to take it on as a follow-up if you want it.

@akshatmalik-bruh

Copy link
Copy Markdown
Contributor

@RaghavChamadiya take a look at this once !

@Ayush7614 Ayush7614 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: pytest tests/unit/server/test_workspace_router.py -q 45/45 passed including new test_configured_db_repo_reported_as_indexed (Postgres fallback without local wiki.db). Fixes #1034_query_repo_stats and _query_top_language now prefer repo-local SQLite then fall back to session_factory keyed by local_path, and sync_workspace checks the DB row before re-indexing (addressing @akshatmalik-bruh's point).\n\nThank you @akshatmalik-bruh for the thorough review — both observations were spot-on (session-factory consolidation vs dual path, and the POST /api/workspace/sync db_path.exists bug). @sloemo01 addressed the second point with a fix that keeps 45/45 green while leaving the larger consolidation as a clean follow-up as you both agreed. Great collaboration.\n\nLGTM — approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] GET /api/workspace lists repos from the current directory, but doesn't check if they've already been indexed in the database when using postgresql

3 participants