gabriel / musehub public

0003_normalized_symbol_intel_tables.py file-level

at sha256:3 · View file ↗ · Intel ↗

History
1 files
1 commits
0 hotspots
0 🧊 dead
0 💥 blast risk
sha256:0 fix: fall back to any indexed mpack in read_object_bytes when push mpac… · gabriel · Jun 17, 2026
1 """normalized_symbol_intel_tables
2
3 Replaces three unbounded JSON blobs in musehub_intel_results with normalized
4 relational tables:
5
6 musehub_symbol_history_entries — one row per (repo_id, address, commit_id)
7 musehub_symbol_intel — one row per (repo_id, address)
8 musehub_hash_occurrence_entries — one row per (content_id, repo_id, address)
9
10 code.symbol_history, code.per_symbol_intel, and code.hash_occurrence rows in
11 musehub_intel_results are deleted on upgrade — they will be rebuilt
12 incrementally on the next push to each repo.
13
14 Revision ID: 0003
15 Revises: 0002
16 Create Date: 2026-04-29 20:45:00.000000+00:00
17 """
18 from __future__ import annotations
19
20 from typing import Sequence, Union
21
22 import sqlalchemy as sa
23 from alembic import op
24
25 revision: str = '0003'
26 down_revision: Union[str, None] = '0002'
27 branch_labels: Union[str, Sequence[str], None] = None
28 depends_on: Union[str, Sequence[str], None] = None
29
30
31 def upgrade() -> None:
32 op.create_table(
33 'musehub_symbol_history_entries',
34 sa.Column('repo_id', sa.String(128), sa.ForeignKey('musehub_repos.repo_id', ondelete='CASCADE'), primary_key=True, nullable=False),
35 sa.Column('address', sa.String(512), primary_key=True, nullable=False),
36 sa.Column('commit_id', sa.String(128), primary_key=True, nullable=False),
37 sa.Column('committed_at', sa.DateTime(timezone=True), nullable=False),
38 sa.Column('author', sa.String(256), nullable=True),
39 sa.Column('op', sa.String(32), nullable=False),
40 sa.Column('content_id', sa.String(128), nullable=True),
41 )
42 op.create_index('ix_symbol_history_repo_address', 'musehub_symbol_history_entries', ['repo_id', 'address'])
43 op.create_index('ix_symbol_history_repo_address_ts', 'musehub_symbol_history_entries', ['repo_id', 'address', 'committed_at'])
44
45 op.create_table(
46 'musehub_symbol_intel',
47 sa.Column('repo_id', sa.String(128), sa.ForeignKey('musehub_repos.repo_id', ondelete='CASCADE'), primary_key=True, nullable=False),
48 sa.Column('address', sa.String(512), primary_key=True, nullable=False),
49 sa.Column('churn', sa.Integer, nullable=False, server_default='0'),
50 sa.Column('churn_30d', sa.Integer, nullable=False, server_default='0'),
51 sa.Column('churn_90d', sa.Integer, nullable=False, server_default='0'),
52 sa.Column('blast', sa.Integer, nullable=False, server_default='0'),
53 sa.Column('blast_direct', sa.Integer, nullable=False, server_default='0'),
54 sa.Column('blast_cross', sa.Integer, nullable=False, server_default='0'),
55 sa.Column('blast_top', sa.ARRAY(sa.Text), nullable=False, server_default='{}'),
56 sa.Column('last_changed', sa.DateTime(timezone=True), nullable=True),
57 sa.Column('last_author', sa.String(256), nullable=True),
58 sa.Column('author_count', sa.Integer, nullable=False, server_default='0'),
59 sa.Column('gravity', sa.Float, nullable=False, server_default='0.0'),
60 sa.Column('weekly', sa.ARRAY(sa.Integer), nullable=False, server_default='{}'),
61 )
62 op.create_index('ix_symbol_intel_repo_churn', 'musehub_symbol_intel', ['repo_id', 'churn'])
63 op.create_index('ix_symbol_intel_repo_gravity', 'musehub_symbol_intel', ['repo_id', 'gravity'])
64
65 op.create_table(
66 'musehub_hash_occurrence_entries',
67 sa.Column('content_id', sa.String(128), primary_key=True, nullable=False),
68 sa.Column('repo_id', sa.String(128), sa.ForeignKey('musehub_repos.repo_id', ondelete='CASCADE'), primary_key=True, nullable=False),
69 sa.Column('address', sa.String(512), primary_key=True, nullable=False),
70 )
71 op.create_index('ix_hash_occurrence_repo_content', 'musehub_hash_occurrence_entries', ['repo_id', 'content_id'])
72
73 # Remove stale blob types — will be rebuilt from normalized tables on next push.
74 op.execute("""
75 DELETE FROM musehub_intel_results
76 WHERE intel_type IN (
77 'code.symbol_history',
78 'code.per_symbol_intel',
79 'code.hash_occurrence'
80 )
81 """)
82
83
84 def downgrade() -> None:
85 op.drop_table('musehub_hash_occurrence_entries')
86 op.drop_table('musehub_symbol_intel')
87 op.drop_table('musehub_symbol_history_entries')