diff options
| author | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-21 13:32:24 +0300 |
|---|---|---|
| committer | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-21 13:32:24 +0300 |
| commit | f1842be3bfabe7850d33662da2da377676144c48 (patch) | |
| tree | 95e1f5d6a72a2fc99847f0331907139b6b750dcb /app/utils/db.py | |
| parent | 70b0706973d9d856ca9f136df23a6fbec0901aea (diff) | |
uv migration
Diffstat (limited to 'app/utils/db.py')
| -rw-r--r-- | app/utils/db.py | 38 |
1 files changed, 21 insertions, 17 deletions
diff --git a/app/utils/db.py b/app/utils/db.py index 4daca74..5531998 100644 --- a/app/utils/db.py +++ b/app/utils/db.py @@ -1,34 +1,38 @@ -import asyncio import os +from typing import AsyncGenerator from dotenv import load_dotenv -from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine -from sqlalchemy.orm import DeclarativeBase, sessionmaker +from sqlalchemy.ext.asyncio import ( + AsyncEngine, + AsyncSession, + async_sessionmaker, + create_async_engine, +) +from sqlalchemy.orm import DeclarativeBase load_dotenv() + DATABASE_URL = os.getenv("DATABASE_URL") if not DATABASE_URL: raise ValueError("DATABASE_URL not found in .env") -engine = create_async_engine(DATABASE_URL, echo=True, future=True) -async_session = sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False) + +engine: AsyncEngine = create_async_engine( + DATABASE_URL, + echo=True, + pool_pre_ping=True, +) + +async_session = async_sessionmaker( + bind=engine, + expire_on_commit=False, +) class Base(DeclarativeBase): pass -async def get_async_session() -> AsyncSession: +async def get_async_session() -> AsyncGenerator[AsyncSession, None]: async with async_session() as session: yield session - - -async def init_db(): - - async with engine.begin() as conn: - await conn.run_sync(Base.metadata.create_all) - - -if __name__ == "__main__": - asyncio.run(init_db()) - print("All tables created!") |
