From 63e87a3ed84ee9e9e4a4ff329a40d2b7ce5e5d0d Mon Sep 17 00:00:00 2001 From: l3wdfut4pwr Date: Sat, 4 Apr 2026 00:03:04 +0300 Subject: add profile --- app/utils/db.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'app/utils/db.py') diff --git a/app/utils/db.py b/app/utils/db.py index b823904..4daca74 100644 --- a/app/utils/db.py +++ b/app/utils/db.py @@ -1,3 +1,4 @@ +import asyncio import os from dotenv import load_dotenv @@ -5,20 +6,29 @@ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import DeclarativeBase, sessionmaker load_dotenv() - DATABASE_URL = os.getenv("DATABASE_URL") if not DATABASE_URL: - raise ValueError("DATABASE_URL not found .env") + 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) +class Base(DeclarativeBase): + pass + + async def get_async_session() -> AsyncSession: async with async_session() as session: yield session -class Base(DeclarativeBase): - pass +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!") -- cgit v1.3-3-g829e