From 2d25c263e98554e481920cef04cf76cb15e68053 Mon Sep 17 00:00:00 2001 From: Chang She <759245+changhiskhan@users.noreply.github.com> Date: Mon, 31 Jul 2023 10:25:09 +0200 Subject: [PATCH] Implement drop table if exists (#383) --- python/lancedb/db.py | 14 ++++++++++---- python/tests/test_db.py | 4 ++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/python/lancedb/db.py b/python/lancedb/db.py index d8063442..85564d3a 100644 --- a/python/lancedb/db.py +++ b/python/lancedb/db.py @@ -319,14 +319,20 @@ class LanceDBConnection(DBConnection): """ return LanceTable.open(self, name) - def drop_table(self, name: str): + def drop_table(self, name: str, ignore_missing: bool = False): """Drop a table from the database. Parameters ---------- name: str The name of the table. + ignore_missing: bool, default False + If True, ignore if the table does not exist. """ - filesystem, path = fs_from_uri(self.uri) - table_path = os.path.join(path, name + ".lance") - filesystem.delete_dir(table_path) + try: + filesystem, path = fs_from_uri(self.uri) + table_path = os.path.join(path, name + ".lance") + filesystem.delete_dir(table_path) + except FileNotFoundError: + if not ignore_missing: + raise diff --git a/python/tests/test_db.py b/python/tests/test_db.py index 96ef2dfd..d3173a76 100644 --- a/python/tests/test_db.py +++ b/python/tests/test_db.py @@ -149,6 +149,10 @@ def test_delete_table(tmp_path): db.create_table("test", data=data) assert db.table_names() == ["test"] + # dropping a table that does not exist should pass + # if ignore_missing=True + db.drop_table("does_not_exist", ignore_missing=True) + def test_empty_or_nonexistent_table(tmp_path): db = lancedb.connect(tmp_path)