|
| 1 | +"""Add project_id to relation/observation and pg_trgm for fuzzy link resolution |
| 2 | +
|
| 3 | +Revision ID: f8a9b2c3d4e5 |
| 4 | +Revises: 314f1ea54dc4 |
| 5 | +Create Date: 2025-12-01 12:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | + |
| 14 | + |
| 15 | +# revision identifiers, used by Alembic. |
| 16 | +revision: str = "f8a9b2c3d4e5" |
| 17 | +down_revision: Union[str, None] = "314f1ea54dc4" |
| 18 | +branch_labels: Union[str, Sequence[str], None] = None |
| 19 | +depends_on: Union[str, Sequence[str], None] = None |
| 20 | + |
| 21 | + |
| 22 | +def upgrade() -> None: |
| 23 | + """Add project_id to relation and observation tables, plus pg_trgm indexes. |
| 24 | +
|
| 25 | + This migration: |
| 26 | + 1. Adds project_id column to relation and observation tables (denormalization) |
| 27 | + 2. Backfills project_id from the associated entity |
| 28 | + 3. Enables pg_trgm extension for trigram-based fuzzy matching (Postgres only) |
| 29 | + 4. Creates GIN indexes on entity title and permalink for fast similarity searches |
| 30 | + 5. Creates partial index on unresolved relations for efficient bulk resolution |
| 31 | + """ |
| 32 | + connection = op.get_bind() |
| 33 | + dialect = connection.dialect.name |
| 34 | + |
| 35 | + # ------------------------------------------------------------------------- |
| 36 | + # Add project_id to relation table |
| 37 | + # ------------------------------------------------------------------------- |
| 38 | + |
| 39 | + # Step 1: Add project_id column as nullable first |
| 40 | + op.add_column("relation", sa.Column("project_id", sa.Integer(), nullable=True)) |
| 41 | + |
| 42 | + # Step 2: Backfill project_id from entity.project_id via from_id |
| 43 | + if dialect == "postgresql": |
| 44 | + op.execute(""" |
| 45 | + UPDATE relation |
| 46 | + SET project_id = entity.project_id |
| 47 | + FROM entity |
| 48 | + WHERE relation.from_id = entity.id |
| 49 | + """) |
| 50 | + else: |
| 51 | + # SQLite syntax |
| 52 | + op.execute(""" |
| 53 | + UPDATE relation |
| 54 | + SET project_id = ( |
| 55 | + SELECT entity.project_id |
| 56 | + FROM entity |
| 57 | + WHERE entity.id = relation.from_id |
| 58 | + ) |
| 59 | + """) |
| 60 | + |
| 61 | + # Step 3: Make project_id NOT NULL and add foreign key |
| 62 | + op.alter_column("relation", "project_id", nullable=False) |
| 63 | + op.create_foreign_key( |
| 64 | + "fk_relation_project_id", |
| 65 | + "relation", |
| 66 | + "project", |
| 67 | + ["project_id"], |
| 68 | + ["id"], |
| 69 | + ) |
| 70 | + |
| 71 | + # Step 4: Create index on relation.project_id |
| 72 | + op.create_index("ix_relation_project_id", "relation", ["project_id"]) |
| 73 | + |
| 74 | + # ------------------------------------------------------------------------- |
| 75 | + # Add project_id to observation table |
| 76 | + # ------------------------------------------------------------------------- |
| 77 | + |
| 78 | + # Step 1: Add project_id column as nullable first |
| 79 | + op.add_column("observation", sa.Column("project_id", sa.Integer(), nullable=True)) |
| 80 | + |
| 81 | + # Step 2: Backfill project_id from entity.project_id via entity_id |
| 82 | + if dialect == "postgresql": |
| 83 | + op.execute(""" |
| 84 | + UPDATE observation |
| 85 | + SET project_id = entity.project_id |
| 86 | + FROM entity |
| 87 | + WHERE observation.entity_id = entity.id |
| 88 | + """) |
| 89 | + else: |
| 90 | + # SQLite syntax |
| 91 | + op.execute(""" |
| 92 | + UPDATE observation |
| 93 | + SET project_id = ( |
| 94 | + SELECT entity.project_id |
| 95 | + FROM entity |
| 96 | + WHERE entity.id = observation.entity_id |
| 97 | + ) |
| 98 | + """) |
| 99 | + |
| 100 | + # Step 3: Make project_id NOT NULL and add foreign key |
| 101 | + op.alter_column("observation", "project_id", nullable=False) |
| 102 | + op.create_foreign_key( |
| 103 | + "fk_observation_project_id", |
| 104 | + "observation", |
| 105 | + "project", |
| 106 | + ["project_id"], |
| 107 | + ["id"], |
| 108 | + ) |
| 109 | + |
| 110 | + # Step 4: Create index on observation.project_id |
| 111 | + op.create_index("ix_observation_project_id", "observation", ["project_id"]) |
| 112 | + |
| 113 | + # Postgres-specific: pg_trgm and GIN indexes |
| 114 | + if dialect == "postgresql": |
| 115 | + # Enable pg_trgm extension for fuzzy string matching |
| 116 | + op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm") |
| 117 | + |
| 118 | + # Create trigram indexes on entity table for fuzzy matching |
| 119 | + # GIN indexes with gin_trgm_ops support similarity searches |
| 120 | + op.execute(""" |
| 121 | + CREATE INDEX IF NOT EXISTS idx_entity_title_trgm |
| 122 | + ON entity USING gin (title gin_trgm_ops) |
| 123 | + """) |
| 124 | + |
| 125 | + op.execute(""" |
| 126 | + CREATE INDEX IF NOT EXISTS idx_entity_permalink_trgm |
| 127 | + ON entity USING gin (permalink gin_trgm_ops) |
| 128 | + """) |
| 129 | + |
| 130 | + # Create partial index on unresolved relations for efficient bulk resolution |
| 131 | + # This makes "WHERE to_id IS NULL AND project_id = X" queries very fast |
| 132 | + op.execute(""" |
| 133 | + CREATE INDEX IF NOT EXISTS idx_relation_unresolved |
| 134 | + ON relation (project_id, to_name) |
| 135 | + WHERE to_id IS NULL |
| 136 | + """) |
| 137 | + |
| 138 | + # Create index on relation.to_name for join performance in bulk resolution |
| 139 | + op.execute(""" |
| 140 | + CREATE INDEX IF NOT EXISTS idx_relation_to_name |
| 141 | + ON relation (to_name) |
| 142 | + """) |
| 143 | + |
| 144 | + |
| 145 | +def downgrade() -> None: |
| 146 | + """Remove project_id from relation/observation and pg_trgm indexes.""" |
| 147 | + connection = op.get_bind() |
| 148 | + dialect = connection.dialect.name |
| 149 | + |
| 150 | + if dialect == "postgresql": |
| 151 | + # Drop Postgres-specific indexes |
| 152 | + op.execute("DROP INDEX IF EXISTS idx_relation_to_name") |
| 153 | + op.execute("DROP INDEX IF EXISTS idx_relation_unresolved") |
| 154 | + op.execute("DROP INDEX IF EXISTS idx_entity_permalink_trgm") |
| 155 | + op.execute("DROP INDEX IF EXISTS idx_entity_title_trgm") |
| 156 | + # Note: We don't drop the pg_trgm extension as other code may depend on it |
| 157 | + |
| 158 | + # Drop project_id from observation |
| 159 | + op.drop_index("ix_observation_project_id", table_name="observation") |
| 160 | + op.drop_constraint("fk_observation_project_id", "observation", type_="foreignkey") |
| 161 | + op.drop_column("observation", "project_id") |
| 162 | + |
| 163 | + # Drop project_id from relation |
| 164 | + op.drop_index("ix_relation_project_id", table_name="relation") |
| 165 | + op.drop_constraint("fk_relation_project_id", "relation", type_="foreignkey") |
| 166 | + op.drop_column("relation", "project_id") |
0 commit comments