35 lines
851 B
Python
35 lines
851 B
Python
from __future__ import annotations
|
|
|
|
from contextlib import contextmanager
|
|
from typing import Iterator
|
|
|
|
import psycopg2
|
|
from psycopg2.extensions import connection as Connection
|
|
from psycopg2.extras import RealDictCursor
|
|
|
|
from .settings import get_settings
|
|
|
|
|
|
@contextmanager
|
|
def get_connection() -> Iterator[Connection]:
|
|
settings = get_settings()
|
|
connection = psycopg2.connect(
|
|
host=settings.pg_host,
|
|
port=settings.pg_port,
|
|
dbname=settings.pg_database,
|
|
user=settings.pg_user,
|
|
password=settings.pg_password,
|
|
connect_timeout=10,
|
|
application_name="governance_review_ui_v0314",
|
|
)
|
|
try:
|
|
yield connection
|
|
finally:
|
|
connection.close()
|
|
|
|
|
|
def dict_cursor(connection: Connection) -> RealDictCursor:
|
|
return connection.cursor(
|
|
cursor_factory=RealDictCursor
|
|
)
|