fix: bound schema setup with lock timeouts
This commit is contained in:
parent
248177dedd
commit
e8007fcb63
@ -1,6 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
import logging
|
||||||
from typing import Any, Iterable
|
from typing import Any, Iterable
|
||||||
|
|
||||||
import psycopg
|
import psycopg
|
||||||
@ -8,6 +9,8 @@ from psycopg_pool import ConnectionPool
|
|||||||
|
|
||||||
from .schema import ARIADNE_ACCESS_REQUEST_ALTER, ARIADNE_TABLES_SQL
|
from .schema import ARIADNE_ACCESS_REQUEST_ALTER, ARIADNE_TABLES_SQL
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Database:
|
class Database:
|
||||||
def __init__(self, dsn: str, pool_size: int = 5) -> None:
|
def __init__(self, dsn: str, pool_size: int = 5) -> None:
|
||||||
@ -21,12 +24,25 @@ class Database:
|
|||||||
conn.row_factory = psycopg.rows.dict_row
|
conn.row_factory = psycopg.rows.dict_row
|
||||||
yield conn
|
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:
|
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:
|
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:
|
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:
|
def fetchone(self, query: str, params: Iterable[Any] | None = None) -> dict[str, Any] | None:
|
||||||
with self.connection() as conn:
|
with self.connection() as conn:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user