From 38b756cf9dcb6195b880a1bc0a2873653cefeed0 Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 4 Jan 2022 11:05:00 -0700 Subject: [PATCH] Fixing bug where video.textsearch would be null if one of the columns was null. --- ...0005fe2dc_video_textsearch_coalesce_fix.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 alembic/versions/0400005fe2dc_video_textsearch_coalesce_fix.py diff --git a/alembic/versions/0400005fe2dc_video_textsearch_coalesce_fix.py b/alembic/versions/0400005fe2dc_video_textsearch_coalesce_fix.py new file mode 100644 index 00000000..051eb539 --- /dev/null +++ b/alembic/versions/0400005fe2dc_video_textsearch_coalesce_fix.py @@ -0,0 +1,37 @@ +"""Video textsearch coalesce fix. + +Revision ID: 0400005fe2dc +Revises: 53f6780aa599 +Create Date: 2022-01-04 11:00:21.776124 + +""" +import os + +from alembic import op +from sqlalchemy.orm import Session + +# revision identifiers, used by Alembic. +revision = '0400005fe2dc' +down_revision = '53f6780aa599' +branch_labels = None +depends_on = None + +DOCKERIZED = True if os.environ.get('DOCKER', '').lower().startswith('t') else False + + +def upgrade(): + bind = op.get_bind() + session = Session(bind=bind) + + # Replace the textsearch and index. The old textsearch failed if one of the columns was null. + session.execute('DROP INDEX IF EXISTS video_textsearch_idx') + session.execute('ALTER TABLE video DROP COLUMN IF EXISTS textsearch') + session.execute(''' + ALTER TABLE video + ADD COLUMN textsearch tsvector + GENERATED ALWAYS AS ( + setweight(to_tsvector('english'::regconfig, COALESCE(title, '')), 'A') || + setweight(to_tsvector('english'::regconfig, COALESCE(caption, '')), 'D') + ) STORED + ''') + session.execute('CREATE INDEX IF NOT EXISTS video_textsearch_idx ON video USING GIN(textsearch)')