37 lines
969 B
Python
37 lines
969 B
Python
import os
|
|
import logging
|
|
import asyncpg
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_pool: asyncpg.Pool | None = None
|
|
|
|
|
|
async def _init_connection(conn: asyncpg.Connection) -> None:
|
|
await conn.execute("SET search_path TO willhaben_tracker")
|
|
|
|
|
|
async def get_pool() -> asyncpg.Pool:
|
|
global _pool
|
|
if _pool is None:
|
|
_pool = await asyncpg.create_pool(
|
|
host=os.getenv("POSTGRES_HOST", "db"),
|
|
port=int(os.getenv("POSTGRES_PORT", "5432")),
|
|
user=os.getenv("POSTGRES_USER", "postgres"),
|
|
password=os.getenv("POSTGRES_PASSWORD"),
|
|
database=os.getenv("POSTGRES_DB", "postgres"),
|
|
min_size=2,
|
|
max_size=10,
|
|
init=_init_connection
|
|
)
|
|
logger.info("Database pool initialized")
|
|
return _pool
|
|
|
|
|
|
async def close_pool() -> None:
|
|
global _pool
|
|
if _pool is not None:
|
|
await _pool.close()
|
|
logger.info("Database pool closed")
|
|
_pool = None
|