Fix live servers sqlite read path

This commit is contained in:
devRaGonSa
2026-03-24 15:09:35 +01:00
parent cf27fdb1cf
commit ab485b273b
2 changed files with 25 additions and 8 deletions

View File

@@ -754,7 +754,7 @@ def _should_refresh_snapshot(
def _try_collect_real_time_snapshot() -> tuple[list[dict[str, object]], list[dict[str, object]]]: 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") snapshots = payload.get("snapshots")
items = _select_primary_snapshot_items(_enrich_server_items(list(snapshots or []))) items = _select_primary_snapshot_items(_enrich_server_items(list(snapshots or [])))
errors = payload.get("errors") errors = payload.get("errors")

View File

@@ -18,9 +18,14 @@ DEFAULT_GAME_SOURCE = {
SUMMARY_SNAPSHOT_LIMIT = 6 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: def initialize_storage(*, db_path: Path | None = None) -> Path:
"""Create the local database file and minimal schema when missing.""" """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) resolved_path.parent.mkdir(parents=True, exist_ok=True)
with _connect(resolved_path) as connection: 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]]: def list_latest_snapshots(*, db_path: Path | None = None) -> list[dict[str, object]]:
"""Return the latest persisted snapshot for each known server.""" """Return the latest persisted snapshot for each known server."""
resolved_path = initialize_storage(db_path=db_path) resolved_path = resolve_storage_path(db_path=db_path)
with _connect(resolved_path) as connection: if not resolved_path.exists():
return []
with _connect_readonly(resolved_path) as connection:
rows = connection.execute( rows = connection.execute(
""" """
SELECT SELECT
@@ -182,8 +189,10 @@ def list_snapshot_history(
limit: int = 20, limit: int = 20,
) -> list[dict[str, object]]: ) -> list[dict[str, object]]:
"""Return recent persisted snapshots across all servers.""" """Return recent persisted snapshots across all servers."""
resolved_path = initialize_storage(db_path=db_path) resolved_path = resolve_storage_path(db_path=db_path)
with _connect(resolved_path) as connection: if not resolved_path.exists():
return []
with _connect_readonly(resolved_path) as connection:
rows = connection.execute( rows = connection.execute(
""" """
SELECT SELECT
@@ -220,9 +229,11 @@ def list_server_history(
limit: int = 20, limit: int = 20,
) -> list[dict[str, object]]: ) -> list[dict[str, object]]:
"""Return recent history for one server by numeric id or external id.""" """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) 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( rows = connection.execute(
f""" f"""
SELECT SELECT
@@ -259,6 +270,12 @@ def _connect(db_path: Path) -> sqlite3.Connection:
return 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( def _upsert_game_source(
connection: sqlite3.Connection, connection: sqlite3.Connection,
game_source: Mapping[str, str], game_source: Mapping[str, str],