From c723a1ff8e76aaef227d5af7a57006cc9bfd2fc8 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Tue, 19 Jun 2018 09:35:55 +0200 Subject: [PATCH] Fixed #29506 -- Added validation for migrate's app_label option. Thanks MyungSeKyo for the report and the initial patch. --- django/core/management/commands/migrate.py | 21 +++++++++++---------- tests/migrations/test_commands.py | 8 ++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index 0e27eaa19f..2792484b47 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -100,12 +100,18 @@ class Command(BaseCommand): # If they supplied command line arguments, work out what they mean. target_app_labels_only = True - if options['app_label'] and options['migration_name']: - app_label, migration_name = options['app_label'], options['migration_name'] + if options['app_label']: + # Validate app_label. + app_label = options['app_label'] + try: + apps.get_app_config(app_label) + except LookupError as err: + raise CommandError(str(err)) if app_label not in executor.loader.migrated_apps: - raise CommandError( - "App '%s' does not have migrations." % app_label - ) + raise CommandError("App '%s' does not have migrations." % app_label) + + if options['app_label'] and options['migration_name']: + migration_name = options['migration_name'] if migration_name == "zero": targets = [(app_label, None)] else: @@ -123,11 +129,6 @@ class Command(BaseCommand): targets = [(app_label, migration.name)] target_app_labels_only = False elif options['app_label']: - app_label = options['app_label'] - if app_label not in executor.loader.migrated_apps: - raise CommandError( - "App '%s' does not have migrations." % app_label - ) targets = [key for key in executor.loader.graph.leaf_nodes() if key[0] == app_label] else: targets = executor.loader.graph.leaf_nodes() diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index c9791af51e..494d5cd211 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -1425,3 +1425,11 @@ class AppLabelErrorTests(TestCase): with self.assertRaises(SystemExit): call_command('makemigrations', 'django.contrib.auth', stderr=err) self.assertIn(self.did_you_mean_auth_error, err.getvalue()) + + def test_migrate_nonexistent_app_label(self): + with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): + call_command('migrate', 'nonexistent_app') + + def test_migrate_app_name_specified_as_label(self): + with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): + call_command('migrate', 'django.contrib.auth')