From e8007fcb63e965e82a0dcbf9aa2fbff4a085c151 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Mon, 19 Jan 2026 22:58:26 -0300 Subject: [PATCH] fix: bound schema setup with lock timeouts --- ariadne/db/database.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ariadne/db/database.py b/ariadne/db/database.py index ee9e9ba..7c5d0d4 100644 --- a/ariadne/db/database.py +++ b/ariadne/db/database.py @@ -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: