diff --git a/sqlalchemy_searchable/__init__.py b/sqlalchemy_searchable/__init__.py index 65e3a97..01fbccb 100644 --- a/sqlalchemy_searchable/__init__.py +++ b/sqlalchemy_searchable/__init__.py @@ -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 @@ -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, @@ -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 @@ -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, diff --git a/tests/test_sync_trigger.py b/tests/test_sync_trigger.py index ea785ef..ff5ee6d 100644 --- a/tests/test_sync_trigger.py +++ b/tests/test_sync_trigger.py @@ -35,7 +35,16 @@ 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 + ); """ ) ) @@ -43,7 +52,15 @@ def create_tables(self, engine): 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: @@ -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(