gabriel / musehub public
0058_commits_snapshots_global.py python
103 lines 5.1 KB
Raw
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor ⚠ breaking 1 day ago
1 """Make commits and snapshots globally content-addressed — remove repo_id.
2
3 Commits and snapshots are content-addressed objects identified by their SHA-256
4 hash alone. They do not belong to any repo. Repo membership is tracked in
5 separate ref tables (musehub_commit_refs, musehub_snapshot_refs), exactly
6 mirroring how musehub_object_refs tracks object membership.
7
8 Changes:
9 - Drop repo_id FK + composite indexes from musehub_commits
10 - Drop repo_id FK + index from musehub_snapshots
11 - Create musehub_commit_refs (repo_id, commit_id)
12 - Create musehub_snapshot_refs (repo_id, snapshot_id)
13 - Backfill both ref tables from existing rows
14 """
15
16 from __future__ import annotations
17
18 import sqlalchemy as sa
19 from alembic import op
20
21 revision = "0058"
22 down_revision = "0057"
23 branch_labels = None
24 depends_on = None
25
26
27 def upgrade() -> None:
28 # ── 1. Create musehub_commit_refs ────────────────────────────────────────
29 op.create_table(
30 "musehub_commit_refs",
31 sa.Column("repo_id", sa.String(128), sa.ForeignKey("musehub_repos.repo_id", ondelete="CASCADE"), primary_key=True),
32 sa.Column("commit_id", sa.String(128), sa.ForeignKey("musehub_commits.commit_id", ondelete="CASCADE"), primary_key=True),
33 sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
34 )
35 op.create_index("ix_musehub_commit_refs_repo_id", "musehub_commit_refs", ["repo_id"])
36
37 # ── 2. Create musehub_snapshot_refs ──────────────────────────────────────
38 op.create_table(
39 "musehub_snapshot_refs",
40 sa.Column("repo_id", sa.String(128), sa.ForeignKey("musehub_repos.repo_id", ondelete="CASCADE"), primary_key=True),
41 sa.Column("snapshot_id", sa.String(128), sa.ForeignKey("musehub_snapshots.snapshot_id", ondelete="CASCADE"), primary_key=True),
42 sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.func.now()),
43 )
44 op.create_index("ix_musehub_snapshot_refs_repo_id", "musehub_snapshot_refs", ["repo_id"])
45
46 # ── 3. Backfill commit refs from existing musehub_commits rows ────────────
47 op.execute(
48 """
49 INSERT INTO musehub_commit_refs (repo_id, commit_id, created_at)
50 SELECT repo_id, commit_id, now()
51 FROM musehub_commits
52 ON CONFLICT DO NOTHING
53 """
54 )
55
56 # ── 4. Backfill snapshot refs from existing musehub_snapshots rows ────────
57 op.execute(
58 """
59 INSERT INTO musehub_snapshot_refs (repo_id, snapshot_id, created_at)
60 SELECT repo_id, snapshot_id, now()
61 FROM musehub_snapshots
62 ON CONFLICT DO NOTHING
63 """
64 )
65
66 # ── 5. Drop composite indexes on musehub_commits ─────────────────────────
67 op.drop_index("ix_musehub_commits_repo_branch", table_name="musehub_commits")
68 op.drop_index("ix_musehub_commits_repo_timestamp", table_name="musehub_commits")
69
70 # ── 6. Drop repo_id FK + column from musehub_commits ─────────────────────
71 op.drop_constraint("musehub_commits_repo_id_fkey", "musehub_commits", type_="foreignkey")
72 op.drop_index("ix_musehub_commits_repo_id", table_name="musehub_commits")
73 op.drop_column("musehub_commits", "repo_id")
74
75 # ── 7. Drop repo_id FK + column from musehub_snapshots ───────────────────
76 op.drop_constraint("musehub_snapshots_repo_id_fkey", "musehub_snapshots", type_="foreignkey")
77 op.drop_index("ix_musehub_snapshots_repo_id", table_name="musehub_snapshots")
78 op.drop_column("musehub_snapshots", "repo_id")
79
80
81 def downgrade() -> None:
82 # Re-add repo_id to musehub_snapshots (data loss — refs table is lost)
83 op.add_column("musehub_snapshots", sa.Column("repo_id", sa.String(128), nullable=True))
84 op.create_foreign_key(
85 "musehub_snapshots_repo_id_fkey", "musehub_snapshots",
86 "musehub_repos", ["repo_id"], ["repo_id"], ondelete="CASCADE",
87 )
88 op.create_index("ix_musehub_snapshots_repo_id", "musehub_snapshots", ["repo_id"])
89
90 # Re-add repo_id to musehub_commits
91 op.add_column("musehub_commits", sa.Column("repo_id", sa.String(128), nullable=True))
92 op.create_foreign_key(
93 "musehub_commits_repo_id_fkey", "musehub_commits",
94 "musehub_repos", ["repo_id"], ["repo_id"], ondelete="CASCADE",
95 )
96 op.create_index("ix_musehub_commits_repo_id", "musehub_commits", ["repo_id"])
97 op.create_index("ix_musehub_commits_repo_branch", "musehub_commits", ["repo_id", "branch"])
98 op.create_index("ix_musehub_commits_repo_timestamp","musehub_commits", ["repo_id", "timestamp"])
99
100 op.drop_index("ix_musehub_snapshot_refs_repo_id", table_name="musehub_snapshot_refs")
101 op.drop_table("musehub_snapshot_refs")
102 op.drop_index("ix_musehub_commit_refs_repo_id", table_name="musehub_commit_refs")
103 op.drop_table("musehub_commit_refs")
File History 1 commit
sha256:3ff9c9863a9891bdcde71b4a43228f66d0493e38b7cc1d09fe9eb7de774046b2 feat: add repair-commit wire endpoint (API parity with repa… Opus 4.8 minor 1 day ago