Fix SQLAlchemy 2.0 count().filter(); add search_vector trigger for full-text search
Some checks failed
build-and-deploy / build (push) Failing after 15s
Some checks failed
build-and-deploy / build (push) Failing after 15s
This commit is contained in:
parent
49d5756ceb
commit
8eabe3b5e8
2 changed files with 20 additions and 3 deletions
|
|
@ -65,6 +65,23 @@ def upgrade() -> None:
|
|||
# Convert events to TimescaleDB hypertable
|
||||
op.execute("SELECT create_hypertable('events', 'ingested_at', if_not_exists => TRUE)")
|
||||
|
||||
# Keep search_vector populated from title + body on every insert/update.
|
||||
op.execute("""
|
||||
CREATE OR REPLACE FUNCTION events_search_vector_update() RETURNS trigger AS $$
|
||||
BEGIN
|
||||
NEW.search_vector :=
|
||||
setweight(to_tsvector('english', coalesce(NEW.title, '')), 'A') ||
|
||||
setweight(to_tsvector('english', coalesce(NEW.body, '')), 'B');
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
""")
|
||||
op.execute("""
|
||||
CREATE TRIGGER events_search_vector_trigger
|
||||
BEFORE INSERT OR UPDATE ON events
|
||||
FOR EACH ROW EXECUTE FUNCTION events_search_vector_update();
|
||||
""")
|
||||
|
||||
# GIN index for full-text search
|
||||
op.create_index('ix_events_search_vector', 'events', ['search_vector'], postgresql_using='gin')
|
||||
# Spatial index
|
||||
|
|
|
|||
Loading…
Reference in a new issue