Skip to content

feat: add drop table #195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions examples/pg_vectorstore.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@
"source": [
"await vectorstore.adrop_vector_index() # Drop index using default name"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Clean up\n",
"\n",
"**⚠️ WARNING: this can not be undone**\n",
"\n",
"Drop the vector store table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"await pg_engine.adrop_table(TABLE_NAME)"
]
}
],
"metadata": {
Expand Down
16 changes: 11 additions & 5 deletions examples/pg_vectorstore_how_to.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -691,12 +691,18 @@
"source": [
"## Clean up\n",
"\n",
"#### ⚠️ WARNING: this can not be undone\n",
"In a [`psql`](https://www.postgresql.org/docs/current/app-psql.html) client, run:\n",
"**⚠️ WARNING: this can not be undone**\n",
"\n",
"```\n",
"DROP TABLE <TABLE_NAME>;\n",
"```"
"Drop the vector store table."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"await pg_engine.adrop_table(TABLE_NAME)"
]
}
],
Expand Down
32 changes: 32 additions & 0 deletions langchain_postgres/v2/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,35 @@ def init_vectorstore_table(
store_metadata=store_metadata,
)
)

async def _adrop_table(
self,
table_name: str,
*,
schema_name: str = "public",
) -> None:
"""Drop the vector store table"""
query = f'DROP TABLE "{schema_name}"."{table_name}";'
async with self._pool.connect() as conn:
await conn.execute(text(query))
await conn.commit()

async def adrop_table(
self,
table_name: str,
*,
schema_name: str = "public",
) -> None:
await self._run_as_async(
self._adrop_table(table_name=table_name, schema_name=schema_name)
)

async def drop_table(
self,
table_name: str,
*,
schema_name: str = "public",
) -> None:
self._run_as_sync(
self._adrop_table(table_name=table_name, schema_name=schema_name)
)
4 changes: 2 additions & 2 deletions tests/unit_tests/v2/test_async_pg_vectorstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ async def engine(self) -> AsyncIterator[PGEngine]:
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)

yield engine
await aexecute(engine, f'DROP TABLE IF EXISTS "{DEFAULT_TABLE}"')
await aexecute(engine, f'DROP TABLE IF EXISTS "{CUSTOM_TABLE}"')
await engine.adrop_table(DEFAULT_TABLE)
await engine.adrop_table(CUSTOM_TABLE)
await engine.close()

@pytest_asyncio.fixture(scope="class")
Expand Down
6 changes: 3 additions & 3 deletions tests/unit_tests/v2/test_async_pg_vectorstore_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ class TestVectorStoreSearch:
async def engine(self) -> AsyncIterator[PGEngine]:
engine = PGEngine.from_connection_string(url=CONNECTION_STRING)
yield engine
await aexecute(engine, f"DROP TABLE IF EXISTS {DEFAULT_TABLE}")
await aexecute(engine, f"DROP TABLE IF EXISTS {CUSTOM_TABLE}")
await aexecute(engine, f"DROP TABLE IF EXISTS {CUSTOM_FILTER_TABLE}")
await engine.adrop_table(DEFAULT_TABLE)
await engine.adrop_table(CUSTOM_TABLE)
await engine.adrop_table(CUSTOM_FILTER_TABLE)
await engine.close()

@pytest_asyncio.fixture(scope="class")
Expand Down