Skip to content

Commit 164810f

Browse files
authored
feat: add drop table (#195)
1 parent 12a11d7 commit 164810f

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

examples/pg_vectorstore.ipynb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,26 @@
493493
"source": [
494494
"await vectorstore.adrop_vector_index() # Drop index using default name"
495495
]
496+
},
497+
{
498+
"cell_type": "markdown",
499+
"metadata": {},
500+
"source": [
501+
"## Clean up\n",
502+
"\n",
503+
"**⚠️ WARNING: this can not be undone**\n",
504+
"\n",
505+
"Drop the vector store table."
506+
]
507+
},
508+
{
509+
"cell_type": "code",
510+
"execution_count": null,
511+
"metadata": {},
512+
"outputs": [],
513+
"source": [
514+
"await pg_engine.adrop_table(TABLE_NAME)"
515+
]
496516
}
497517
],
498518
"metadata": {

examples/pg_vectorstore_how_to.ipynb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -691,12 +691,18 @@
691691
"source": [
692692
"## Clean up\n",
693693
"\n",
694-
"#### ⚠️ WARNING: this can not be undone\n",
695-
"In a [`psql`](https://www.postgresql.org/docs/current/app-psql.html) client, run:\n",
694+
"**⚠️ WARNING: this can not be undone**\n",
696695
"\n",
697-
"```\n",
698-
"DROP TABLE <TABLE_NAME>;\n",
699-
"```"
696+
"Drop the vector store table."
697+
]
698+
},
699+
{
700+
"cell_type": "code",
701+
"execution_count": null,
702+
"metadata": {},
703+
"outputs": [],
704+
"source": [
705+
"await pg_engine.adrop_table(TABLE_NAME)"
700706
]
701707
}
702708
],

langchain_postgres/v2/engine.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,35 @@ def init_vectorstore_table(
346346
store_metadata=store_metadata,
347347
)
348348
)
349+
350+
async def _adrop_table(
351+
self,
352+
table_name: str,
353+
*,
354+
schema_name: str = "public",
355+
) -> None:
356+
"""Drop the vector store table"""
357+
query = f'DROP TABLE "{schema_name}"."{table_name}";'
358+
async with self._pool.connect() as conn:
359+
await conn.execute(text(query))
360+
await conn.commit()
361+
362+
async def adrop_table(
363+
self,
364+
table_name: str,
365+
*,
366+
schema_name: str = "public",
367+
) -> None:
368+
await self._run_as_async(
369+
self._adrop_table(table_name=table_name, schema_name=schema_name)
370+
)
371+
372+
async def drop_table(
373+
self,
374+
table_name: str,
375+
*,
376+
schema_name: str = "public",
377+
) -> None:
378+
self._run_as_sync(
379+
self._adrop_table(table_name=table_name, schema_name=schema_name)
380+
)

tests/unit_tests/v2/test_async_pg_vectorstore.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ async def engine(self) -> AsyncIterator[PGEngine]:
5050
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
5151

5252
yield engine
53-
await aexecute(engine, f'DROP TABLE IF EXISTS "{DEFAULT_TABLE}"')
54-
await aexecute(engine, f'DROP TABLE IF EXISTS "{CUSTOM_TABLE}"')
53+
await engine.adrop_table(DEFAULT_TABLE)
54+
await engine.adrop_table(CUSTOM_TABLE)
5555
await engine.close()
5656

5757
@pytest_asyncio.fixture(scope="class")

tests/unit_tests/v2/test_async_pg_vectorstore_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class TestVectorStoreSearch:
6666
async def engine(self) -> AsyncIterator[PGEngine]:
6767
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
6868
yield engine
69-
await aexecute(engine, f"DROP TABLE IF EXISTS {DEFAULT_TABLE}")
70-
await aexecute(engine, f"DROP TABLE IF EXISTS {CUSTOM_TABLE}")
71-
await aexecute(engine, f"DROP TABLE IF EXISTS {CUSTOM_FILTER_TABLE}")
69+
await engine.adrop_table(DEFAULT_TABLE)
70+
await engine.adrop_table(CUSTOM_TABLE)
71+
await engine.adrop_table(CUSTOM_FILTER_TABLE)
7272
await engine.close()
7373

7474
@pytest_asyncio.fixture(scope="class")

0 commit comments

Comments
 (0)