add Alias.ts_vector column to use full text search
This commit is contained in:
parent
e2fea3aed8
commit
2b048543d3
|
@ -8,7 +8,7 @@ import arrow
|
|||
from arrow import Arrow
|
||||
from flask import url_for
|
||||
from flask_login import UserMixin
|
||||
from sqlalchemy import text, desc, CheckConstraint
|
||||
from sqlalchemy import text, desc, CheckConstraint, Index
|
||||
from sqlalchemy.orm import deferred
|
||||
from sqlalchemy_utils import ArrowType
|
||||
|
||||
|
@ -39,6 +39,13 @@ from app.utils import (
|
|||
random_word,
|
||||
)
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects.postgresql import TSVECTOR
|
||||
|
||||
|
||||
class TSVector(sa.types.TypeDecorator):
|
||||
impl = TSVECTOR
|
||||
|
||||
|
||||
class ModelMixin(object):
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
|
||||
|
@ -1121,6 +1128,16 @@ class Alias(db.Model, ModelMixin):
|
|||
hibp_last_check = db.Column(ArrowType, default=None)
|
||||
hibp_breaches = db.relationship("Hibp", secondary="alias_hibp")
|
||||
|
||||
# to use Postgres full text search. Only applied on "note" column for now
|
||||
# this is a generated Postgres column
|
||||
ts_vector = db.Column(
|
||||
TSVector(), db.Computed("to_tsvector('english', note)", persisted=True)
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
Index("ix_video___ts_vector__", ts_vector, postgresql_using="gin"),
|
||||
)
|
||||
|
||||
user = db.relationship(User, foreign_keys=[user_id])
|
||||
mailbox = db.relationship("Mailbox", lazy="joined")
|
||||
|
||||
|
|
31
migrations/versions/2021_080409_9014cca7097c_.py
Normal file
31
migrations/versions/2021_080409_9014cca7097c_.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
"""empty message
|
||||
|
||||
Revision ID: 9014cca7097c
|
||||
Revises: ffa75d04e6ef
|
||||
Create Date: 2021-08-04 09:28:26.620053
|
||||
|
||||
"""
|
||||
import sqlalchemy_utils
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '9014cca7097c'
|
||||
down_revision = 'ffa75d04e6ef'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.add_column('alias', sa.Column('ts_vector', app.models.TSVector(), sa.Computed("to_tsvector('english', note)", persisted=True), nullable=True))
|
||||
op.create_index('ix_video___ts_vector__', 'alias', ['ts_vector'], unique=False, postgresql_using='gin')
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade():
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index('ix_video___ts_vector__', table_name='alias')
|
||||
op.drop_column('alias', 'ts_vector')
|
||||
# ### end Alembic commands ###
|
Loading…
Reference in a new issue