From 63a6a653d4282f74e35582d41ca507139c9cfbe1 Mon Sep 17 00:00:00 2001 From: Emre Yilmaz Date: Thu, 3 Dec 2015 18:51:39 +0200 Subject: [PATCH] Fixed #25855 -- Enhanced the migration warning for runserver. Added unapplied migration count and the list of unmigrated apps. --- django/core/management/commands/runserver.py | 14 ++++++-- .../another_app_waiting_migration/__init__.py | 0 .../migrations/0001_initial.py | 21 ++++++++++++ .../migrations/__init__.py | 0 .../another_app_waiting_migration/models.py | 10 ++++++ .../app_waiting_migration/__init__.py | 0 .../migrations/0001_initial.py | 21 ++++++++++++ .../migrations/__init__.py | 0 .../app_waiting_migration/models.py | 10 ++++++ tests/admin_scripts/tests.py | 32 ++++++++++++++++++- 10 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/admin_scripts/another_app_waiting_migration/__init__.py create mode 100644 tests/admin_scripts/another_app_waiting_migration/migrations/0001_initial.py create mode 100644 tests/admin_scripts/another_app_waiting_migration/migrations/__init__.py create mode 100644 tests/admin_scripts/another_app_waiting_migration/models.py create mode 100644 tests/admin_scripts/app_waiting_migration/__init__.py create mode 100644 tests/admin_scripts/app_waiting_migration/migrations/0001_initial.py create mode 100644 tests/admin_scripts/app_waiting_migration/migrations/__init__.py create mode 100644 tests/admin_scripts/app_waiting_migration/models.py diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py index 6d05fa8d6b..68de4ac6e5 100644 --- a/django/core/management/commands/runserver.py +++ b/django/core/management/commands/runserver.py @@ -172,9 +172,17 @@ class Command(BaseCommand): plan = executor.migration_plan(executor.loader.graph.leaf_nodes()) if plan: - self.stdout.write(self.style.NOTICE( - "\nYou have unapplied migrations; your app may not work properly until they are applied." - )) + apps_waiting_migration = sorted(set(migration.app_label for migration, backwards in plan)) + self.stdout.write( + self.style.NOTICE( + "\nYou have %(unpplied_migration_count)s unapplied migration(s). " + "Your project may not work properly until you apply the " + "migrations for app(s): %(apps_waiting_migration)s." % { + "unpplied_migration_count": len(plan), + "apps_waiting_migration": ", ".join(apps_waiting_migration), + } + ) + ) self.stdout.write(self.style.NOTICE("Run 'python manage.py migrate' to apply them.\n")) # Kept for backward compatibility diff --git a/tests/admin_scripts/another_app_waiting_migration/__init__.py b/tests/admin_scripts/another_app_waiting_migration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/admin_scripts/another_app_waiting_migration/migrations/0001_initial.py b/tests/admin_scripts/another_app_waiting_migration/migrations/0001_initial.py new file mode 100644 index 0000000000..1486231d6b --- /dev/null +++ b/tests/admin_scripts/another_app_waiting_migration/migrations/0001_initial.py @@ -0,0 +1,21 @@ +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Foo', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + ), + ] diff --git a/tests/admin_scripts/another_app_waiting_migration/migrations/__init__.py b/tests/admin_scripts/another_app_waiting_migration/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/admin_scripts/another_app_waiting_migration/models.py b/tests/admin_scripts/another_app_waiting_migration/models.py new file mode 100644 index 0000000000..6c12c6ab5d --- /dev/null +++ b/tests/admin_scripts/another_app_waiting_migration/models.py @@ -0,0 +1,10 @@ +from __future__ import unicode_literals + +from django.db import models + + +class Foo(models.Model): + name = models.CharField(max_length=255) + + class Meta: + app_label = 'another_app_waiting_migration' diff --git a/tests/admin_scripts/app_waiting_migration/__init__.py b/tests/admin_scripts/app_waiting_migration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/admin_scripts/app_waiting_migration/migrations/0001_initial.py b/tests/admin_scripts/app_waiting_migration/migrations/0001_initial.py new file mode 100644 index 0000000000..52d594a94a --- /dev/null +++ b/tests/admin_scripts/app_waiting_migration/migrations/0001_initial.py @@ -0,0 +1,21 @@ +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Bar', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + ), + ] diff --git a/tests/admin_scripts/app_waiting_migration/migrations/__init__.py b/tests/admin_scripts/app_waiting_migration/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/admin_scripts/app_waiting_migration/models.py b/tests/admin_scripts/app_waiting_migration/models.py new file mode 100644 index 0000000000..5e9f0e3314 --- /dev/null +++ b/tests/admin_scripts/app_waiting_migration/models.py @@ -0,0 +1,10 @@ +from __future__ import unicode_literals + +from django.db import models + + +class Bar(models.Model): + name = models.CharField(max_length=255) + + class Meta: + app_label = 'app_waiting_migration' diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py index ea7c8a25f0..805be8b144 100644 --- a/tests/admin_scripts/tests.py +++ b/tests/admin_scripts/tests.py @@ -26,7 +26,7 @@ from django.db import ConnectionHandler from django.db.migrations.exceptions import MigrationSchemaMissing from django.db.migrations.recorder import MigrationRecorder from django.test import ( - LiveServerTestCase, SimpleTestCase, mock, override_settings, + LiveServerTestCase, SimpleTestCase, TestCase, mock, override_settings, ) from django.test.runner import DiscoverRunner from django.utils._os import npath, upath @@ -1383,6 +1383,36 @@ class ManageRunserver(AdminScriptTestCase): self.assertIn("Not checking migrations", self.output.getvalue()) +class ManageRunserverMigrationWarning(TestCase): + + def setUp(self): + from django.core.management.commands.runserver import Command + self.stdout = StringIO() + self.runserver_command = Command(stdout=self.stdout) + + @override_settings(INSTALLED_APPS=["admin_scripts.app_waiting_migration"]) + def test_migration_warning_one_app(self): + self.runserver_command.check_migrations() + output = self.stdout.getvalue() + self.assertIn('You have 1 unapplied migration(s)', output) + self.assertIn('apply the migrations for app(s): app_waiting_migration.', output) + + @override_settings( + INSTALLED_APPS=[ + "admin_scripts.app_waiting_migration", + "admin_scripts.another_app_waiting_migration", + ], + ) + def test_migration_warning_multiple_apps(self): + self.runserver_command.check_migrations() + output = self.stdout.getvalue() + self.assertIn('You have 2 unapplied migration(s)', output) + self.assertIn( + 'apply the migrations for app(s): another_app_waiting_migration, ' + 'app_waiting_migration.', output + ) + + class ManageRunserverEmptyAllowedHosts(AdminScriptTestCase): def setUp(self): self.write_settings('settings.py', sdict={