[Django] #35429: Add argparse choices to --database options

26 views
Skip to first unread message

Django

unread,
May 3, 2024, 5:15:18 PMMay 3
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam | Owner: nobody
Johnson |
Type: | Status: new
Cleanup/optimization |
Component: Core | Version: dev
(Management commands) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Many management commands take a `--database` option to select which
database to operate on. Currently, this is unvalidated, causing crashes
when a bad name is typed:

{{{
$ ./manage.py migrate --database deflaut
Traceback (most recent call last):
...
File "/.../django/core/management/commands/migrate.py", line 100, in
handle
self.check(databases=[database])
...
File "/.../django/db/models/fields/__init__.py", line 442, in
_check_backend_specific_checks
errors.extend(connections[alias].validation.check_field(self,
**kwargs))
~~~~~~~~~~~^^^^^^^
File "/.../django/utils/connection.py", line 61, in __getitem__
raise self.exception_class(f"The connection '{alias}' doesn't exist.")
django.utils.connection.ConnectionDoesNotExist: The connection 'deflaut'
doesn't exist.
}}}

We can add [https://docs.python.org/3.12/library/argparse.html#choices
argparse’s choices] for validation:

{{{
--- django/core/management/commands/migrate.py
+++ django/core/management/commands/migrate.py
@@ -47,6 +47,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
+ choices=tuple(connections),
help=(
'Nominates a database to synchronize. Defaults to the
"default" '
"database."
}}}

The failure is then graceful, and lists the available options:

{{{
$ ./manage.py migrate --database deflaut
usage: manage.py migrate ...
manage.py migrate: error: argument --database: invalid choice: 'deflaut'
(choose from 'default')
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35429>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 3, 2024, 5:31:28 PMMay 3
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted

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

Django

unread,
May 4, 2024, 1:39:15 AMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by saJaeHyukc):

Is it okay if I try to solve this problem? If possible, please charge me a
ticket.?
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:2>

Django

unread,
May 4, 2024, 1:39:51 AMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | saJaeHyukc
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by saJaeHyukc):

* owner: nobody => saJaeHyukc
* status: new => assigned

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

Django

unread,
May 4, 2024, 2:38:49 AMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | saJaeHyukc
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by saJaeHyukc):

* has_patch: 0 => 1

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

Django

unread,
May 4, 2024, 2:39:42 AMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner:
Type: | saJaeHyukc
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by saJaeHyukc):

[https://github.com/django/django/pull/18130]
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:5>

Django

unread,
May 4, 2024, 6:05:09 AMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Adam Johnson):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:6>

Django

unread,
May 4, 2024, 11:15:49 PMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jae Hyuck Sa ):

Replying to [comment:6 Adam Johnson]:
As you told me, I applied them to the related database options.
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:7>

Django

unread,
May 4, 2024, 11:16:19 PMMay 4
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jae Hyuck Sa ):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:8>

Django

unread,
May 5, 2024, 7:28:40 AMMay 5
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

Replying to [ticket:35429 Adam Johnson]:
> We can add [https://docs.python.org/3.12/library/argparse.html#choices
argparse’s choices] for validation:
>
> {{{
> --- django/core/management/commands/migrate.py
> +++ django/core/management/commands/migrate.py
> @@ -47,6 +47,7 @@ def add_arguments(self, parser):
> parser.add_argument(
> "--database",
> default=DEFAULT_DB_ALIAS,
> + choices=tuple(connections),
> help=(
> 'Nominates a database to synchronize. Defaults to the
"default" '
> "database."
> }}}


Can we? Do we really want to iterate over connections? As far as I'm
aware, this will force initialization of all database connections. I don't
think it's acceptable. IMO it's not worth doing.
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:9>

Django

unread,
May 5, 2024, 2:08:23 PMMay 5
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

> Do we really want to iterate over connections? As far as I'm aware, this
will force initialization of all database connections

I know it's confusing (I had to check myself to make sure when accepting
the ticket) but `connections.__iter__`
[https://github.com/django/django/blob/9a27c76021f934201cccf12215514a3091325ec8/django/utils/connection.py#L72-L73
doesn't create any connections] it simply iterates over
`settings.DATABASES`.
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:10>

Django

unread,
May 6, 2024, 6:40:59 AMMay 6
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* cc: Mariusz Felisiak (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:11>

Django

unread,
May 8, 2024, 9:40:44 AMMay 8
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: assigned
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:12>

Django

unread,
May 10, 2024, 5:14:17 AMMay 10
to django-...@googlegroups.com
#35429: Add argparse choices to --database options
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Jae Hyuck
Type: | Sa
Cleanup/optimization | Status: closed
Component: Core (Management | Version: dev
commands) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce <42296566+sarahboyce@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"4a76ac0e9d21c3680cc533ec7f55c0a1919424ba" 4a76ac0]:
{{{#!CommitTicketReference repository=""
revision="4a76ac0e9d21c3680cc533ec7f55c0a1919424ba"
Fixed #35429 -- Added argparse choices to --database options.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35429#comment:13>
Reply all
Reply to author
Forward
0 new messages