"""DB helpers for MuseHub test fixtures. Provides insert_commit and upsert_snapshot for populating the muse_commits table in tests. """ import logging from muse.core.types import short_id from sqlalchemy.ext.asyncio import AsyncSession from musehub.db.muse_cli_models import MuseCliCommit, MuseCliSnapshot from musehub.types.json_types import StrDict logger = logging.getLogger(__name__) async def upsert_snapshot( session: AsyncSession, manifest: StrDict, snapshot_id: str ) -> MuseCliSnapshot: """Insert a MuseCliSnapshot row, ignoring duplicates.""" existing = await session.get(MuseCliSnapshot, snapshot_id) if existing is not None: logger.debug("⚠️ Snapshot %s already exists — skipped", short_id(snapshot_id)) return existing snap = MuseCliSnapshot(snapshot_id=snapshot_id, manifest=manifest) session.add(snap) logger.debug("✅ New snapshot %s (%d files)", short_id(snapshot_id), len(manifest)) return snap async def insert_commit(session: AsyncSession, commit: MuseCliCommit) -> None: """Insert a new MuseCliCommit row.""" session.add(commit) logger.debug("✅ New commit %s branch=%r", short_id(commit.commit_id), commit.branch)