import os from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy.orm import sessionmaker from sqlmodel import SQLModel DATABASE_URL = ( f'postgresql+asyncpg://{os.environ.get("POSTGRES_USER")}:' + f'{os.environ.get("POSTGRES_PASSWORD")}@' + os.environ.get("POSTGRES_CONTAINER") + f':5432/{os.environ.get("POSTGRES_DB")}' ) engine = create_async_engine(DATABASE_URL, echo=True, future=True) async def get_session() -> AsyncSession: async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async with async_session() as session: yield session async def create_db_and_tables(): async with engine.begin() as conn: await conn.run_sync(SQLModel.metadata.drop_all) await conn.run_sync(SQLModel.metadata.create_all)