Skip to content

Commit b24058b

Browse files
committed
no string injections!!!:D
1 parent 1d8b8c0 commit b24058b

1 file changed

Lines changed: 19 additions & 17 deletions

File tree

src/datasync/pit_registering_salmon.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -320,42 +320,44 @@ def check_table_has_data(db_path: str, table_name: str, dataset_name: str) -> bo
320320
Args:
321321
db_path: Path to the DuckDB database file
322322
table_name: Name of the table to check
323+
dataset_name: Name of the schema/dataset
323324
324325
Returns:
325326
bool: True if table exists and has at least one row, False otherwise
326327
"""
327328
try:
328329
conn = duckdb.connect(db_path, read_only=True)
329330
try:
330-
# check if table exists
331-
print(f"Describing table {table_name}")
332-
result = conn.execute(f"DESCRIBE {dataset_name}.{table_name}").fetchone() # noqa: S608
333-
334-
print(result)
335-
336-
log.info(f"Checking table {table_name} existence")
331+
log.info(f"Checking table {table_name} existence in schema {dataset_name}")
332+
333+
check_table_exists = """
334+
SELECT table_name
335+
FROM information_schema.tables
336+
WHERE table_schema = ? AND table_name = ?
337+
"""
338+
result = conn.execute(
339+
check_table_exists, [dataset_name, table_name]
340+
).fetchone()
337341

338342
if result is None:
339-
log.info(f"Table {table_name} does not exist")
343+
log.info(f"Table {table_name} does not exist in schema {dataset_name}")
340344
return False
341345

342-
# check if table has data
343-
count_result = conn.execute(
344-
f"SELECT COUNT(*) FROM {dataset_name}.{table_name}" # noqa: S608
345-
).fetchone()
346+
qualified_table = f"{dataset_name}.{table_name}"
347+
table_relation = conn.table(qualified_table)
348+
count_result = table_relation.count("*").fetchone()
349+
346350
row_count = count_result[0] if count_result else 0
347351

348352
log.info(f"Table {table_name} has {row_count} rows")
349353
return row_count > 0
350-
except Exception as e:
351-
raise Exception(f"Error checking table {table_name}") from e
354+
352355
finally:
353356
conn.close()
354357

355358
except Exception as e:
356-
log.error(f"Error with database: {e}")
357-
358-
return False
359+
log.error(f"Error checking table {table_name}: {e}")
360+
return False
359361

360362

361363
@app.command()

0 commit comments

Comments
 (0)