"""plex HU-overlay item/show columns + guid (E5 S5a) On plex_items and plex_shows: `guid` (canonical external id, the EN<->HU pairing key), the Hungarian twin fields `title_hu`/`summary_hu`/`genres_hu`, and a STORED generated `search_vector_hu` mirroring the EN vector (title_hu A / people_text B / left(summary_hu,1000) C) + a GIN index. people_text is language-neutral, reused for both vectors. Run BEFORE the first HU overlay sync: while the *_hu columns are all NULL the generated-column build is trivial. ADD only (no drop/rebuild of the EN search_vector). NOTE: adding a STORED generated column + its GIN takes an ACCESS EXCLUSIVE lock and rewrites the table — brief, but flag it on prod. """ from typing import Sequence, Union import sqlalchemy as sa from alembic import op from sqlalchemy.dialects import postgresql revision: str = "0064_plex_hu_overlay_cols" down_revision: Union[str, None] = "0063_plex_library_overlay" branch_labels: Union[str, Sequence[str], None] = None depends_on: Union[str, Sequence[str], None] = None _TABLES = ("plex_items", "plex_shows") # Mirror of the EN search_vector (models.py) but over the HU twin columns; people_text is shared. _VECTOR_HU = ( "setweight(to_tsvector('public.unaccent_simple', coalesce(title_hu, '')), 'A') || " "setweight(to_tsvector('public.unaccent_simple', coalesce(people_text, '')), 'B') || " "setweight(to_tsvector('public.unaccent_simple', left(coalesce(summary_hu, ''), 1000)), 'C')" ) def upgrade() -> None: for t in _TABLES: op.add_column(t, sa.Column("guid", sa.String(64), nullable=True)) op.add_column(t, sa.Column("title_hu", sa.String(512), nullable=True)) op.add_column(t, sa.Column("summary_hu", sa.Text(), nullable=True)) op.add_column(t, sa.Column("genres_hu", postgresql.JSONB(), nullable=True)) op.create_index(f"ix_{t}_guid", t, ["guid"]) op.create_index(f"ix_{t}_genres_hu_gin", t, ["genres_hu"], postgresql_using="gin") # Generated column can't be expressed via add_column — raw DDL, matching the EN idiom (0044). op.execute( f"ALTER TABLE {t} ADD COLUMN search_vector_hu tsvector " f"GENERATED ALWAYS AS ({_VECTOR_HU}) STORED" ) op.execute(f"CREATE INDEX ix_{t}_search_vector_hu ON {t} USING gin (search_vector_hu)") def downgrade() -> None: for t in _TABLES: op.execute(f"DROP INDEX IF EXISTS ix_{t}_search_vector_hu") op.execute(f"ALTER TABLE {t} DROP COLUMN IF EXISTS search_vector_hu") op.drop_index(f"ix_{t}_genres_hu_gin", table_name=t) op.drop_index(f"ix_{t}_guid", table_name=t) op.drop_column(t, "genres_hu") op.drop_column(t, "summary_hu") op.drop_column(t, "title_hu") op.drop_column(t, "guid")