chore: release v1.0.0 — каркас FastAPI, ingress Grafana, интеграции, документация
Made-with: Cursor
This commit is contained in:
32
onguard24/db.py
Normal file
32
onguard24/db.py
Normal file
@ -0,0 +1,32 @@
|
||||
import asyncpg
|
||||
|
||||
from onguard24.config import Settings
|
||||
|
||||
|
||||
def normalize_dsn(url: str) -> str:
|
||||
if url.startswith("postgres://"):
|
||||
return url.replace("postgres://", "postgresql://", 1)
|
||||
return url
|
||||
|
||||
|
||||
async def create_pool(settings: Settings) -> asyncpg.Pool | None:
|
||||
if not settings.database_url.strip():
|
||||
return None
|
||||
dsn = normalize_dsn(settings.database_url.strip())
|
||||
return await asyncpg.create_pool(dsn=dsn, min_size=1, max_size=10)
|
||||
|
||||
|
||||
MIGRATION_001 = """
|
||||
CREATE TABLE IF NOT EXISTS ingress_events (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
source text NOT NULL,
|
||||
received_at timestamptz NOT NULL DEFAULT now(),
|
||||
body jsonb NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS ingress_events_received_at_idx ON ingress_events (received_at DESC);
|
||||
"""
|
||||
|
||||
|
||||
async def migrate(pool: asyncpg.Pool) -> None:
|
||||
async with pool.acquire() as conn:
|
||||
await conn.execute(MIGRATION_001)
|
||||
Reference in New Issue
Block a user