← Back to Pull requests

feat: detect and repair index definition drift during migrations #134

Open
jerenkrantz opened this pull request with 221 lines diff across 2 files
View on GitHub
jerenkrantz jerenkrantz commented on Feb 25, 2026 20:45 UTC
Author

Summary

  • Migrations() now detects when an existing index's definition has drifted from the expected state and automatically repairs it

  • Previously, only index existence was checked — if an index existed by name but had different columns, method, or predicate, the drift was silently ignored

  • Compares against PostgreSQL's canonical pg_indexes.indexdef as the source of truth, handling all index types (BTREE, GIN, BTREE_GIN, HNSW), UNIQUE indexes, WHERE predicates, override expressions, and partitioned tables (ON ONLY)
  • Migration strategy

    Uses a create-then-drop approach to avoid any window without an index:

    1. CREATE INDEX CONCURRENTLY IF NOT EXISTS "<name>_new" ... — builds the replacement index (non-blocking)
    2. DROP INDEX CONCURRENTLY IF EXISTS "<name>" — removes the stale index (non-blocking)
    3. ALTER INDEX "<name>_new" RENAME TO "<name>" — instant catalog-only rename

    CONCURRENTLY is used for non-partitioned tables (matching existing behavior via index2sql). This ensures queries always have at least one usable index during the transition.

    Drift detection only triggers when index2expectedDef() produces a definition that differs from what pg_indexes.indexdef actually stores — unchanged indexes are skipped with zero overhead.

    Test plan

  • Unit tests for pgQuoteIdent, pgNormalizeExpr, index2expectedDef, indexRename2sql covering all index types, partitioned tables, uppercase names, dropped/primary indexes

  • Integration test TestMigrationIndexMutation — creates schema, introduces drift, verifies the 3-step CREATE/DROP/RENAME migration, and confirms convergence

  • All existing tests pass (animals, food, zoo, city, pgdb/v1)
  • 🤖 Generated with Claude Code

    Diff: 221 lines (+165 / -56) across 2 files Reviewer: None requested Review duration: 408126m
    All Vouch review confidence checks passed
    Residual risk 0.46 = Change Risk (0.79) × (1 − Review Confidence (0.42)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.79 · 221 lines diff across 2 files
    0.79
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 10%
    42%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.323
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Sensitive Paths sensitive_paths
    0.079
    Real PR Conversation Timeline
    1 comments
    "Review active on #134"
    general weight: 0.35
    Reviewers
    None requested
    Assignees
    jerenkrantz jerenkrantz
    Labels
    risk: medium confidence: 42.2% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 79.0% (HIGH) · Higher = larger blast radius
    79.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 42.2% · Lower = rushed or shallow review
    42.2%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 45.6% · Moderate risk
    45.6%