[Django] #34859: Format SQL code prodcued by sqlmigrate

22 views
Skip to first unread message

Django

unread,
Sep 21, 2023, 6:32:28 AM9/21/23
to django-...@googlegroups.com
#34859: Format SQL code prodcued by sqlmigrate
-------------------------------------+-------------------------------------
Reporter: Paolo | Owner: nobody
Melchiorre |
Type: New | Status: new
feature |
Component: | Version: dev
Migrations | Keywords: sql, sqlmigrate,
Severity: Normal | migrations
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I propose to use `sqlparse`, which is already a Django dependency, to
format the SQL code generated by the `sqlmigrate` command.


{{{#!diff
diff --git a/django/core/management/commands/sqlmigrate.py
b/django/core/management/commands/sqlmigrate.py
index 2f6993682f..158bcf722b 100644
--- a/django/core/management/commands/sqlmigrate.py
+++ b/django/core/management/commands/sqlmigrate.py
@@ -1,3 +1,5 @@
+import sqlparse
+
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS, connections
@@ -80,4 +82,4 @@ class Command(BaseCommand):
sql_statements = loader.collect_sql(plan)
if not sql_statements and options["verbosity"] >= 1:
self.stderr.write("No operations found.")
- return "\n".join(sql_statements)
+ return sqlparse.format("\n".join(sql_statements), reindent=True)
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34859>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 21, 2023, 7:36:43 AM9/21/23
to django-...@googlegroups.com
#34859: Format SQL code prodcued by sqlmigrate
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: nobody
Type: New feature | Status: new
Component: Migrations | Version: dev
Severity: Normal | Resolution:
Keywords: sql, sqlmigrate, | Triage Stage:
migrations | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

Personally I'm not sure it's worth formatting… it's kinda nice to have the
current format have 1 line per ddl statement. Additionally in my
experience sqlmigrate's formatting of ddl is lack lustre 🤷‍♂️

--
Ticket URL: <https://code.djangoproject.com/ticket/34859#comment:1>

Django

unread,
Sep 22, 2023, 8:21:16 AM9/22/23
to django-...@googlegroups.com
#34859: Format SQL code prodcued by sqlmigrate
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix

Keywords: sql, sqlmigrate, | Triage Stage:
migrations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks Paolo for your ticket!

I personally think that if we were to do this, it should be via a
dedicated CLI switch, I wouldn't change the default formatting because
there may be tools that parse this output and assume one command per line.

Having said that, I would suggest/like that this feature request is
proposed in the Django forum to reach a wider audience, Trac notifications
are receive by a small subset of people from the community.

Following the triage procedure, I'll close as wontfix pending the
conversation in the forum, happy to re-open later!

--
Ticket URL: <https://code.djangoproject.com/ticket/34859#comment:2>

Django

unread,
Sep 22, 2023, 8:23:25 AM9/22/23
to django-...@googlegroups.com
#34859: Format SQL code prodcued by sqlmigrate
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sql, sqlmigrate, | Triage Stage:
migrations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Natalia Bidart):

Replying to [comment:1 David Sanders]:


> Personally I'm not sure it's worth formatting… it's kinda nice to have
the current format have 1 line per ddl statement. Additionally in my
experience sqlmigrate's formatting of ddl is lack lustre 🤷‍♂️

Did you mean that `sqlmigrate`'s formatting is subpar, or that
`sqlparse`'s is?

--
Ticket URL: <https://code.djangoproject.com/ticket/34859#comment:3>

Django

unread,
Sep 22, 2023, 8:24:59 AM9/22/23
to django-...@googlegroups.com
#34859: Format SQL code prodcued by sqlmigrate
-------------------------------------+-------------------------------------
Reporter: Paolo Melchiorre | Owner: nobody
Type: New feature | Status: closed
Component: Migrations | Version: dev
Severity: Normal | Resolution: wontfix
Keywords: sql, sqlmigrate, | Triage Stage:
migrations | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

> Did you mean that sqlmigrate's formatting is subpar, or that sqlparse's
is?

oops! I meant sqlparse :)

--
Ticket URL: <https://code.djangoproject.com/ticket/34859#comment:4>

Reply all
Reply to author
Forward
0 new messages