|
7 | 7 |
|
8 | 8 | import django |
9 | 9 |
|
| 10 | +from django.conf import settings |
10 | 11 | from django.core.exceptions import SuspiciousOperation |
11 | 12 | from django.db.models import Expression, Model, Q |
12 | 13 | from django.db.models.fields.related import RelatedField |
|
24 | 25 |
|
25 | 26 |
|
26 | 27 | def append_caller_to_sql(sql): |
| 28 | + """Append the caller to SQL queries. |
| 29 | +
|
| 30 | + Adds the calling file and function as an SQL comment to each query. |
| 31 | + Examples: |
| 32 | + INSERT INTO "tests_47ee19d1" ("id", "title") |
| 33 | + VALUES (1, 'Test') |
| 34 | + RETURNING "tests_47ee19d1"."id" |
| 35 | + /* 998020 test_append_caller_to_sql_crud .../django-postgres-extra/tests/test_append_caller_to_sql.py 55 */ |
| 36 | +
|
| 37 | + SELECT "tests_47ee19d1"."id", "tests_47ee19d1"."title" |
| 38 | + FROM "tests_47ee19d1" |
| 39 | + WHERE "tests_47ee19d1"."id" = 1 |
| 40 | + LIMIT 1 |
| 41 | + /* 998020 test_append_caller_to_sql_crud .../django-postgres-extra/tests/test_append_caller_to_sql.py 69 */ |
| 42 | +
|
| 43 | + UPDATE "tests_47ee19d1" |
| 44 | + SET "title" = 'success' |
| 45 | + WHERE "tests_47ee19d1"."id" = 1 |
| 46 | + /* 998020 test_append_caller_to_sql_crud .../django-postgres-extra/tests/test_append_caller_to_sql.py 64 */ |
| 47 | +
|
| 48 | + DELETE FROM "tests_47ee19d1" |
| 49 | + WHERE "tests_47ee19d1"."id" IN (1) |
| 50 | + /* 998020 test_append_caller_to_sql_crud .../django-postgres-extra/tests/test_append_caller_to_sql.py 74 */ |
| 51 | +
|
| 52 | + Slow and blocking queries could be easily tracked down to their originator |
| 53 | + within the source code using the "pg_stat_activity" table. |
| 54 | +
|
| 55 | + Enable "PSQLEXTRA_ANNOTATE_SQL" within the database settings to enable this feature. |
| 56 | + """ |
| 57 | + |
| 58 | + if not getattr(settings, "PSQLEXTRA_ANNOTATE_SQL", None): |
| 59 | + return sql |
| 60 | + |
27 | 61 | try: |
28 | 62 | # Search for the first non-Django caller |
29 | 63 | stack = inspect.stack() |
30 | | - i = 0 |
31 | 64 | for stack_frame in stack[1:]: |
32 | | - i += 1 |
33 | 65 | frame_filename = stack_frame[1] |
34 | 66 | frame_line = stack_frame[2] |
35 | 67 | frame_function = stack_frame[3] |
@@ -148,7 +180,6 @@ def as_sql(self, return_id=False): |
148 | 180 | self._rewrite_insert(sql, params, return_id) |
149 | 181 | for sql, params in super().as_sql() |
150 | 182 | ] |
151 | | - print(f"InsertConflictCompiler {queries}") |
152 | 183 |
|
153 | 184 | return queries |
154 | 185 |
|
@@ -196,12 +227,11 @@ def _rewrite_insert(self, sql, params, return_id=False): |
196 | 227 | self.qn(self.query.model._meta.pk.attname) if return_id else "*" |
197 | 228 | ) |
198 | 229 |
|
199 | | - if hasattr(self.query, "conflict_action"): |
200 | | - (sql, params) = self._rewrite_insert_on_conflict( |
201 | | - sql, params, self.query.conflict_action.value, returning |
202 | | - ) |
| 230 | + (sql, params) = self._rewrite_insert_on_conflict( |
| 231 | + sql, params, self.query.conflict_action.value, returning |
| 232 | + ) |
203 | 233 |
|
204 | | - return (append_caller_to_sql(sql), params) |
| 234 | + return append_caller_to_sql(sql), params |
205 | 235 |
|
206 | 236 | def _rewrite_insert_on_conflict( |
207 | 237 | self, sql, params, conflict_action: ConflictAction, returning |
|
0 commit comments