gabriel / musehub public
0010_coordination_bus.py python
79 lines 2.8 KB
Raw
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 170 days ago
1 """Add musehub_coord_records table for the coordination bus.
2
3 Agents on different machines push local coordination records (reservations,
4 intents, releases, heartbeats, dependencies, tasks, claims) to this table so
5 that distributed agent swarms can share state without filesystem access.
6
7 Key design choices:
8 - ``id`` is a monotonically increasing integer PK used as the SSE cursor —
9 more reliable than timestamps for ordering within the same millisecond.
10 - ``(repo_id, kind, record_uuid)`` unique constraint enforces write-once
11 semantics at the DB level (the service layer skips on IntegrityError).
12 - ``ix_coord_repo_id_cursor`` optimizes the primary pull query pattern:
13 "give me all records for repo X with id > Y in insertion order".
14 - ``ix_coord_repo_kind_id`` optimizes filtered pulls:
15 "give me all heartbeats for repo X with id > Y".
16 - ``ON DELETE CASCADE`` on the FK ensures coord records are removed when
17 their parent repo is deleted.
18
19 Revision ID: 0010
20 Revises: 0009
21 """
22
23 from __future__ import annotations
24
25 from alembic import op
26 import sqlalchemy as sa
27
28 revision: str = "0010"
29 down_revision: str = "0009"
30 branch_labels = None
31 depends_on = None
32
33
34 def upgrade() -> None:
35 op.create_table(
36 "musehub_coord_records",
37 sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
38 sa.Column("repo_id", sa.String(36), nullable=False),
39 sa.Column("kind", sa.String(32), nullable=False),
40 sa.Column("record_uuid", sa.String(36), nullable=False),
41 sa.Column("run_id", sa.String(255), nullable=False, server_default=""),
42 sa.Column("payload", sa.JSON(), nullable=False),
43 sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
44 sa.Column("expires_at", sa.DateTime(timezone=True), nullable=True),
45 sa.ForeignKeyConstraint(
46 ["repo_id"],
47 ["musehub_repos.repo_id"],
48 name="fk_coord_repo_id",
49 ondelete="CASCADE",
50 ),
51 sa.PrimaryKeyConstraint("id"),
52 sa.UniqueConstraint(
53 "repo_id", "kind", "record_uuid",
54 name="uq_coord_repo_kind_uuid",
55 ),
56 )
57 op.create_index(
58 "ix_coord_repo_id_cursor",
59 "musehub_coord_records",
60 ["repo_id", "id"],
61 )
62 op.create_index(
63 "ix_coord_repo_kind_id",
64 "musehub_coord_records",
65 ["repo_id", "kind", "id"],
66 )
67 # Index on repo_id alone for fast repo-scoped queries without kind filter.
68 op.create_index(
69 "ix_coord_records_repo_id",
70 "musehub_coord_records",
71 ["repo_id"],
72 )
73
74
75 def downgrade() -> None:
76 op.drop_index("ix_coord_records_repo_id", table_name="musehub_coord_records")
77 op.drop_index("ix_coord_repo_kind_id", table_name="musehub_coord_records")
78 op.drop_index("ix_coord_repo_id_cursor", table_name="musehub_coord_records")
79 op.drop_table("musehub_coord_records")
File History 1 commit
sha256:a10adeeb7a0169cb9900f9806ed7a973047258abb6283724fe55e8eb68ff3f0a init: musehub initial commit Human 170 days ago