summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2014-02-09 10:46:57 +0000
committerAndrew Godwin <andrew@aeracode.org>2014-02-09 10:46:57 +0000
commit38b4adc6960d28483124509aa363dadbe8cc17a6 (patch)
tree364b0c3fad4001c73ee5a21500bc8721c508e0fa
parent46a87214caf07f8b21adce5d1733d5d598d16e26 (diff)
parenta3e0d7753d8a3cec7efcc69b9af8160bdf17947b (diff)
Merge pull request #2244 from mlavin/21856-migration-checks
Fixed #21856: Allow Empty DATABASES Setting
-rw-r--r--django/core/checks/__init__.py1
-rw-r--r--django/core/checks/migrations.py31
-rw-r--r--django/core/management/commands/runserver.py14
-rw-r--r--tests/migrations/test_checks.py43
4 files changed, 75 insertions, 14 deletions
diff --git a/django/core/checks/__init__.py b/django/core/checks/__init__.py
index 82a3acf337..691f2f9606 100644
--- a/django/core/checks/__init__.py
+++ b/django/core/checks/__init__.py
@@ -8,6 +8,7 @@ from .registry import register, run_checks, tag_exists
# Import these to force registration of checks
import django.core.checks.compatibility.django_1_6_0 # NOQA
+import django.core.checks.migrations # NOQA
import django.core.checks.model_checks # NOQA
__all__ = [
diff --git a/django/core/checks/migrations.py b/django/core/checks/migrations.py
new file mode 100644
index 0000000000..c557ad39e7
--- /dev/null
+++ b/django/core/checks/migrations.py
@@ -0,0 +1,31 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.conf import settings
+
+from . import Warning, register
+
+
+@register('migrations')
+def check_migrations(app_configs=None, **kwargs):
+ """
+ Checks to see if the set of migrations on disk matches the
+ migrations in the database. Prints a warning if they don't match.
+ """
+ from django.db import connections, DEFAULT_DB_ALIAS
+ from django.db.migrations.executor import MigrationExecutor
+
+ errors = []
+ plan = None
+ if settings.DATABASES:
+ executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
+ plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
+ if plan:
+ errors.append(
+ Warning(
+ "You have unapplied migrations; "
+ "your app may not work properly until they are applied.",
+ hint="Run 'python manage.py migrate' to apply them.",
+ )
+ )
+ return errors
diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
index e85aa88c3e..8320614409 100644
--- a/django/core/management/commands/runserver.py
+++ b/django/core/management/commands/runserver.py
@@ -10,8 +10,6 @@ import socket
from django.core.management.base import BaseCommand, CommandError
from django.core.servers.basehttp import run, get_internal_wsgi_application
-from django.db import connections, DEFAULT_DB_ALIAS
-from django.db.migrations.executor import MigrationExecutor
from django.utils import autoreload
from django.utils import six
@@ -101,7 +99,6 @@ class Command(BaseCommand):
self.stdout.write("Performing system checks...\n\n")
self.validate(display_num_errors=True)
- self.check_migrations()
now = datetime.now().strftime('%B %d, %Y - %X')
if six.PY2:
now = now.decode('utf-8')
@@ -146,16 +143,5 @@ class Command(BaseCommand):
self.stdout.write(shutdown_message)
sys.exit(0)
- def check_migrations(self):
- """
- Checks to see if the set of migrations on disk matches the
- migrations in the database. Prints a warning if they don't match.
- """
- executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
- 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."))
- self.stdout.write(self.style.NOTICE("Run 'python manage.py migrate' to apply them.\n"))
-
# Kept for backward compatibility
BaseRunserverCommand = Command
diff --git a/tests/migrations/test_checks.py b/tests/migrations/test_checks.py
new file mode 100644
index 0000000000..9ba5bb8e25
--- /dev/null
+++ b/tests/migrations/test_checks.py
@@ -0,0 +1,43 @@
+# encoding: utf8
+from django.core import checks
+from django.core.checks.migrations import check_migrations
+from django.test import TestCase, override_settings
+
+from .test_base import MigrationTestBase
+
+
+class CheckMigrationTests(MigrationTestBase):
+ """
+ Test checks for unapplied migrations.
+ """
+
+ @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
+ def test_unapplied(self):
+ """
+ check_migrations should return a warning when there are unapplied migrations.
+ """
+ expected = [
+ checks.Warning(
+ "You have unapplied migrations; "
+ "your app may not work properly until they are applied.",
+ hint="Run 'python manage.py migrate' to apply them.",
+ )
+ ]
+ errors = check_migrations()
+ self.assertEqual(errors, expected)
+
+ @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}, DATABASES={})
+ def test_no_databases(self):
+ """
+ Migration checks should not consider unapplied migrations if there is
+ no database configured.
+ """
+ errors = check_migrations()
+ self.assertEqual(errors, [])
+
+ def test_no_unapplied(self):
+ """
+ No warning should be issued if all migrations have been applied.
+ """
+ errors = check_migrations()
+ self.assertEqual(errors, [])