Skip to content

Commit 34e56c3

Browse files
committed
added ON DUPLICATE KEY UPDATE clause
1 parent 2099ab7 commit 34e56c3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/acquisition/covid_hosp/common/database.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,13 @@ def nan_safe_dtype(dtype, value):
186186

187187
num_columns = 2 + len(dataframe_columns_and_types) + len(self.additional_fields)
188188
value_placeholders = ', '.join(['%s'] * num_columns)
189-
columns = ', '.join(f'`{i.sql_name}`' for i in dataframe_columns_and_types + self.additional_fields)
189+
col_names = [f'`{i.sql_name}`' for i in dataframe_columns_and_types + self.additional_fields]
190+
columns = ', '.join(col_names)
191+
updates = ', '.join(f'{c}=new_values.{c}' for c in col_names)
192+
# NOTE: list in `updates` presumes `publication_col_name` is part of the unique key and thus not needed in UPDATE
190193
sql = f'INSERT INTO `{self.table_name}` (`id`, `{self.publication_col_name}`, {columns}) ' \
191-
f'VALUES ({value_placeholders})' # TODO: add ON DUPLICATE KEY UPDATE here for when we need to backfill yesterday's issue
194+
f'VALUES ({value_placeholders}) AS new_values ' \
195+
f'ON DUPLICATE KEY UPDATE {updates}'
192196
id_and_publication_date = (0, publication_date)
193197
if logger:
194198
logger.info('updating values', count=len(dataframe.index))

0 commit comments

Comments
 (0)