Skip to content

Commit bf51d2c

Browse files
lidavidmclaude
andauthored
refactor: extract some utilities (#161)
Closes #160. Co-authored-by: Claude <noreply@anthropic.com>
1 parent 27fd351 commit bf51d2c

5 files changed

Lines changed: 113 additions & 151 deletions

File tree

adbc_drivers_validation/model.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,51 @@ def drop_table(
261261
else:
262262
return f"DROP TABLE {name}"
263263

264+
def try_drop_table(
265+
self,
266+
cursor: adbc_driver_manager.dbapi.Cursor,
267+
*,
268+
table_name: str,
269+
schema_name: str | None = None,
270+
catalog_name: str | None = None,
271+
temporary: bool = False,
272+
) -> None:
273+
"""
274+
Try to drop a table, ignoring errors if the table does not exist.
275+
276+
Some databases have no way to do DROP IF EXISTS, so this method
277+
attempts to drop the table and catches errors that indicate the
278+
table was not found.
279+
280+
Parameters
281+
----------
282+
cursor : adbc_driver_manager.dbapi.Cursor
283+
The cursor to execute the query.
284+
table_name : str
285+
The name of the table to drop.
286+
schema_name : str, optional
287+
The schema containing the table (if not given, assume current
288+
schema).
289+
catalog_name : str, optional
290+
The catalog containing the table (if not given, assume current
291+
catalog).
292+
temporary : bool
293+
If True, the table is a temporary table.
294+
"""
295+
try:
296+
cursor.execute(
297+
self.drop_table(
298+
table_name=table_name,
299+
schema_name=schema_name,
300+
catalog_name=catalog_name,
301+
temporary=temporary,
302+
)
303+
)
304+
except adbc_driver_manager.Error as e:
305+
# Some databases have no way to do DROP IF EXISTS
306+
if not self.is_table_not_found(table_name=table_name, error=e):
307+
raise
308+
264309
@abc.abstractmethod
265310
def is_table_not_found(self, table_name: str | None, error: Exception) -> bool:
266311
"""

adbc_drivers_validation/tests/connection.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,7 @@ def test_get_objects_table_not_exist(
266266
# parts of it
267267
table_name = "getobjectstest2"
268268
with conn.cursor() as cursor:
269-
try:
270-
cursor.execute(driver.drop_table(table_name=table_name))
271-
except adbc_driver_manager.Error as e:
272-
if not driver.is_table_not_found(table_name=table_name, error=e):
273-
raise
269+
driver.try_drop_table(cursor, table_name=table_name)
274270

275271
objects = conn.adbc_get_objects(depth="tables").read_all().to_pylist()
276272
tables = [
@@ -688,24 +684,14 @@ def get_objects_table(
688684
table_name,
689685
)
690686
with conn.cursor() as cursor:
691-
try:
692-
cursor.execute(driver.drop_table(table_name=table_name))
693-
except adbc_driver_manager.Error as e:
694-
# Some databases have no way to do DROP IF EXISTS
695-
if not driver.is_table_not_found(table_name=None, error=e):
696-
raise
687+
driver.try_drop_table(cursor, table_name=table_name)
697688

698689
cursor.adbc_ingest(table_name, data)
699690

700691
yield table_id
701692

702693
with conn.cursor() as cursor:
703-
try:
704-
cursor.execute(driver.drop_table(table_name=table_name))
705-
except adbc_driver_manager.Error as e:
706-
# Some databases have no way to do DROP IF EXISTS
707-
if not driver.is_table_not_found(table_name=None, error=e):
708-
raise
694+
driver.try_drop_table(cursor, table_name=table_name)
709695

710696
@pytest.fixture(scope="class")
711697
def get_objects_constraints(

0 commit comments

Comments
 (0)