@@ -97,9 +97,11 @@ def upload_archive(
9797 filename). If boolean, whether to force WIP status (True) or
9898 production status (False) regardless of what the filename says
9999
100+ :return: the number of modified rows
100101 """
101102 archive_as_successful , archive_as_failed = handlers
102-
103+ total_modified_row_count = 0
104+
103105 # iterate over each file
104106 for path , details in path_details :
105107 print ('handling ' , path )
@@ -127,18 +129,19 @@ def upload_archive(
127129 all_rows_valid = rows_list and all (r is not None for r in rows_list )
128130 if all_rows_valid :
129131 try :
130- result = database .insert_or_update_bulk (rows_list )
131- print (f"insert_or_update_bulk { filename } returned { result } " )
132+ modified_row_count = database .insert_or_update_bulk (rows_list )
133+ print (f"insert_or_update_bulk { filename } returned { modified_row_count } " )
132134 logger .info (
133135 "Inserted database rows" ,
134- row_count = result ,
136+ row_count = modified_row_count ,
135137 source = source ,
136138 signal = signal ,
137139 geo_type = geo_type ,
138140 time_value = time_value ,
139141 issue = issue ,
140142 lag = lag )
141- if result is None or result : # else would indicate zero rows inserted
143+ if modified_row_count is None or modified_row_count : # else would indicate zero rows inserted
144+ total_modified_row_count += (modified_row_count if modified_row_count else 0 )
142145 database .commit ()
143146 except Exception as e :
144147 all_rows_valid = False
@@ -150,6 +153,9 @@ def upload_archive(
150153 archive_as_successful (path_src , filename , source )
151154 else :
152155 archive_as_failed (path_src , filename , source )
156+
157+ return total_modified_row_count
158+
153159
154160def main (
155161 args ,
@@ -180,25 +186,19 @@ def main(
180186
181187 database = database_impl ()
182188 database .connect ()
183- num_starting_rows = database .count_all_rows ()
184189
185190 try :
186- upload_archive_impl (
191+ modified_row_count = upload_archive_impl (
187192 path_details ,
188193 database ,
189194 make_handlers (args .data_dir , args .specific_issue_date ),
190195 logger ,
191196 is_wip_override = wip_override )
197+ logger .info ("Finished inserting database rows" , row_count = modified_row_count )
198+ print ('inserted/updated %d rows' % modified_row_count )
192199 finally :
193- # no catch block so that an exception above will cause the program to fail
194- # after the following cleanup
195- try :
196- num_inserted_rows = database .count_all_rows () - num_starting_rows
197- logger .info ("Finished inserting database rows" , row_count = num_inserted_rows )
198- print ('inserted/updated %d rows' % num_inserted_rows )
199- finally :
200- # unconditionally commit database changes since CSVs have been archived
201- database .disconnect (True )
200+ # unconditionally commit database changes since CSVs have been archived
201+ database .disconnect (True )
202202
203203 logger .info (
204204 "Ingested CSVs into database" ,
0 commit comments