gabriel / musehub public
db.py python
35 lines 1.2 KB
Raw
sha256:7281683f5c41e5d88b6d8811fbdafebd3e01a0c9dcd90975cfcb444ba71e8e81 docs: add local source-of-truth for musehub#225, #226, #227… Sonnet 5 11 hours ago
1 """DB helpers for MuseHub test fixtures.
2
3 Provides insert_commit and upsert_snapshot for populating the
4 muse_commits table in tests.
5 """
6
7 import logging
8
9 from muse.core.types import short_id
10 from sqlalchemy.ext.asyncio import AsyncSession
11
12 from musehub.db.muse_cli_models import MuseCliCommit, MuseCliSnapshot
13 from musehub.types.json_types import StrDict
14
15 logger = logging.getLogger(__name__)
16
17
18 async def upsert_snapshot(
19 session: AsyncSession, manifest: StrDict, snapshot_id: str
20 ) -> MuseCliSnapshot:
21 """Insert a MuseCliSnapshot row, ignoring duplicates."""
22 existing = await session.get(MuseCliSnapshot, snapshot_id)
23 if existing is not None:
24 logger.debug("⚠️ Snapshot %s already exists — skipped", short_id(snapshot_id))
25 return existing
26 snap = MuseCliSnapshot(snapshot_id=snapshot_id, manifest=manifest)
27 session.add(snap)
28 logger.debug("✅ New snapshot %s (%d files)", short_id(snapshot_id), len(manifest))
29 return snap
30
31
32 async def insert_commit(session: AsyncSession, commit: MuseCliCommit) -> None:
33 """Insert a new MuseCliCommit row."""
34 session.add(commit)
35 logger.debug("✅ New commit %s branch=%r", short_id(commit.commit_id), commit.branch)
File History 1 commit
sha256:7281683f5c41e5d88b6d8811fbdafebd3e01a0c9dcd90975cfcb444ba71e8e81 docs: add local source-of-truth for musehub#225, #226, #227… Sonnet 5 11 hours ago