"""MuseHub V2 — Domain-agnostic paradigm shift. Revision ID: 0002 Revises: 0001 Create Date: 2026-03-18 Adds the Muse domain plugin registry and makes all existing tables domain-agnostic. Changes: NEW TABLES - musehub_domains: Muse domain plugin registry (@author/slug namespace) - musehub_domain_installs: User ↔ domain adoption tracking MUSEHUB_REPOS - ADD domain_id (nullable FK → musehub_domains) - ADD domain_meta JSON (replaces key_signature + tempo_bpm) - DROP key_signature - DROP tempo_bpm MUSEHUB_PR_COMMENTS (proposal review comments) - ADD dimension_ref JSON (domain-agnostic replacement for music-specific fields) - DROP target_type - DROP target_track - DROP target_beat_start - DROP target_beat_end - DROP target_note_pitch MUSEHUB_ISSUE_COMMENTS - RENAME musical_refs → state_refs MUSEHUB_RENDER_JOBS - RENAME midi_count → artifact_count - RENAME mp3_object_ids → audio_object_ids - RENAME image_object_ids → preview_object_ids SEED DATA - Insert @gabriel/code built-in domain (symbol-graph code) """ from __future__ import annotations import json import hashlib import sqlalchemy as sa from alembic import op revision = "0002" down_revision = "0001" branch_labels = None depends_on = None def _manifest_hash(capabilities: dict) -> str: """Compute SHA-256 of the capabilities JSON (sorted keys).""" blob = json.dumps(capabilities, sort_keys=True, separators=(",", ":")).encode() return hashlib.sha256(blob).hexdigest() # Capability manifest for the built-in .code domain _CODE_CAPABILITIES = { "dimensions": [ {"name": "symbols", "description": "Function and class symbol graph"}, {"name": "hotspots", "description": "Most frequently changed symbols"}, {"name": "coupling", "description": "Symbol-level coupling and cohesion"}, {"name": "complexity", "description": "Cyclomatic complexity per symbol"}, {"name": "churn", "description": "Commit frequency per file and symbol"}, {"name": "coverage", "description": "Test coverage by symbol"}, {"name": "dependencies", "description": "Import and dependency graph"}, {"name": "duplicates", "description": "Semantically duplicated code blocks"}, {"name": "refactors", "description": "Detected rename and move operations"}, {"name": "types", "description": "Type annotation completeness"}, ], "viewer_type": "symbol_graph", "artifact_types": [ "text/x-python", "text/typescript", "text/javascript", "text/x-go", "text/x-rust", "text/x-java", ], "merge_semantics": "ot", "supported_commands": [ "muse symbols", "muse hotspots", "muse coupling", "muse diff", "muse query", "muse refactor", ], } def upgrade() -> None: # ── musehub_domains ─────────────────────────────────────────────────────── op.create_table( "musehub_domains", sa.Column("domain_id", sa.String(36), nullable=False), sa.Column("author_user_id", sa.String(36), nullable=True), sa.Column("author_slug", sa.String(64), nullable=False), sa.Column("slug", sa.String(64), nullable=False), sa.Column("display_name", sa.String(255), nullable=False), sa.Column("description", sa.Text(), nullable=False, server_default=""), sa.Column("version", sa.String(32), nullable=False, server_default="1.0.0"), sa.Column("manifest_hash", sa.String(64), nullable=False, server_default=""), sa.Column("capabilities", sa.JSON(), nullable=False), sa.Column("viewer_type", sa.String(64), nullable=False, server_default="generic"), sa.Column("install_count", sa.Integer(), nullable=False, server_default="0"), sa.Column("is_verified", sa.Boolean(), nullable=False, server_default="false"), sa.Column("is_deprecated", sa.Boolean(), nullable=False, server_default="false"), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")), sa.PrimaryKeyConstraint("domain_id"), sa.UniqueConstraint("author_slug", "slug", name="uq_musehub_domains_author_slug"), ) op.create_index("ix_musehub_domains_author_slug", "musehub_domains", ["author_slug"]) op.create_index("ix_musehub_domains_slug", "musehub_domains", ["slug"]) op.create_index("ix_musehub_domains_author_user_id", "musehub_domains", ["author_user_id"]) # ── musehub_domain_installs ─────────────────────────────────────────────── op.create_table( "musehub_domain_installs", sa.Column("install_id", sa.String(36), nullable=False), sa.Column("user_id", sa.String(36), nullable=False), sa.Column("domain_id", sa.String(36), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), nullable=False, server_default=sa.text("CURRENT_TIMESTAMP")), sa.PrimaryKeyConstraint("install_id"), sa.UniqueConstraint("user_id", "domain_id", name="uq_musehub_domain_installs"), ) op.create_index("ix_musehub_domain_installs_user_id", "musehub_domain_installs", ["user_id"]) op.create_index("ix_musehub_domain_installs_domain_id", "musehub_domain_installs", ["domain_id"]) # ── musehub_repos: add domain_id + domain_meta, drop key_signature + tempo_bpm ── op.add_column("musehub_repos", sa.Column("domain_id", sa.String(36), nullable=True)) op.add_column("musehub_repos", sa.Column("domain_meta", sa.JSON(), nullable=False, server_default="{}")) op.create_index("ix_musehub_repos_domain_id", "musehub_repos", ["domain_id"]) op.drop_column("musehub_repos", "key_signature") op.drop_column("musehub_repos", "tempo_bpm") # ── musehub_pr_comments: add dimension_ref, drop music-specific fields ─── op.add_column("musehub_pr_comments", sa.Column("dimension_ref", sa.JSON(), nullable=False, server_default="{}")) op.drop_column("musehub_pr_comments", "target_type") op.drop_column("musehub_pr_comments", "target_track") op.drop_column("musehub_pr_comments", "target_beat_start") op.drop_column("musehub_pr_comments", "target_beat_end") op.drop_column("musehub_pr_comments", "target_note_pitch") # ── musehub_issue_comments: rename musical_refs → state_refs ───────────── op.add_column("musehub_issue_comments", sa.Column("state_refs", sa.JSON(), nullable=False, server_default="[]")) # Copy existing data to new column op.execute( "UPDATE musehub_issue_comments SET state_refs = musical_refs" ) op.drop_column("musehub_issue_comments", "musical_refs") # ── musehub_render_jobs: rename domain-specific column names ────────────── op.add_column("musehub_render_jobs", sa.Column("artifact_count", sa.Integer(), nullable=False, server_default="0")) op.add_column("musehub_render_jobs", sa.Column("audio_object_ids", sa.JSON(), nullable=False, server_default="[]")) op.add_column("musehub_render_jobs", sa.Column("preview_object_ids", sa.JSON(), nullable=False, server_default="[]")) # Copy existing data op.execute("UPDATE musehub_render_jobs SET artifact_count = midi_count") op.execute("UPDATE musehub_render_jobs SET audio_object_ids = mp3_object_ids") op.execute("UPDATE musehub_render_jobs SET preview_object_ids = image_object_ids") op.drop_column("musehub_render_jobs", "midi_count") op.drop_column("musehub_render_jobs", "mp3_object_ids") op.drop_column("musehub_render_jobs", "image_object_ids") # ── Seed built-in .code domain ──────────────────────────────────────────── # Stable ID so tooling can reference it without querying the DB. # Not a real UUID — valid VARCHAR(36) matching DOMAIN_CODE constant. _DOMAIN_CODE_ID = "domain-code-gabriel-0001" code_caps_json = json.dumps(_CODE_CAPABILITIES) op.execute( sa.text( "INSERT INTO musehub_domains " "(domain_id, author_user_id, author_slug, slug, display_name, description, " "version, manifest_hash, capabilities, viewer_type, install_count, " "is_verified, is_deprecated, created_at, updated_at) " "VALUES (:did, NULL, 'gabriel', 'code', 'Code', " "'Symbol-graph code state space — diff and merge at the level of named " "functions, classes, and modules across Python, TypeScript, Go, Rust, " "Java, C, C++, C#, Ruby, and Kotlin.', " f"'1.0.0', :chash, CAST('{code_caps_json.replace(chr(39), chr(39)+chr(39))}' AS json), " "'symbol_graph', 0, true, false, now(), now()) " "ON CONFLICT (author_slug, slug) DO NOTHING" ).bindparams( did=_DOMAIN_CODE_ID, chash=_manifest_hash(_CODE_CAPABILITIES), ) ) def downgrade() -> None: # Restore musehub_render_jobs original columns op.add_column("musehub_render_jobs", sa.Column("midi_count", sa.Integer(), nullable=False, server_default="0")) op.add_column("musehub_render_jobs", sa.Column("mp3_object_ids", sa.JSON(), nullable=False, server_default="[]")) op.add_column("musehub_render_jobs", sa.Column("image_object_ids", sa.JSON(), nullable=False, server_default="[]")) op.execute("UPDATE musehub_render_jobs SET midi_count = artifact_count") op.execute("UPDATE musehub_render_jobs SET mp3_object_ids = audio_object_ids") op.execute("UPDATE musehub_render_jobs SET image_object_ids = preview_object_ids") op.drop_column("musehub_render_jobs", "artifact_count") op.drop_column("musehub_render_jobs", "audio_object_ids") op.drop_column("musehub_render_jobs", "preview_object_ids") # Restore musehub_issue_comments op.add_column("musehub_issue_comments", sa.Column("musical_refs", sa.JSON(), nullable=False, server_default="[]")) op.execute("UPDATE musehub_issue_comments SET musical_refs = state_refs") op.drop_column("musehub_issue_comments", "state_refs") # Restore musehub_pr_comments op.add_column("musehub_pr_comments", sa.Column("target_type", sa.String(20), nullable=False, server_default="general")) op.add_column("musehub_pr_comments", sa.Column("target_track", sa.String(255), nullable=True)) op.add_column("musehub_pr_comments", sa.Column("target_beat_start", sa.Float(), nullable=True)) op.add_column("musehub_pr_comments", sa.Column("target_beat_end", sa.Float(), nullable=True)) op.add_column("musehub_pr_comments", sa.Column("target_note_pitch", sa.Integer(), nullable=True)) op.drop_column("musehub_pr_comments", "dimension_ref") # Restore musehub_repos op.drop_index("ix_musehub_repos_domain_id", table_name="musehub_repos") op.drop_column("musehub_repos", "domain_id") op.drop_column("musehub_repos", "domain_meta") op.add_column("musehub_repos", sa.Column("key_signature", sa.String(50), nullable=True)) op.add_column("musehub_repos", sa.Column("tempo_bpm", sa.Integer(), nullable=True)) # Drop new tables op.drop_index("ix_musehub_domain_installs_domain_id", table_name="musehub_domain_installs") op.drop_index("ix_musehub_domain_installs_user_id", table_name="musehub_domain_installs") op.drop_table("musehub_domain_installs") op.drop_index("ix_musehub_domains_author_user_id", table_name="musehub_domains") op.drop_index("ix_musehub_domains_slug", table_name="musehub_domains") op.drop_index("ix_musehub_domains_author_slug", table_name="musehub_domains") op.drop_table("musehub_domains")