-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce dev utils, add debug_sql() (#3113)
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |