PMFlow/backend/alembic/versions/20260311_add_pg_trgm_text_i...

77 lines
2.0 KiB
Python

"""add pg_trgm text indexes for project search
Revision ID: add_pg_trgm_text_indexes
Revises: add_feishu_outbox_reply_hash
Create Date: 2026-03-11
"""
from __future__ import annotations
from alembic import op
revision = "add_pg_trgm_text_indexes"
down_revision = "add_feishu_outbox_reply_hash"
branch_labels = None
depends_on = None
def _is_postgresql() -> bool:
bind = op.get_bind()
return str(getattr(getattr(bind, "dialect", None), "name", "")).lower().startswith("postgres")
def upgrade() -> None:
if not _is_postgresql():
return
op.execute("CREATE EXTENSION IF NOT EXISTS pg_trgm")
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_projects_name_trgm
ON projects USING GIN (name gin_trgm_ops)
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_projects_title_trgm
ON projects USING GIN (title gin_trgm_ops)
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_projects_description_trgm
ON projects USING GIN (description gin_trgm_ops)
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_signals_title_trgm
ON signals USING GIN (title gin_trgm_ops)
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_signals_content_trgm
ON signals USING GIN (content gin_trgm_ops)
"""
)
op.execute(
"""
CREATE INDEX IF NOT EXISTS ix_tasks_title_trgm
ON tasks USING GIN (title gin_trgm_ops)
"""
)
def downgrade() -> None:
if not _is_postgresql():
return
op.execute("DROP INDEX IF EXISTS ix_tasks_title_trgm")
op.execute("DROP INDEX IF EXISTS ix_signals_content_trgm")
op.execute("DROP INDEX IF EXISTS ix_signals_title_trgm")
op.execute("DROP INDEX IF EXISTS ix_projects_description_trgm")
op.execute("DROP INDEX IF EXISTS ix_projects_title_trgm")
op.execute("DROP INDEX IF EXISTS ix_projects_name_trgm")