Skip to content

Commit b007d77

Browse files
authored
Merge pull request #863 from cmu-delphi/krivard/fix_delete_batch
Fix bugs identified in staging tests
2 parents e26e3f3 + 5d8ddde commit b007d77

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/acquisition/covidcast/database.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ def delete_batch(self, cc_deletions):
326326
'''
327327

328328
drop_tmp_table_sql = f'DROP TABLE {tmp_table_name}'
329+
total = None
329330
try:
330331
self._cursor.execute(create_tmp_table_sql)
331332
self._cursor.execute(amend_tmp_table_sql)
@@ -337,12 +338,18 @@ def delete_batch(self, cc_deletions):
337338
raise Exception(f"Bad deletions argument: need a filename or a list of tuples; got a {type(cc_deletions)}")
338339
self._cursor.execute(add_id_sql)
339340
self._cursor.execute(delete_sql)
341+
total = self._cursor.rowcount
340342
self._cursor.execute(fix_latest_issue_sql)
341343
self._connection.commit()
344+
345+
if total == -1:
346+
# the SQL connector does not support returning number of rows affected (see PEP 249)
347+
total = None
342348
except Exception as e:
343349
raise e
344350
finally:
345351
self._cursor.execute(drop_tmp_table_sql)
352+
return total
346353

347354
def compute_covidcast_meta(self, table_name='covidcast', use_index=True):
348355
"""Compute and return metadata on all non-WIP COVIDcast signals."""

src/acquisition/covidcast/delete_batch.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# standard library
44
import argparse
5+
import glob
56
import os
67
import time
78

@@ -22,19 +23,21 @@ def get_argument_parser():
2223
help="filename for log output (defaults to stdout)")
2324
return parser
2425

25-
def handle_file(deletion_file, database):
26+
def handle_file(deletion_file, database, logger):
2627
logger.info("Deleting from csv file", filename=deletion_file)
2728
rows = []
2829
with open(deletion_file) as f:
2930
for line in f:
30-
rows.append(line.strip().split(","))
31+
fields = line.strip().split(",")
32+
if len(fields) < 9: continue
33+
rows.append(fields + ["day"])
3134
rows = rows[1:]
3235
try:
3336
n = database.delete_batch(rows)
3437
logger.info("Deleted database rows", row_count=n)
3538
return n
3639
except Exception as e:
37-
logger.exception('Exception while deleting rows:', e)
40+
logger.exception('Exception while deleting rows', exception=e)
3841
database.rollback()
3942
return 0
4043

@@ -49,7 +52,11 @@ def main(args):
4952

5053
try:
5154
for deletion_file in sorted(glob.glob(os.path.join(args.deletion_dir, '*.csv'))):
52-
all_n += handle_file(deletion_file)
55+
n = handle_file(deletion_file, database, logger)
56+
if n is not None:
57+
all_n += n
58+
else:
59+
all_n = "rowcount unsupported"
5360
finally:
5461
database.disconnect(True)
5562

0 commit comments

Comments
 (0)