From 00c8852fa3022c696da2701df287a415a554ab25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Thu, 22 Feb 2024 21:24:12 +0100 Subject: [PATCH] Introduce dev utils, add debug_sql() (#3113) --- pontoon/dev/__init__.py | 0 pontoon/dev/utils.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 pontoon/dev/__init__.py create mode 100644 pontoon/dev/utils.py diff --git a/pontoon/dev/__init__.py b/pontoon/dev/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/pontoon/dev/utils.py b/pontoon/dev/utils.py new file mode 100644 index 0000000000..52ecbaab26 --- /dev/null +++ b/pontoon/dev/utils.py @@ -0,0 +1,34 @@ +""" +Author: phlax + +Usage: +with debug_sql(): + code_with_some_db_action() +""" + +import logging +from contextlib import contextmanager +from django.db import connection + + +log = logging.getLogger(__name__) + + +def log_new_queries(queries): + new_queries = list(connection.queries[queries:]) + + for query in new_queries: + log.debug(query["time"]) + log.debug("\t%s", query["sql"]) + + log.debug("total db calls: %s", len(new_queries)) + + +@contextmanager +def debug_sql(): + queries = len(connection.queries) + + try: + yield + finally: + log_new_queries(queries)