Skip to content

Instantly share code, notes, and snippets.

@mathjazz
Created September 3, 2019 08:30
Show Gist options
  • Save mathjazz/92d2f6a36c968e8c1f6496f5e5aa0318 to your computer and use it in GitHub Desktop.
Save mathjazz/92d2f6a36c968e8c1f6496f5e5aa0318 to your computer and use it in GitHub Desktop.
Debug DB calls
Leopold:pontoon mathjazz$ git diff pontoon/
diff --git a/pontoon/base/models.py b/pontoon/base/models.py
index 0627cc95..ca5cf160 100644
--- a/pontoon/base/models.py
+++ b/pontoon/base/models.py
@@ -2846,8 +2846,34 @@ class Translation(DirtyFieldsMixin, models.Model):
locale=self.locale
)
+ 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)
+
# Update latest translation where necessary
- self.update_latest_translation()
+ with debug_sql():
+ self.update_latest_translation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment