diff options
| author | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-04 00:03:04 +0300 |
|---|---|---|
| committer | l3wdfut4pwr <l3wdfut4pwr@gmail.com> | 2026-04-04 00:03:04 +0300 |
| commit | 63e87a3ed84ee9e9e4a4ff329a40d2b7ce5e5d0d (patch) | |
| tree | c24731c200df3b5854a5abc14f7a5481a33d838a /app/utils/db.py | |
| parent | d835d79eb24c730ec8148415113e846a01cefd19 (diff) | |
add profile
Diffstat (limited to 'app/utils/db.py')
| -rw-r--r-- | app/utils/db.py | 20 |
1 files changed, 15 insertions, 5 deletions
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!") |
