gabriel / musehub public
muse_cli_models.py python
115 lines 3.9 KB
Raw
sha256:bb2baaabdd19320bde50cb69d447fd1c1e571467df729be1a23b5e93064b046a feat(intel): standardize headers, gauge icon, velocity card… Sonnet 4.6 minor ⚠ breaking 142 days ago
1 """SQLAlchemy ORM models for the Muse CLI commit tables.
2
3 MuseHub reads from muse_commits / muse_snapshots / muse_tags / muse_objects
4 to display repository history.
5 """
6 from __future__ import annotations
7
8
9 from datetime import datetime, timezone
10 import uuid
11
12 from sqlalchemy import DateTime, ForeignKey, Integer, String, Text
13 from sqlalchemy.orm import Mapped, mapped_column
14 from sqlalchemy.types import JSON
15
16 from muse.core.types import short_id
17 from musehub.db.database import Base
18 from musehub.types.json_types import JSONObject, JSONValue # JSONValue needed for ForwardRef resolution in Mapped[]
19
20
21 def _utc_now() -> datetime:
22 return datetime.now(timezone.utc)
23
24
25 class MuseCliObject(Base):
26 """A content-addressed blob: sha256(file_bytes) → bytes on disk."""
27
28 __tablename__ = "muse_objects"
29
30 object_id: Mapped[str] = mapped_column(String(128), primary_key=True)
31 size_bytes: Mapped[int] = mapped_column(Integer, nullable=False)
32 created_at: Mapped[datetime] = mapped_column(
33 DateTime(timezone=True), nullable=False, default=_utc_now
34 )
35
36 def __repr__(self) -> str:
37 return f"<MuseCliObject {short_id(self.object_id)} size={self.size_bytes}>"
38
39
40 class MuseCliSnapshot(Base):
41 """An immutable snapshot manifest: sha256(sorted(path:object_id pairs))."""
42
43 __tablename__ = "muse_snapshots"
44
45 snapshot_id: Mapped[str] = mapped_column(String(128), primary_key=True)
46 manifest: Mapped[dict[str, str]] = mapped_column(JSON, nullable=False)
47 created_at: Mapped[datetime] = mapped_column(
48 DateTime(timezone=True), nullable=False, default=_utc_now
49 )
50
51 def __repr__(self) -> str:
52 files = len(self.manifest) if self.manifest else 0
53 return f"<MuseCliSnapshot {short_id(self.snapshot_id)} files={files}>"
54
55
56 class MuseCliCommit(Base):
57 """A versioned commit record pointing to a snapshot and its parent."""
58
59 __tablename__ = "muse_commits"
60
61 commit_id: Mapped[str] = mapped_column(String(128), primary_key=True)
62 repo_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
63 branch: Mapped[str] = mapped_column(String(255), nullable=False)
64 parent_commit_id: Mapped[str | None] = mapped_column(
65 String(128), nullable=True, index=True
66 )
67 parent2_commit_id: Mapped[str | None] = mapped_column(
68 String(128), nullable=True, index=True
69 )
70 snapshot_id: Mapped[str] = mapped_column(
71 String(128),
72 ForeignKey("muse_snapshots.snapshot_id", ondelete="RESTRICT"),
73 nullable=False,
74 )
75 message: Mapped[str] = mapped_column(Text, nullable=False)
76 author: Mapped[str] = mapped_column(String(255), nullable=False, default="")
77 committed_at: Mapped[datetime] = mapped_column(
78 DateTime(timezone=True), nullable=False
79 )
80 created_at: Mapped[datetime] = mapped_column(
81 DateTime(timezone=True), nullable=False, default=_utc_now
82 )
83 commit_metadata: Mapped[JSONObject | None] = mapped_column(
84 JSON, nullable=True, default=None
85 )
86
87 def __repr__(self) -> str:
88 return (
89 f"<MuseCliCommit {short_id(self.commit_id)} branch={self.branch!r}"
90 f" msg={self.message[:30]!r}>"
91 )
92
93
94 class MuseCliTag(Base):
95 """A music-semantic tag attached to a Muse CLI commit."""
96
97 __tablename__ = "muse_tags"
98
99 tag_id: Mapped[str] = mapped_column(
100 String(36), primary_key=True, default=lambda: str(uuid.uuid4())
101 )
102 repo_id: Mapped[str] = mapped_column(String(128), nullable=False, index=True)
103 commit_id: Mapped[str] = mapped_column(
104 String(128),
105 ForeignKey("muse_commits.commit_id", ondelete="CASCADE"),
106 nullable=False,
107 index=True,
108 )
109 tag: Mapped[str] = mapped_column(Text, nullable=False, index=True)
110 created_at: Mapped[datetime] = mapped_column(
111 DateTime(timezone=True), nullable=False, default=_utc_now
112 )
113
114 def __repr__(self) -> str:
115 return f"<MuseCliTag {self.tag!r} commit={short_id(self.commit_id)}>"
File History 1 commit
sha256:bb2baaabdd19320bde50cb69d447fd1c1e571467df729be1a23b5e93064b046a feat(intel): standardize headers, gauge icon, velocity card… Sonnet 4.6 minor 142 days ago