forked from mtusi2026/mtusi2026lab2
25 lines
711 B
Python
25 lines
711 B
Python
import os
|
|
import asyncpg
|
|
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql://messenger:secret_password@postgres:5432/messenger_db")
|
|
|
|
pool: asyncpg.Pool = None
|
|
|
|
async def init_db():
|
|
global pool
|
|
pool = await asyncpg.create_pool(DATABASE_URL, min_size=2, max_size=10)
|
|
|
|
migration_path = os.path.join(os.path.dirname(__file__), "migrations", "001_init_db.sql")
|
|
if os.path.exists(migration_path):
|
|
with open(migration_path, "r", encoding="utf-8") as f:
|
|
sql = f.read()
|
|
async with pool.acquire() as conn:
|
|
await conn.execute(sql)
|
|
|
|
async def close_db():
|
|
global pool
|
|
if pool:
|
|
await pool.close()
|
|
|
|
def get_pool() -> asyncpg.Pool:
|
|
return pool |