Skip to content

Commit 08cdcab

Browse files
authored
fix: Pgvector - ensure DB is initialized when calling delete_table (#2055)
* fix: delete table first call, ensure_db_setup add and covered with test * chore: remove unnecessary file * fix: move written test to test_document_store and test_document_store_async * fix: comment line
1 parent f9ee241 commit 08cdcab

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

integrations/pgvector/src/haystack_integrations/document_stores/pgvector/document_store.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ def delete_table(self):
534534
The name of the schema (`schema_name`) and the name of the table (`table_name`)
535535
are defined when initializing the `PgvectorDocumentStore`.
536536
"""
537+
self._ensure_db_setup()
537538
delete_sql = SQL("DROP TABLE IF EXISTS {schema_name}.{table_name}").format(
538539
schema_name=Identifier(self.schema_name),
539540
table_name=Identifier(self.table_name),
@@ -551,6 +552,7 @@ async def delete_table_async(self):
551552
"""
552553
Async method to delete the table used to store Haystack documents.
553554
"""
555+
await self._ensure_db_setup_async()
554556
delete_sql = SQL("DROP TABLE IF EXISTS {schema_name}.{table_name}").format(
555557
schema_name=Identifier(self.schema_name),
556558
table_name=Identifier(self.table_name),

integrations/pgvector/tests/test_document_store.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,12 @@ def get_table_oid(document_store, schema_name, table_name):
237237
# Clean up: drop the schema after the test
238238
with psycopg.connect(connection_string, autocommit=True) as conn:
239239
conn.execute(f"DROP SCHEMA IF EXISTS {schema_name} CASCADE")
240+
241+
242+
@pytest.mark.integration
243+
def test_delete_table_first_call(document_store):
244+
"""
245+
Test that delete_table can be executed as the initial operation on the Document Store
246+
without triggering errors due to an uninitialized state.
247+
"""
248+
document_store.delete_table() # if throw error, test fails

integrations/pgvector/tests/test_document_store_async.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,13 @@ async def get_table_oid(cursor, document_store, schema_name, table_name):
206206
# Clean up: drop the schema after the test
207207
async with await psycopg.AsyncConnection.connect(connection_string, autocommit=True) as conn:
208208
await conn.execute(f"DROP SCHEMA IF EXISTS {schema_name} CASCADE")
209+
210+
211+
@pytest.mark.integration
212+
@pytest.mark.asyncio
213+
async def test_delete_table_async_first_call(document_store):
214+
"""
215+
Test that delete_table_async can be executed as the initial operation on the Document Store
216+
without triggering errors due to an uninitialized state.
217+
"""
218+
await document_store.delete_table_async() # if throw error, test fails

0 commit comments

Comments
 (0)