db.py
python
sha256:c2e5cf2d754ed2fe9a94e879d41ad12ff221d2ac4ac7b45247fcb981c76858bf
docs: Section 8 database/migrations/backups — verified live…
Sonnet 5
24 days 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
2 commits
sha256:c2e5cf2d754ed2fe9a94e879d41ad12ff221d2ac4ac7b45247fcb981c76858bf
docs: Section 8 database/migrations/backups — verified live…
Sonnet 5
24 days ago
sha256:80e1a60a39562f6e616aaafbb27487f03abbec7d8ffc6164302b7a0c0bfc63ee
docs: check off Section 0 items verified in inventory doc, …
Sonnet 5
24 days ago