Fixed #25855 -- Enhanced the migration warning for runserver.

Added unapplied migration count and the list of unmigrated apps.
This commit is contained in:
Emre Yilmaz 2015-12-03 18:51:39 +02:00 committed by Tim Graham
parent 541000773a
commit 63a6a653d4
10 changed files with 104 additions and 4 deletions

View File

@ -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

View File

@ -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)),
],
),
]

View File

@ -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'

View File

@ -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)),
],
),
]

View File

@ -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'

View File

@ -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={