Skip to content

Commit

Permalink
Fixed #35429 -- Added argparse choices to --database options.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHyuckSa authored and sarahboyce committed May 10, 2024
1 parent 962215d commit 4a76ac0
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion django/contrib/auth/management/commands/changepassword.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.contrib.auth.password_validation import validate_password
from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS
from django.db import DEFAULT_DB_ALIAS, connections

UserModel = get_user_model()

Expand Down Expand Up @@ -32,6 +32,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Specifies the database to use. Default is "default".',
)

Expand Down
3 changes: 2 additions & 1 deletion django/contrib/auth/management/commands/createsuperuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib.auth.password_validation import validate_password
from django.core import exceptions
from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS
from django.db import DEFAULT_DB_ALIAS, connections
from django.utils.functional import cached_property
from django.utils.text import capfirst

Expand Down Expand Up @@ -56,6 +56,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Specifies the database to use. Default is "default".',
)
for field_name in self.UserModel.REQUIRED_FIELDS:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.core.management import BaseCommand
from django.db import DEFAULT_DB_ALIAS, router
from django.db import DEFAULT_DB_ALIAS, connections, router
from django.db.models.deletion import Collector


Expand All @@ -21,6 +21,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Nominates the database to use. Defaults to the "default" database.',
)
parser.add_argument(
Expand Down
2 changes: 2 additions & 0 deletions django/core/management/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.core import checks
from django.core.checks.registry import registry
from django.core.management.base import BaseCommand, CommandError
from django.db import connections


class Command(BaseCommand):
Expand Down Expand Up @@ -43,6 +44,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
action="append",
choices=tuple(connections),
dest="databases",
help="Run database related checks against these aliases.",
)
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/createcachetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help="Nominates a database onto which the cache tables will be "
'installed. Defaults to the "default" database.',
)
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/dbshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
"Nominates a database onto which to open a shell. Defaults to the "
'"default" database.'
Expand Down
3 changes: 2 additions & 1 deletion django/core/management/commands/dumpdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.core import serializers
from django.core.management.base import BaseCommand, CommandError
from django.core.management.utils import parse_apps_and_model_labels
from django.db import DEFAULT_DB_ALIAS, router
from django.db import DEFAULT_DB_ALIAS, connections, router

try:
import bz2
Expand Down Expand Up @@ -56,6 +56,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help="Nominates a specific database to dump fixtures from. "
'Defaults to the "default" database.',
)
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Nominates a database to flush. Defaults to the "default" database.',
)

Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/inspectdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to introspect. Defaults to using the "default" '
"database."
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
"Nominates a specific database to load fixtures into. Defaults to the "
'"default" database.'
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/showmigrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
"Nominates a database to show migrations for. Defaults to the "
'"default" database.'
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/sqlflush.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to print the SQL for. Defaults to the "default" '
"database."
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/sqlmigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to create SQL for. Defaults to the "default" '
"database."
Expand Down
1 change: 1 addition & 0 deletions django/core/management/commands/sqlsequencereset.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def add_arguments(self, parser):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to print the SQL for. Defaults to the "default" '
"database."
Expand Down
29 changes: 29 additions & 0 deletions tests/admin_scripts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2301,6 +2301,35 @@ def test_precedence(self):
self.assertEqual(out.getvalue().strip(), "simple_app")


class CommandDBOptionChoiceTests(SimpleTestCase):
def test_invalid_choice_db_option(self):
expected_error = (
"Error: argument --database: invalid choice: "
"'deflaut' (choose from 'default', 'other')"
)
args = [
"changepassword",
"createsuperuser",
"remove_stale_contenttypes",
"check",
"createcachetable",
"dbshell",
"flush",
"dumpdata",
"inspectdb",
"loaddata",
"showmigrations",
"sqlflush",
"sqlmigrate",
"sqlsequencereset",
"migrate",
]

for arg in args:
with self.assertRaisesMessage(CommandError, expected_error):
call_command(arg, "--database", "deflaut", verbosity=0)


class ArgumentOrder(AdminScriptTestCase):
"""Tests for 2-stage argument parsing scheme.
Expand Down

0 comments on commit 4a76ac0

Please sign in to comment.