Fixed #29518 -- Added validation for sqlmigrate's app_label argument.

This commit is contained in:
oliver 2018-06-25 23:43:12 +09:00 committed by Tim Graham
parent e7185a6514
commit 6b3e17bab6
3 changed files with 15 additions and 0 deletions

View File

@ -739,6 +739,7 @@ answer newbie questions, and generally made Django that much better:
Sean Brant Sean Brant
Sebastian Hillig <sebastian.hillig@gmail.com> Sebastian Hillig <sebastian.hillig@gmail.com>
Sebastian Spiegel <http://www.tivix.com/> Sebastian Spiegel <http://www.tivix.com/>
Segyo Myung <myungsekyo@gmail.com>
Selwin Ong <selwin@ui.co.id> Selwin Ong <selwin@ui.co.id>
Sengtha Chay <sengtha@e-khmer.com> Sengtha Chay <sengtha@e-khmer.com>
Senko Rašić <senko.rasic@dobarkod.hr> Senko Rašić <senko.rasic@dobarkod.hr>

View File

@ -1,3 +1,4 @@
from django.apps import apps
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.db import DEFAULT_DB_ALIAS, connections from django.db import DEFAULT_DB_ALIAS, connections
from django.db.migrations.executor import MigrationExecutor from django.db.migrations.executor import MigrationExecutor
@ -37,6 +38,11 @@ class Command(BaseCommand):
# Resolve command-line arguments into a migration # Resolve command-line arguments into a migration
app_label, migration_name = options['app_label'], options['migration_name'] 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: 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)
try: try:

View File

@ -1434,6 +1434,14 @@ class AppLabelErrorTests(TestCase):
with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error):
call_command('migrate', 'django.contrib.auth') 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): def test_squashmigrations_nonexistent_app_label(self):
with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): with self.assertRaisesMessage(CommandError, self.nonexistent_app_error):
call_command('squashmigrations', 'nonexistent_app', '0002') call_command('squashmigrations', 'nonexistent_app', '0002')