Spaces:
Sleeping
Sleeping
Download database/session.py from zoldyckff999oy/legacybot: direct link, hf CLI and curl.
- Browser
- Download file 1.08 kB
-
https://huggingface.co/spaces/zoldyckff999oy/legacybot/resolve/main/database/session.py
- Command line
-
hf download hf://spaces/zoldyckff999oy/legacybot/database/session.py
-
curl -L -o session.py https://huggingface.co/spaces/zoldyckff999oy/legacybot/resolve/main/database/session.py
1.08 kB
| """ | |
| Session helpers — replace the old `async with db.acquire()` pattern. | |
| """ | |
| from __future__ import annotations | |
| from contextlib import asynccontextmanager | |
| from typing import AsyncIterator | |
| from sqlalchemy.ext.asyncio import AsyncSession | |
| async def session_scope() -> AsyncIterator[AsyncSession]: | |
| """ | |
| Preferred context manager for business logic. | |
| """ | |
| # استيراد المصنع ديناميكياً داخل الدالة لتفادي تعارض الأسماء والحصول على القيمة المحدثة | |
| from database.engine import async_session_factory | |
| if async_session_factory is None: | |
| raise RuntimeError("Database not initialized. Call init_database() first.") | |
| session: AsyncSession = async_session_factory() | |
| try: | |
| yield session | |
| await session.commit() | |
| except Exception: | |
| await session.rollback() | |
| raise | |
| finally: | |
| await session.close() | |
| async def get_session() -> AsyncIterator[AsyncSession]: | |
| async with session_scope() as session: | |
| yield session | |