|
| 1 | +"""Persist vector sync fingerprints on chunk metadata. |
| 2 | +
|
| 3 | +Revision ID: m6h7i8j9k0l1 |
| 4 | +Revises: l5g6h7i8j9k0 |
| 5 | +Create Date: 2026-04-07 00:00:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +from alembic import op |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision: str = "m6h7i8j9k0l1" |
| 15 | +down_revision: Union[str, None] = "l5g6h7i8j9k0" |
| 16 | +branch_labels: Union[str, Sequence[str], None] = None |
| 17 | +depends_on: Union[str, Sequence[str], None] = None |
| 18 | + |
| 19 | + |
| 20 | +def upgrade() -> None: |
| 21 | + """Add entity fingerprint + embedding model metadata to Postgres chunk rows. |
| 22 | +
|
| 23 | + Trigger: vector sync now fast-skips unchanged entities using persisted |
| 24 | + semantic fingerprints. |
| 25 | + Why: chunk rows already own the per-entity derived metadata we diff against, |
| 26 | + so persisting the fingerprint on that table avoids a second sync-state table. |
| 27 | + Outcome: existing rows get empty-string placeholders and will be refreshed on |
| 28 | + the next vector sync before they become eligible for skip checks. |
| 29 | + """ |
| 30 | + connection = op.get_bind() |
| 31 | + if connection.dialect.name != "postgresql": |
| 32 | + return |
| 33 | + |
| 34 | + op.execute( |
| 35 | + """ |
| 36 | + ALTER TABLE search_vector_chunks |
| 37 | + ADD COLUMN IF NOT EXISTS entity_fingerprint TEXT |
| 38 | + """ |
| 39 | + ) |
| 40 | + op.execute( |
| 41 | + """ |
| 42 | + ALTER TABLE search_vector_chunks |
| 43 | + ADD COLUMN IF NOT EXISTS embedding_model TEXT |
| 44 | + """ |
| 45 | + ) |
| 46 | + op.execute( |
| 47 | + """ |
| 48 | + UPDATE search_vector_chunks |
| 49 | + SET entity_fingerprint = COALESCE(entity_fingerprint, ''), |
| 50 | + embedding_model = COALESCE(embedding_model, '') |
| 51 | + """ |
| 52 | + ) |
| 53 | + op.execute( |
| 54 | + """ |
| 55 | + ALTER TABLE search_vector_chunks |
| 56 | + ALTER COLUMN entity_fingerprint SET NOT NULL |
| 57 | + """ |
| 58 | + ) |
| 59 | + op.execute( |
| 60 | + """ |
| 61 | + ALTER TABLE search_vector_chunks |
| 62 | + ALTER COLUMN embedding_model SET NOT NULL |
| 63 | + """ |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def downgrade() -> None: |
| 68 | + """Remove vector sync fingerprint columns from Postgres chunk rows.""" |
| 69 | + connection = op.get_bind() |
| 70 | + if connection.dialect.name != "postgresql": |
| 71 | + return |
| 72 | + |
| 73 | + op.execute( |
| 74 | + """ |
| 75 | + ALTER TABLE search_vector_chunks |
| 76 | + DROP COLUMN IF EXISTS embedding_model |
| 77 | + """ |
| 78 | + ) |
| 79 | + op.execute( |
| 80 | + """ |
| 81 | + ALTER TABLE search_vector_chunks |
| 82 | + DROP COLUMN IF EXISTS entity_fingerprint |
| 83 | + """ |
| 84 | + ) |
0 commit comments