Refactor code structure for improved readability and maintainability

This commit is contained in:
JSC
2025-07-22 13:21:44 +02:00
parent 11796b1012
commit fefb7f7bf4
26 changed files with 1424 additions and 7 deletions

38
app/core/database.py Normal file
View File

@@ -0,0 +1,38 @@
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from sqlmodel import SQLModel
from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.config import settings
from app.models import ( # noqa: F401
plan,
playlist,
playlist_sound,
sound,
sound_played,
stream,
user,
user_oauth,
)
engine: AsyncEngine = create_async_engine(
settings.DATABASE_URL,
echo=settings.DATABASE_ECHO,
)
async def get_db() -> AsyncGenerator[AsyncSession, None]:
async with AsyncSession(engine) as session:
try:
yield session
except Exception:
await session.rollback()
raise
finally:
await session.close()
async def init_db() -> None:
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)