Files
state-hub/migrations/versions/a3b4c5d6e7f8_gems_pass3_sbom_snapshot.py
tegwick fc87e26b4b feat(gems): three-pass schema migration aligning state-hub with GEMS
Implements CUST-WP-0007. Resolves inconsistencies I-1, I-2, I-5, I-6
identified in the GEMS audit (GenericEntityModellingSystem.md).

Pass 1 (e1f2a3b4c5d6): domain_id FK on extension_points and
technical_debt (replaces raw string column); repo_id FK on contributions.
Fixes domain-filtering bugs in EP/TD dashboard pages.

Pass 2 (f2a3b4c5d6e7): repo_id nullable FK on workstreams, aligning
the GEMS primary attachment with ADR-001 (repo > topic). Dashboard
pages updated to prefer repo->domain over topic->domain.

Pass 3 (a3b4c5d6e7f8): SBOMSnapshot container entity (GEMS Complex
between Repository and SBOMEntry). Ingest is now additive — each call
creates a new snapshot; history is retained. List/report endpoints
filter to latest snapshot per repo via _latest_snapshot_ids_subquery().
New endpoints: GET /sbom/snapshots/, GET /sbom/snapshots/{id}/.
Dashboard gains a Snapshot History section.

Also adds GEMS analysis artefacts: wiki/GEMS-StateHub-TypeRegistry.md,
wiki/GEMS-StateHub-SWOT.md, workplans/CUST-WP-0006 (analysis),
workplans/CUST-WP-0007 (migration, now completed).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-02 23:39:17 +01:00

94 lines
3.4 KiB
Python

"""GEMS Pass 3: add sbom_snapshots container entity
Revision ID: a3b4c5d6e7f8
Revises: f2a3b4c5d6e7
Create Date: 2026-03-02 00:00:00.000000
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
revision: str = "a3b4c5d6e7f8"
down_revision: Union[str, None] = "f2a3b4c5d6e7"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ── Create sbom_snapshots table ────────────────────────────────────────────
op.create_table(
"sbom_snapshots",
sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True),
sa.Column(
"repo_id",
postgresql.UUID(as_uuid=True),
sa.ForeignKey("managed_repos.id", ondelete="RESTRICT"),
nullable=False,
index=True,
),
sa.Column("snapshot_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("source", sa.String(200), nullable=True),
sa.Column("entry_count", sa.Integer, nullable=False, server_default="0"),
sa.Column(
"created_at",
sa.DateTime(timezone=True),
server_default=sa.text("now()"),
nullable=False,
),
)
# ── Add snapshot_id FK to sbom_entries (nullable during backfill) ──────────
op.add_column(
"sbom_entries",
sa.Column("snapshot_id", postgresql.UUID(as_uuid=True), nullable=True),
)
op.create_foreign_key(
"fk_sbom_entry_snapshot_id",
"sbom_entries", "sbom_snapshots",
["snapshot_id"], ["id"],
ondelete="RESTRICT",
)
# ── Backfill: create one snapshot per (repo_id, snapshot_at) group ─────────
op.execute("""
INSERT INTO sbom_snapshots (id, repo_id, snapshot_at, source, entry_count, created_at)
SELECT
gen_random_uuid(),
repo_id,
snapshot_at,
'backfill' AS source,
COUNT(*) AS entry_count,
MIN(created_at) AS created_at
FROM sbom_entries
GROUP BY repo_id, snapshot_at
""")
# ── Assign snapshot_id to each entry ───────────────────────────────────────
op.execute("""
UPDATE sbom_entries e
SET snapshot_id = s.id
FROM sbom_snapshots s
WHERE s.repo_id = e.repo_id
AND s.snapshot_at = e.snapshot_at
""")
# ── Make snapshot_id NOT NULL ──────────────────────────────────────────────
op.execute("""
DO $$
BEGIN
IF EXISTS (SELECT 1 FROM sbom_entries WHERE snapshot_id IS NULL) THEN
RAISE EXCEPTION 'GEMS Pass 3: sbom_entries rows with no snapshot assigned';
END IF;
END $$;
""")
op.alter_column("sbom_entries", "snapshot_id", nullable=False)
op.create_index("ix_sbom_entries_snapshot_id", "sbom_entries", ["snapshot_id"])
def downgrade() -> None:
op.drop_index("ix_sbom_entries_snapshot_id", table_name="sbom_entries")
op.drop_constraint("fk_sbom_entry_snapshot_id", "sbom_entries", type_="foreignkey")
op.drop_column("sbom_entries", "snapshot_id")
op.drop_table("sbom_snapshots")