db_kyc_project/batcher/app/src/db/pg/pg.py
Michail Kostochka b2fb2f9380 Various fixes
- cleaned up Dockefiles
- added healthchecks to docker-compose files
- moved celery & celery-beat to one container
- cleaned up nginx config
2024-12-16 19:13:27 +03:00

34 lines
1010 B
Python

import asyncio
from app.src.config import PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DB
from pathlib import Path
from starlette.requests import Request
import asyncpg
import logging
from asyncpg_trek import plan, execute, Direction
from asyncpg_trek.asyncpg import AsyncpgBackend
DB_URL = f'postgresql://{PG_USER}:{str(PG_PASSWORD)}@{PG_HOST}:{PG_PORT}/{PG_DB}'
MIGRATIONS_DIR = Path(__file__).parent.resolve() / "migrations"
logger = logging.getLogger("uvicorn")
async def connect_pg() -> asyncpg.Pool:
return await asyncpg.create_pool(DB_URL)
async def get_pg(request: Request) -> asyncpg.Connection:
async with request.app.state.pg_pool.acquire() as conn:
yield conn
async def migrate(
target_revision: str,
) -> None:
pool = await connect_pg()
async with pool.acquire() as conn:
backend = AsyncpgBackend(conn)
planned = await plan(backend, MIGRATIONS_DIR, target_revision=target_revision, direction=Direction.up)
await execute(backend, planned)