From 7d9c9e7eb598a0f9623250131e56382a9d7b885a Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Mon, 27 Apr 2020 18:01:30 +0100 Subject: [PATCH] UPSERT when creating timers --- fcp_timers.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/fcp_timers.py b/fcp_timers.py index 7a6e534..c671989 100644 --- a/fcp_timers.py +++ b/fcp_timers.py @@ -95,8 +95,15 @@ def _db_save_timer(self, timestamp: datetime, proposal_num: int): with self.store.conn: self.store.cur.execute(""" INSERT INTO fcp_timers (proposal_num, end_timestamp) - VALUES (%s, %s) - """, (proposal_num, timestamp.timestamp())) + VALUES (%s, %s) + ON CONFLICT (proposal_num) + DO UPDATE + SET end_timestamp = %s + WHERE fcp_timers.proposal_num = %s + """, ( + proposal_num, timestamp.timestamp(), + proposal_num, timestamp.timestamp(), + )) def _db_delete_timer(self, proposal_num: int): """Deletes a timer from the database"""