-
-
Save alirezashojaei/55e087b525f82cf8b14621195aba9589 to your computer and use it in GitHub Desktop.
A handy SQL debug function for Flask-SQLAlchemy
This file contains hidden or 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
from . import app | |
from flask.ext.sqlalchemy import get_debug_queries | |
if app.debug: | |
app.after_request(sql_debug) | |
def sql_debug(response): | |
queries = list(get_debug_queries()) | |
query_str = '' | |
total_duration = 0.0 | |
if queries: | |
for q in queries: | |
total_duration += q.duration | |
statement = str(q.statement).replace('?', '%s') | |
stmt = f"{statement} {q.parameters}".replace('\n', '\n ') | |
query_str += 'Query: {0}\nDuration: {1}ms\n\n'.format(stmt, round(q.duration * 1000, 2)) | |
print('\n' + '=' * 80) | |
print(' SQL Queries - {0} Queries Executed in {1}ms'.format(len(queries), round(total_duration * 1000, 2))) | |
print('=' * 80) | |
print(query_str.rstrip('\n')) | |
print('=' * 80 + '\n\n') | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment