/
tinypot
/
integr
Обзор
Документация
Войти
/
tinypot
/
integr
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task_management_api/app/core/database.py
47 строк
1 KB
Tim Polus
lab1
21 янв 2026, 23:15
21 янв 2026, 23:15
dd1578c
Код
Авторство
О чём код?
""" Database configuration and session management """ from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from app.core.config import settings # Create async engine engine = create_async_engine( settings.DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://"), echo=False, # Set to True for SQL query logging future=True ) # Create async session async_session = sessionmaker( engine, class_=AsyncSession, expire_on_commit=False ) # Base class for models Base = declarative_base() async def get_db() -> AsyncSession: """Dependency to get database session""" async with async_session() as session: try: yield session finally: await session.close() async def create_tables(): """Create all database tables""" async with engine.begin() as conn: await conn.run_sync(Base.metadata.create_all) async def drop_tables(): """Drop all database tables""" async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all)