Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhances Signals table #3898

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Adds search vector column to the signal instance model

Revision ID: f2605bfc1f59
Revises: 064c71206256
Create Date: 2023-10-23 11:15:53.297563

"""
from alembic import op
import sqlalchemy as sa
import sqlalchemy_utils

# revision identifiers, used by Alembic.
revision = "f2605bfc1f59"
down_revision = "064c71206256"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"signal_instance",
sa.Column("search_vector", sqlalchemy_utils.types.ts_vector.TSVectorType(), nullable=True),
)
op.create_index(
"signal_instance_search_vector_idx",
"signal_instance",
["search_vector"],
unique=False,
postgresql_using="gin",
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"signal_instance_search_vector_idx", table_name="signal_instance", postgresql_using="gin"
)
op.drop_column("signal_instance", "search_vector")
# ### end Alembic commands ###
3 changes: 2 additions & 1 deletion src/dispatch/database/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ def composite_search(*, db_session, query_str: str, models: List[Base], current_

# TODO can we do this with composite filtering?
# for model in models:
# query = apply_model_specific_filters(model, query, current_user)
# query = apply_model_specific_filters(model, query, current_user)

return s.search(query=query)

Expand Down Expand Up @@ -497,6 +497,7 @@ def search_filter_sort_paginate(
):
"""Common functionality for searching, filtering, sorting, and pagination."""
model_cls = get_class_by_tablename(model)

try:
query = db_session.query(model_cls)

Expand Down
18 changes: 15 additions & 3 deletions src/dispatch/signal/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

from pydantic import Field
from sqlalchemy import (
JSON,
select,
Boolean,
Column,
DateTime,
ForeignKey,
Integer,
JSON,
PrimaryKeyConstraint,
String,
Table,
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, column_property
from sqlalchemy_utils import TSVectorType

from dispatch.auth.models import DispatchUser
Expand Down Expand Up @@ -236,8 +237,19 @@ class SignalInstance(Base, TimeStampMixin, ProjectMixin):
signal = relationship("Signal", backref="instances")
signal_id = Column(Integer, ForeignKey("signal.id"))

signal_name = column_property(select([Signal.name]).where(Signal.id == signal_id))
signal_description = column_property(select([Signal.description]).where(Signal.id == signal_id))

search_vector = Column(
TSVectorType(
"signal_name",
"signal_description",
weights={"signal_name": "A", "signal_description": "B"},
)
)


# Pydantic models...
# Pydantic models
class Service(DispatchBase):
id: PrimaryKey
description: Optional[str] = Field(None, nullable=True)
Expand Down
Loading