From ab485b273b32cc612d2148535fd8845cdd9f2161 Mon Sep 17 00:00:00 2001 From: devRaGonSa Date: Tue, 24 Mar 2026 15:09:35 +0100 Subject: [PATCH] Fix live servers sqlite read path --- backend/app/payloads.py | 2 +- backend/app/storage.py | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/backend/app/payloads.py b/backend/app/payloads.py index 2622e5d..e87111f 100644 --- a/backend/app/payloads.py +++ b/backend/app/payloads.py @@ -754,7 +754,7 @@ def _should_refresh_snapshot( def _try_collect_real_time_snapshot() -> tuple[list[dict[str, object]], list[dict[str, object]]]: - payload = get_live_data_source().collect_snapshots(persist=True) + payload = get_live_data_source().collect_snapshots(persist=False) snapshots = payload.get("snapshots") items = _select_primary_snapshot_items(_enrich_server_items(list(snapshots or []))) errors = payload.get("errors") diff --git a/backend/app/storage.py b/backend/app/storage.py index a78b710..a91c7fe 100644 --- a/backend/app/storage.py +++ b/backend/app/storage.py @@ -18,9 +18,14 @@ DEFAULT_GAME_SOURCE = { SUMMARY_SNAPSHOT_LIMIT = 6 +def resolve_storage_path(*, db_path: Path | None = None) -> Path: + """Resolve the SQLite path used by live snapshot persistence.""" + return db_path or get_storage_path() + + def initialize_storage(*, db_path: Path | None = None) -> Path: """Create the local database file and minimal schema when missing.""" - resolved_path = db_path or get_storage_path() + resolved_path = resolve_storage_path(db_path=db_path) resolved_path.parent.mkdir(parents=True, exist_ok=True) with _connect(resolved_path) as connection: @@ -139,8 +144,10 @@ def persist_snapshot_batch( def list_latest_snapshots(*, db_path: Path | None = None) -> list[dict[str, object]]: """Return the latest persisted snapshot for each known server.""" - resolved_path = initialize_storage(db_path=db_path) - with _connect(resolved_path) as connection: + resolved_path = resolve_storage_path(db_path=db_path) + if not resolved_path.exists(): + return [] + with _connect_readonly(resolved_path) as connection: rows = connection.execute( """ SELECT @@ -182,8 +189,10 @@ def list_snapshot_history( limit: int = 20, ) -> list[dict[str, object]]: """Return recent persisted snapshots across all servers.""" - resolved_path = initialize_storage(db_path=db_path) - with _connect(resolved_path) as connection: + resolved_path = resolve_storage_path(db_path=db_path) + if not resolved_path.exists(): + return [] + with _connect_readonly(resolved_path) as connection: rows = connection.execute( """ SELECT @@ -220,9 +229,11 @@ def list_server_history( limit: int = 20, ) -> list[dict[str, object]]: """Return recent history for one server by numeric id or external id.""" - resolved_path = initialize_storage(db_path=db_path) + resolved_path = resolve_storage_path(db_path=db_path) + if not resolved_path.exists(): + return [] server_filter, server_value = _build_server_filter(server_id) - with _connect(resolved_path) as connection: + with _connect_readonly(resolved_path) as connection: rows = connection.execute( f""" SELECT @@ -259,6 +270,12 @@ def _connect(db_path: Path) -> sqlite3.Connection: return connection +def _connect_readonly(db_path: Path) -> sqlite3.Connection: + connection = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True) + connection.row_factory = sqlite3.Row + return connection + + def _upsert_game_source( connection: sqlite3.Connection, game_source: Mapping[str, str],