Released a pluggable application "django-sql-stacktrace"
Читать по-русски
This application is puts python stack trace to a SQL-query as comment. It’s useful when you is debugging your project, when you see many SQL-queries and do not understand where these queries were launched.
PyPI: django-sql-stacktrace
For example, you have come to a new project and you have been asked to deal with a lot of SQL-queries that hang at run time. You look at the “processlist” and see a lot of different SQL-queries:
...
SELECT COUNT(*) FROM `films_film` INNER JOIN `films_year` ON (`films_film`.`year_id` =
`films_year`.`id`) WHERE (`films_film`.`status` = 'published' AND `films_year`.`id` IS NOT NULL);
SELECT `blogs_post`.`id`, `blogs_post`.`blog_id`, `blogs_post`.`title`, `blogs_post`.`slug`,
`blogs_post`.`status`, `blogs_post`.`created`, `blogs_post`.`updated`,
`blogs_post`.`published`, `blogs_post`.`description`, `blogs_post`.`thumbnail`,
`blogs_post`.`content`, `blogs_post`.`count_of_views`, `blogs_post`.`source_content`,
`blogs_post`.`source_published`, `blogs_post`.`source_count_views`,
`blogs_post`.`source_author_of_content`, `blogs_post`.`source_author_of_photo`,
`blogs_post`.`source_keywords`, `blogs_post`.`source_url` FROM `blogs_post` WHERE
`blogs_post`.`status` = 'published' ORDER BY `blogs_post`.`published` DESC LIMIT 4;
SELECT `social_auth_usersocialauth`.`id`, `social_auth_usersocialauth`.`user_id`,
`social_auth_usersocialauth`.`provider`, `social_auth_usersocialauth`.`uid`,
`social_auth_usersocialauth`.`extra_data` FROM `social_auth_usersocialauth` WHERE
`social_auth_usersocialauth`.`user_id` = 42;
SELECT `films_film`.`id`, `films_film`.`status`, `films_film`.`created`, `films_film`.`updated`,
`films_film`.`published`, `films_film`.`title_original`, `films_film`.`title_ru`, `films_film`.`slug`,
`films_film`.`thumbnail`, `films_film`.`slogan`, `films_film`.`description`,
`films_film`.`date_premiere`, `films_film`.`year_id`, `films_film`.`rating_votes`,
`films_film`.`rating_score`, `films_film`.`rating_score_with_vote_weight`, `films_year`.`id`,
`films_year`.`name`, `films_year`.`count`, `films_year`.`size` FROM `films_film` LEFT OUTER
JOIN `films_year` ON (`films_film`.`year_id` = `films_year`.`id`) WHERE (`films_film`.`status`
= 'published' AND `films_year`.`id` IS NOT NULL) ORDER BY `films_film`.`published` DESC
LIMIT 30;
...
Where did they run you do not know, moreover, it is not only the queries from views, it may also be sql-queries from the django commands, something daemons, etc.
No problem! You take django-sql-stacktrace, run on a local copy of the project and see the following:
...
SELECT COUNT(*) FROM `films_film` INNER JOIN `films_year` ON (`films_film`.`year_id` = `films_year`.`id`)
WHERE (`films_film`.`status` = 'published' AND `films_year`.`id` IS NOT NULL)
/* STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/kinsburg_tv/common/views.py", line 27, in get_context_data
> context = super(MainPage, self).get_context_data(**kwargs)
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/kinsburg_tv/films/mixins.py", line 14, in get_context_data
> context = super(FilmsMixin, self).get_context_data(**kwargs)
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/django/db/models/query.py", line 351, in count
> return self.query.get_count(using=self.db)
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 418, in get_count
> number = obj.get_aggregation(using=using)[None]
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 384, in get_aggregation
> result = query.get_compiler(using).execute_sql(SINGLE)
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
> cursor.execute(sql, params)
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/sqlstacktrace/stacktracecursor.py", line 16, in execute
> stacks = get_stacktrace()
STACKTRACE:
> File "/home/adw0rd/work/kinsburg_tv/venv/local/lib/python2.7/site-packages/sqlstacktrace/stacktrace.py", line 93, in get_stacktrace
> stack = get_stack()
*/;
...
Good job! Now you know where these queries were launched and has something to do with this problem.
Installation and configuration
Install from PyPI:
pip install django-sql-stacktrace
Or install the dev-version from GitHub:
pip install -e git://github.com/adw0rd/django-sql-stacktrace.git#egg=sqlstacktrace
Now, add to “INSTALLED_APPS”:
INSTALLED_APPS = (
...
'sqlstacktrace',
...
)
And turn on the SQL_STACKTRACE:
SQL_STACKTRACE = True
Getting code
You can get django-sql-stacktrace from https://github.com/adw0rd/django-sql-stacktrace. The application is distributed by BSD license.
Comments are closed.