fix(analytics): add _conn module-level override for test patching
Some checks failed
CI / test-cli (push) Successful in 11s
CI / test-sqlmesh (push) Successful in 12s
CI / test-web (push) Failing after 12s
CI / tag (push) Has been skipped

Tests monkeypatch analytics._conn to inject a temp DuckDB connection.
The attribute didn't exist; fetch_analytics now uses it when set,
bypassing the _db_path / threading.local path.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-28 02:00:11 +01:00
parent cddcd4463e
commit 8d1dbace0f

View File

@@ -65,6 +65,7 @@ ALLOWED_METRICS = frozenset({
_local = threading.local() _local = threading.local()
_db_path: str = "" _db_path: str = ""
_conn: duckdb.DuckDBPyConnection | None = None # test override: set to bypass _db_path / _local
def open_analytics_db() -> None: def open_analytics_db() -> None:
@@ -110,12 +111,15 @@ async def fetch_analytics(sql: str, params: list | None = None) -> list[dict]:
"""Run a read-only DuckDB query off the event loop. Returns list of dicts. """Run a read-only DuckDB query off the event loop. Returns list of dicts.
Returns empty list if analytics DB is not configured (SERVING_DUCKDB_PATH unset Returns empty list if analytics DB is not configured (SERVING_DUCKDB_PATH unset
or file missing at startup) — dashboard routes degrade gracefully. or file missing at startup) — dashboard routes degrade gracefully.
If the module-level _conn is set (test override), it is used directly in place
of the per-thread _get_conn() path.
""" """
if not _db_path: if _conn is None and not _db_path:
return [] return []
def _query(): def _query():
conn = _get_conn() conn = _conn if _conn is not None else _get_conn()
cursor = conn.cursor() cursor = conn.cursor()
result = cursor.execute(sql, params or []) result = cursor.execute(sql, params or [])
columns = [desc[0] for desc in result.description] columns = [desc[0] for desc in result.description]