Skip to content

Commit

Permalink
Fix .explain() in Django >=4.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
SupImDos committed May 8, 2024
1 parent 3d4fffe commit 09cc92d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
24 changes: 17 additions & 7 deletions django_cte/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,30 +82,40 @@ def generate_sql(cls, connection, query, as_sql):
ctes.append(template.format(name=qn(cte.name), query=cte_sql))
params.extend(cte_params)

explain_query = getattr(query, "explain_query", None)
sql = []
if explain_query:
# Required due to breaking change in django commit
# fc91ea1e50e5ef207f0f291b3f6c1942b10db7c7
if django.VERSION >= (4, 0):
explain_attribute = "explain_info"
explain_info = getattr(query, explain_attribute, None)
explain_format = getattr(explain_info, "format", None)
explain_options = getattr(explain_info, "options", {})
else:
explain_attribute = "explain_query"
explain_format = getattr(query, "explain_format", None)
explain_options = getattr(query, "explain_options", {})

explain_query_or_info = getattr(query, explain_attribute, None)
sql = []
if explain_query_or_info:
sql.append(
connection.ops.explain_query_prefix(
explain_format,
**explain_options
)
)
# this needs to get set to False so that the base as_sql() doesn't
# this needs to get set to None so that the base as_sql() doesn't
# insert the EXPLAIN statement where it would end up between the
# WITH ... clause and the final SELECT
query.explain_query = False
setattr(query, explain_attribute, None)

if ctes:
# Always use WITH RECURSIVE
# https://www.postgresql.org/message-id/13122.1339829536%40sss.pgh.pa.us
sql.extend(["WITH RECURSIVE", ", ".join(ctes)])
base_sql, base_params = as_sql()

if explain_query:
query.explain_query = explain_query
if explain_query_or_info:
setattr(query, explain_attribute, explain_query_or_info)

sql.append(base_sql)
params.extend(base_params)
Expand Down
6 changes: 1 addition & 5 deletions tests/test_cte.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,4 @@ def test_explain(self):
.order_by("amount")
)

# the test db (sqlite3) doesn't support EXPLAIN, so let's just check
# to make sure EXPLAIN is at the top
orders.query.explain_query = True

self.assertTrue(str(orders.query).startswith("EXPLAIN "))
self.assertIsInstance(orders.explain(), str)

0 comments on commit 09cc92d

Please sign in to comment.