Skip to content

Commit

Permalink
Introduce dev utils, add debug_sql() (#3113)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathjazz authored Feb 22, 2024
1 parent 9cf8902 commit 00c8852
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Empty file added pontoon/dev/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions pontoon/dev/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 00c8852

Please sign in to comment.