From 6b3e17bab61b94a307d5b276480f8a0e125cb2e5 Mon Sep 17 00:00:00 2001 From: oliver Date: Mon, 25 Jun 2018 23:43:12 +0900 Subject: [PATCH] Fixed #29518 -- Added validation for sqlmigrate's app_label argument. --- AUTHORS | 1 + django/core/management/commands/sqlmigrate.py | 6 ++++++ tests/migrations/test_commands.py | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/AUTHORS b/AUTHORS index 4ac99db75e..d89c5a2e03 100644 --- a/AUTHORS +++ b/AUTHORS @@ -739,6 +739,7 @@ answer newbie questions, and generally made Django that much better: Sean Brant Sebastian Hillig Sebastian Spiegel + Segyo Myung Selwin Ong Sengtha Chay Senko Rašić diff --git a/django/core/management/commands/sqlmigrate.py b/django/core/management/commands/sqlmigrate.py index 4d0b08b175..32a78da452 100644 --- a/django/core/management/commands/sqlmigrate.py +++ b/django/core/management/commands/sqlmigrate.py @@ -1,3 +1,4 @@ +from django.apps import apps from django.core.management.base import BaseCommand, CommandError from django.db import DEFAULT_DB_ALIAS, connections from django.db.migrations.executor import MigrationExecutor @@ -37,6 +38,11 @@ class Command(BaseCommand): # Resolve command-line arguments into a migration app_label, migration_name = options['app_label'], options['migration_name'] + # Validate 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) try: diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index 4f01a3b665..f625a47c7f 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -1434,6 +1434,14 @@ class AppLabelErrorTests(TestCase): with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): call_command('migrate', 'django.contrib.auth') + def test_sqlmigrate_nonexistent_app_label(self): + with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): + call_command('sqlmigrate', 'nonexistent_app', '0002') + + def test_sqlmigrate_app_name_specified_as_label(self): + with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): + call_command('sqlmigrate', 'django.contrib.auth', '0002') + def test_squashmigrations_nonexistent_app_label(self): with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): call_command('squashmigrations', 'nonexistent_app', '0002')