fix: bound schema setup with lock timeouts

This commit is contained in:
Brad Stein 2026-01-19 22:58:26 -03:00
parent 248177dedd
commit e8007fcb63

View File

@ -1,6 +1,7 @@
from __future__ import annotations
from contextlib import contextmanager
import logging
from typing import Any, Iterable
import psycopg
@ -8,6 +9,8 @@ from psycopg_pool import ConnectionPool
from .schema import ARIADNE_ACCESS_REQUEST_ALTER, ARIADNE_TABLES_SQL
logger = logging.getLogger(__name__)
class Database:
def __init__(self, dsn: str, pool_size: int = 5) -> None:
@ -21,12 +24,25 @@ class Database:
conn.row_factory = psycopg.rows.dict_row
yield conn
def ensure_schema(self) -> None:
def ensure_schema(self, lock_timeout_sec: int = 5, statement_timeout_sec: int = 30) -> None:
with self.connection() as conn:
try:
conn.execute(f"SET lock_timeout = '{lock_timeout_sec}s'")
conn.execute(f"SET statement_timeout = '{statement_timeout_sec}s'")
except Exception:
pass
for stmt in ARIADNE_TABLES_SQL:
conn.execute(stmt)
try:
conn.execute(stmt)
except (psycopg.errors.LockNotAvailable, psycopg.errors.QueryCanceled) as exc:
logger.warning("schema ensure skipped due to lock timeout: %s", exc)
return
for stmt in ARIADNE_ACCESS_REQUEST_ALTER:
conn.execute(stmt)
try:
conn.execute(stmt)
except (psycopg.errors.LockNotAvailable, psycopg.errors.QueryCanceled) as exc:
logger.warning("schema ensure skipped due to lock timeout: %s", exc)
return
def fetchone(self, query: str, params: Iterable[Any] | None = None) -> dict[str, Any] | None:
with self.connection() as conn: