From 202f79dec0d866ae46ebd276da7301067a719425 Mon Sep 17 00:00:00 2001 From: Romain Derie Date: Thu, 20 Apr 2023 10:22:20 +0000 Subject: [PATCH] [IMP] sql_db: allow to track queried table on cursor outside --log-sql This commit introduced a way to log queried table on cursor without having to be in `--log-sql` mode. It will be used in next commit to add complete performances tests which will ensure the correct tables are accessed when a page is requested. This is useful as right now those performances tests are only doing a check against the SQL query number without ensuring the queries are the correct ones. As a result, one could introduce an improvement which would reduce the queries (-2) but break another previous improvement (-2) which would add back those queries (+2). Since the query count would be the same (-2+2), it would be undetected, instead of being fixed directly and keep both improvements (-2-2). See next commit for concrete example(s) and use of this improvement. X-original-commit: 8ef6137a34aa35182399a2bd4b20dd57479ab2e7 Part-of: odoo/odoo#124939 --- odoo/sql_db.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/odoo/sql_db.py b/odoo/sql_db.py index 232cc8c846f..31299df03bd 100644 --- a/odoo/sql_db.py +++ b/odoo/sql_db.py @@ -246,6 +246,7 @@ class Cursor(BaseCursor): # default log level determined at cursor creation, could be # overridden later for debugging purposes self.sql_log_count = 0 + self._sql_table_tracking = False # avoid the call of close() (by __del__) if an exception # is raised by any of the following initializations @@ -331,8 +332,8 @@ class Cursor(BaseCursor): for hook in getattr(current_thread, 'query_hooks', ()): hook(self, query, params, start, delay) - # advanced stats only if logging.DEBUG is enabled - if _logger.isEnabledFor(logging.DEBUG): + # advanced stats + if _logger.isEnabledFor(logging.DEBUG) or self._sql_table_tracking: delay *= 1E6 decoded_query = self._obj.query.decode() @@ -392,6 +393,15 @@ class Cursor(BaseCursor): finally: _logger.setLevel(level) + @contextmanager + def _enable_table_tracking(self): + try: + old = self._sql_table_tracking + self._sql_table_tracking = True + yield + finally: + self._sql_table_tracking = old + def close(self): if not self.closed: return self._close(False)