From 587f19eb75b72c1f40f876c79e68251b1e13135f Mon Sep 17 00:00:00 2001 From: Wolfgang Schnerring Date: Thu, 26 Sep 2024 15:27:20 +0200 Subject: [PATCH] WCM-22, WCM-393: Extract "suppress errors during sql query" into single function This reduces code duplication, and also allows zeit.web to use this as a hook to inject "skip if featuretoggle is off" behaviour --- core/src/zeit/connector/postgresql.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/core/src/zeit/connector/postgresql.py b/core/src/zeit/connector/postgresql.py index 906c086e98..4f71cac2c8 100644 --- a/core/src/zeit/connector/postgresql.py +++ b/core/src/zeit/connector/postgresql.py @@ -589,11 +589,8 @@ def _search_dav(self, attrlist, expr): def search_sql(self, query): result = [] - try: - rows = self.session.execute(query).scalars() - except Exception: - log.warning('Error during search_sql, suppressed', exc_info=True) - self.session.rollback() + rows = self._execute_suppress_errors(query) + if rows is None: return result for content in rows: @@ -613,12 +610,18 @@ def search_sql(self, query): return result def search_sql_count(self, query): + rows = self._execute_suppress_errors(query.with_only_columns(sqlalchemy.func.count())) + if rows is None: + return 0 + return rows.one() + + def _execute_suppress_errors(self, query): try: - return self.session.execute(query.with_only_columns(sqlalchemy.func.count())).scalar() + return self.session.execute(query).scalars() except Exception: - log.warning('Error during search_sql_count, suppressed', exc_info=True) + log.warning('Error during search_sql, suppressed', exc_info=True) self.session.rollback() - return 0 + return None def query(self): return select(Content)