Skip to content

Commit 8afa6f0

Browse files
authored
feat: PgvectorDocumentStore now supports delete_all_documents and delete_all_documents_async (#2394)
* feat: `PgVectorDocumentStore` now supports `delete_all_documents` and `delete_all_documents_async` * style: fix typo * test: Remove comments and verify I can write document after deletion
1 parent 5fa983a commit 8afa6f0

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,38 @@ async def delete_documents_async(self, document_ids: List[str]) -> None:
987987
error_msg="Could not delete documents from PgvectorDocumentStore",
988988
)
989989

990+
def delete_all_documents(self) -> None:
991+
"""
992+
Deletes all documents in the document store.
993+
"""
994+
query = SQL("TRUNCATE TABLE {schema_name}.{table_name}").format(
995+
schema_name=Identifier(self.schema_name),
996+
table_name=Identifier(self.table_name),
997+
)
998+
999+
self._ensure_db_setup()
1000+
assert self._cursor is not None
1001+
self._execute_sql(
1002+
cursor=self._cursor, sql_query=query, error_msg="Could not delete all documents from PgvectorDocumentStore"
1003+
)
1004+
1005+
async def delete_all_documents_async(self) -> None:
1006+
"""
1007+
Asynchronously deletes all documents in the document store.
1008+
"""
1009+
query = SQL("TRUNCATE TABLE {schema_name}.{table_name}").format(
1010+
schema_name=Identifier(self.schema_name),
1011+
table_name=Identifier(self.table_name),
1012+
)
1013+
1014+
await self._ensure_db_setup_async()
1015+
assert self._async_cursor is not None
1016+
await self._execute_sql_async(
1017+
cursor=self._async_cursor,
1018+
sql_query=query,
1019+
error_msg="Could not delete all documents from PgvectorDocumentStore",
1020+
)
1021+
9901022
def _build_keyword_retrieval_query(
9911023
self, query: str, top_k: int, filters: Optional[Dict[str, Any]] = None
9921024
) -> Tuple[Composed, tuple]:

integrations/pgvector/tests/test_document_store.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ def test_connection_check_and_recreation(self, document_store: PgvectorDocumentS
5353
same_connection = document_store._connection
5454
assert same_connection is document_store._connection
5555

56+
def test_delete_all_documents(self, document_store: PgvectorDocumentStore) -> None:
57+
document_store.write_documents([Document(id=str(i)) for i in range(10)])
58+
document_store.delete_all_documents()
59+
assert document_store.count_documents() == 0
60+
document_store.write_documents([Document(id="1")])
61+
assert document_store.count_documents() == 1
62+
5663

5764
@pytest.mark.usefixtures("patches_for_unit_tests")
5865
def test_init(monkeypatch):

integrations/pgvector/tests/test_document_store_async.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ async def test_connection_check_and_recreation(self, document_store: PgvectorDoc
9494
same_connection = document_store._async_connection
9595
assert same_connection is document_store._async_connection
9696

97+
async def test_delete_all_documents_async(self, document_store: PgvectorDocumentStore) -> None:
98+
document_store.write_documents([Document(id=str(i)) for i in range(10)])
99+
await document_store.delete_all_documents_async()
100+
assert document_store.count_documents() == 0
101+
document_store.write_documents([Document(id="1")])
102+
assert document_store.count_documents() == 1
103+
97104

98105
@pytest.mark.integration
99106
@pytest.mark.asyncio

0 commit comments

Comments
 (0)