Skip to content

Commit

Permalink
feat: allow specifying schema in drop/sync trigger
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Carapetis <[email protected]>
  • Loading branch information
jpvanhal and acarapetis committed Feb 19, 2024
1 parent 603878f commit fe6969b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
31 changes: 27 additions & 4 deletions sqlalchemy_searchable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,13 @@ def attach_ddl_listeners(self):


def sync_trigger(
conn, table_name, tsvector_column, indexed_columns, metadata=None, options=None
conn,
table_name,
tsvector_column,
indexed_columns,
metadata=None,
options=None,
schema=None,
):
"""Synchronize the search trigger and trigger function for the given table and
search vector column. Internally, this function executes the following SQL
Expand Down Expand Up @@ -377,7 +383,12 @@ def hstore_vectorizer(column):
"""
if metadata is None:
metadata = sa.MetaData()
table = sa.Table(table_name, metadata, autoload_with=conn)
table = sa.Table(
table_name,
metadata,
autoload_with=conn,
schema=schema,
)
params = dict(
tsvector_column=getattr(table.c, tsvector_column),
indexed_columns=indexed_columns,
Expand All @@ -397,7 +408,14 @@ def hstore_vectorizer(column):
conn.execute(update_sql)


def drop_trigger(conn, table_name, tsvector_column, metadata=None, options=None):
def drop_trigger(
conn,
table_name,
tsvector_column,
metadata=None,
options=None,
schema=None,
):
"""
Drop the search trigger and trigger function for the given table and
search vector column. Internally, this function executes the following SQL
Expand Down Expand Up @@ -431,7 +449,12 @@ def downgrade():
"""
if metadata is None:
metadata = sa.MetaData()
table = sa.Table(table_name, metadata, autoload_with=conn)
table = sa.Table(
table_name,
metadata,
autoload_with=conn,
schema=schema,
)
params = dict(tsvector_column=getattr(table.c, tsvector_column), options=options)
classes = [
DropSearchTriggerSQL,
Expand Down
41 changes: 39 additions & 2 deletions tests/test_sync_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,32 @@ def create_tables(self, engine):
content TEXT,
"current_user" TEXT,
search_vector TSVECTOR
)
);
CREATE SCHEMA another;
CREATE TABLE another.article (
name TEXT,
content TEXT,
"current_user" TEXT,
search_vector TSVECTOR
);
"""
)
)

yield

with engine.begin() as conn:
conn.execute(text("DROP TABLE article"))
conn.execute(
text(
"""
DROP TABLE article;
DROP TABLE another.article;
DROP SCHEMA another;
"""
)
)

def test_creates_triggers_and_functions(self, engine, ts_vector_options):
with engine.begin() as conn:
Expand All @@ -63,6 +80,26 @@ def test_creates_triggers_and_functions(self, engine, ts_vector_options):
vector = conn.execute(text("SELECT search_vector FROM article")).scalar()
assert vector == "'content':4 'name':2"

def test_different_schema(self, engine):
with engine.begin() as conn:
sync_trigger(
conn,
"article",
"search_vector",
["name", "content"],
schema="another",
)
conn.execute(
text(
"""INSERT INTO another.article (name, content)
VALUES ('some name', 'some content')"""
)
)
vector = conn.execute(
text("SELECT search_vector FROM another.article")
).scalar()
assert vector == "'content':4 'name':2"

def test_updates_column_values(self, engine, ts_vector_options):
with engine.begin() as conn:
sync_trigger(
Expand Down

0 comments on commit fe6969b

Please sign in to comment.