[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
This commit is contained in:
Romain Derie
2023-06-15 18:51:18 +02:00
parent 984bad4974
commit 202f79dec0
+12 -2
View File
@@ -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)