Skip to content

Commit 4b9768f

Browse files
authored
Merge pull request #418 from benjaminysmith/ingestion_logging
Retrieve the modified DB row count using cursor.rowcount
2 parents 886e363 + 2f11bb5 commit 4b9768f

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/acquisition/covidcast/database.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,17 +212,18 @@ def insert_or_update_batch(self, cc_rows, batch_size=2**20, commit_partial=False
212212
) for row in cc_rows[start:end]]
213213

214214

215-
result = self._cursor.executemany(insert_into_tmp_sql, args)
215+
self._cursor.executemany(insert_into_tmp_sql, args)
216216
self._cursor.execute(insert_or_update_sql)
217+
modified_row_count = self._cursor.rowcount
217218
self._cursor.execute(zero_is_latest_issue_sql)
218219
self._cursor.execute(set_is_latest_issue_sql)
219220
self._cursor.execute(truncate_tmp_table_sql)
220221

221-
if result is None:
222-
# the SQL connector does not support returning number of rows affected
222+
if modified_row_count is None or modified_row_count == -1:
223+
# the SQL connector does not support returning number of rows affected (see PEP 249)
223224
total = None
224225
else:
225-
total += result
226+
total += modified_row_count
226227
if commit_partial:
227228
self._connection.commit()
228229
except Exception as e:

tests/acquisition/covidcast/test_database.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,29 @@ def test_insert_or_update_batch_exception_reraised(self):
111111

112112
cc_rows = {MagicMock(geo_id='CA', val=1, se=0, sample_size=0)}
113113
self.assertRaises(Exception, database.insert_or_update_batch, cc_rows)
114+
115+
def test_insert_or_update_batch_row_count_returned(self):
116+
"""Test that the row count is returned"""
117+
mock_connector = MagicMock()
118+
database = Database()
119+
database.connect(connector_impl=mock_connector)
120+
connection = mock_connector.connect()
121+
cursor = connection.cursor()
122+
cursor.rowcount = 3
123+
124+
cc_rows = [MagicMock(geo_id='CA', val=1, se=0, sample_size=0)]
125+
result = database.insert_or_update_batch(cc_rows)
126+
self.assertEqual(result, 3)
127+
128+
def test_insert_or_update_batch_none_returned(self):
129+
"""Test that None is returned when row count cannot be returned"""
130+
mock_connector = MagicMock()
131+
database = Database()
132+
database.connect(connector_impl=mock_connector)
133+
connection = mock_connector.connect()
134+
cursor = connection.cursor()
135+
cursor.rowcount = -1
136+
137+
cc_rows = [MagicMock(geo_id='CA', val=1, se=0, sample_size=0)]
138+
result = database.insert_or_update_batch(cc_rows)
139+
self.assertIsNone(result)

0 commit comments

Comments
 (0)