From 203be870d547109cd8d3c149d368d7dfc7cd9e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= Date: Tue, 11 May 2021 11:29:26 +0200 Subject: [PATCH] logged errors: fix integrity error on tech_id (#53839) --- wcs/logged_errors.py | 3 --- wcs/sql.py | 13 ++++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/wcs/logged_errors.py b/wcs/logged_errors.py index 1dd7fb1a5..c6d9e2fde 100644 --- a/wcs/logged_errors.py +++ b/wcs/logged_errors.py @@ -87,9 +87,6 @@ class LoggedError: error.first_occurence_timestamp = now() error.tech_id = error.build_tech_id() - existing_errors = list(cls.get_with_indexed_value('tech_id', error.tech_id)) - if existing_errors: - error = existing_errors[0] error.occurences_count += 1 error.latest_occurence_timestamp = now() error.store() diff --git a/wcs/sql.py b/wcs/sql.py index 793af6686..c36a3b9c8 100644 --- a/wcs/sql.py +++ b/wcs/sql.py @@ -3005,7 +3005,18 @@ class LoggedError(SqlMixin, wcs.logged_errors.LoggedError): ', '.join(column_names), ', '.join(['%%(%s)s' % x for x in column_names]), ) - cur.execute(sql_statement, sql_dict) + try: + cur.execute(sql_statement, sql_dict) + except psycopg2.IntegrityError: + # tech_id already used ? + conn.rollback() + existing_errors = list(self.get_with_indexed_value('tech_id', self.tech_id)) + if not existing_errors: + raise + error = existing_errors[0] + error.occurences_count += 1 + error.latest_occurence_timestamp = self.latest_occurence_timestamp + return error.store() self.id = cur.fetchone()[0] else: column_names = sql_dict.keys()