Files
leocrm/alembic/versions/0094_fix_gin_indexes_and_plugins_dedup.py

61 lines
2.1 KiB
Python

"""Fix GIN indexes and remove duplicate plugins.name index.
Alembic 0002 created search indexes without USING GIN.
Production already has GIN indexes (corrected by later migrations or manual).
This migration ensures GIN indexes exist for both fresh install and existing DBs.
Also removes the redundant ix_plugins_name unique index (plugins_name_key
already enforces uniqueness from the column definition).
Revision ID: 0094
Revises: 0093
"""
from __future__ import annotations
from alembic import op
revision = "0094"
down_revision = "0093"
branch_labels = None
depends_on = None
# GIN indexes that should exist with USING GIN
GIN_INDEXES = [
("contacts", "ix_contacts_search_tsv", "search_tsv"),
("audit_log", "ix_audit_log_search_tsv", "search_tsv"),
("calendar_entries", "ix_cal_entries_search_tsv", "search_tsv"),
("comm_messages", "ix_comm_messages_search_tsv", "search_tsv"),
("files", "ix_files_content_tsv", "content_tsv"),
("mails", "ix_mails_body_tsv", "body_tsv"),
("tags", "ix_tags_search_tsv", "search_tsv"),
]
def upgrade() -> None:
# Fix GIN indexes: drop and recreate with USING GIN (idempotent)
# Only create index if the column exists (avoids failure on fresh DB)
for table, index_name, column in GIN_INDEXES:
op.execute(f"DROP INDEX IF EXISTS {index_name}")
# Check if column exists before creating index
op.execute(
"DO $do$ BEGIN "
"IF EXISTS (SELECT 1 FROM information_schema.columns "
f"WHERE table_name = '{table}' AND column_name = '{column}') THEN "
f"CREATE INDEX IF NOT EXISTS {index_name} "
f"ON {table} USING gin ({column}); "
"END IF; "
"END $do$;"
)
# Remove redundant plugins.name index (plugins_name_key already enforces uniqueness)
op.execute("DROP INDEX IF EXISTS ix_plugins_name")
def downgrade() -> None:
# Recreate the dropped index without GIN (not truly reversible to wrong state)
op.execute(
"CREATE INDEX IF NOT EXISTS ix_plugins_name ON plugins (name)"
)
# GIN indexes cannot be meaningfully downgraded to non-GIN