Fixed #35429 -- Added argparse choices to --database options.

This commit is contained in:
SaJH 2024-05-08 21:24:44 +09:00 committed by Sarah Boyce
parent 962215db13
commit 4a76ac0e9d
16 changed files with 49 additions and 4 deletions

View File

@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model
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()
@ -32,6 +32,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Specifies the database to use. Default is "default".',
)

View File

@ -11,7 +11,7 @@ from django.contrib.auth.management import get_default_username
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
@ -56,6 +56,7 @@ class Command(BaseCommand):
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:

View File

@ -3,7 +3,7 @@ import itertools
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
@ -21,6 +21,7 @@ class Command(BaseCommand):
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(

View File

@ -2,6 +2,7 @@ from django.apps import apps
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):
@ -43,6 +44,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
action="append",
choices=tuple(connections),
dest="databases",
help="Run database related checks against these aliases.",
)

View File

@ -30,6 +30,7 @@ class Command(BaseCommand):
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.',
)

View File

@ -16,6 +16,7 @@ class Command(BaseCommand):
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.'

View File

@ -6,7 +6,7 @@ from django.apps import apps
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
@ -56,6 +56,7 @@ class Command(BaseCommand):
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.',
)

View File

@ -25,6 +25,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help='Nominates a database to flush. Defaults to the "default" database.',
)

View File

@ -25,6 +25,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to introspect. Defaults to using the "default" '
"database."

View File

@ -55,6 +55,7 @@ class Command(BaseCommand):
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.'

View File

@ -47,6 +47,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to synchronize. Defaults to the "default" '
"database."

View File

@ -19,6 +19,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
"Nominates a database to show migrations for. Defaults to the "
'"default" database.'

View File

@ -16,6 +16,7 @@ class Command(BaseCommand):
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."

View File

@ -19,6 +19,7 @@ class Command(BaseCommand):
parser.add_argument(
"--database",
default=DEFAULT_DB_ALIAS,
choices=tuple(connections),
help=(
'Nominates a database to create SQL for. Defaults to the "default" '
"database."

View File

@ -14,6 +14,7 @@ class Command(AppCommand):
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."

View File

@ -2301,6 +2301,35 @@ class Discovery(SimpleTestCase):
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.