33 lines
719 B
Python
33 lines
719 B
Python
import asyncpg
|
|
import os
|
|
from tenacity import retry, stop_after_attempt, wait_fixed
|
|
|
|
pool: asyncpg.Pool = None
|
|
|
|
@retry(stop=stop_after_attempt(5), wait=wait_fixed(3))
|
|
async def init_db():
|
|
global pool
|
|
|
|
url = os.getenv("DATABASE_URL")
|
|
if not url:
|
|
raise ValueError("DATABASE_URL nicht gesetzt!")
|
|
|
|
pool = await asyncpg.create_pool(
|
|
dsn=url,
|
|
min_size=2,
|
|
max_size=10
|
|
)
|
|
async with pool.acquire() as conn:
|
|
await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
|
|
|
|
print(f"✅ Datenbank verbunden")
|
|
|
|
async def close_db():
|
|
global pool
|
|
if pool:
|
|
await pool.close()
|
|
|
|
async def get_db():
|
|
async with pool.acquire() as conn:
|
|
yield conn
|