Add database seeding functionality and enhance model relationships

- Implement initial data seeding for plans in the database.
- Create a new `seed_all_data` function to manage seeding process.
- Update `Sound` and `User` models to include relationships for `SoundPlayed` and `Stream`.
This commit is contained in:
JSC
2025-07-25 11:44:47 +02:00
parent 4a77a23ee5
commit af20bc8724
4 changed files with 85 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ from sqlmodel.ext.asyncio.session import AsyncSession
from app.core.config import settings
from app.core.logging import get_logger
from app.core.seeds import seed_all_data
from app.models import ( # noqa: F401
plan,
playlist,
@@ -45,6 +46,19 @@ async def init_db() -> None:
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
logger.info("Database tables created successfully")
# Seed initial data
await seed_initial_data()
except Exception:
logger.exception("Failed to initialize database")
raise
async def seed_initial_data() -> None:
"""Seed initial data into the database."""
logger = get_logger(__name__)
logger.info("Starting initial data seeding")
async with AsyncSession(engine) as session:
await seed_all_data(session)