Fixing bug where video.textsearch would be null if one of the columns was null.

This commit is contained in:
Roland 2022-01-04 11:05:00 -07:00
parent fe9bdc190e
commit 38b756cf9d
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93

View file

@ -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)')