Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Migrate prompts if you need makemigrations, runserver prompts for mig…

…rate
  • Loading branch information...
commit 1d20e6df9553346c79edd92e6e8934e9c5c4aa2c 1 parent abb04f1
@andrewgodwin andrewgodwin authored
View
12 django/core/management/commands/migrate.py
@@ -14,6 +14,9 @@
from django.db import connections, router, transaction, models, DEFAULT_DB_ALIAS
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.loader import MigrationLoader, AmbiguityError
+from django.db.migrations.state import ProjectState
+from django.db.migrations.autodetector import MigrationAutodetector
+from django.db.models.loading import cache
from django.utils.module_loading import module_has_submodule
@@ -120,6 +123,15 @@ def handle(self, *args, **options):
if not plan:
if self.verbosity >= 1:
self.stdout.write(" No migrations needed.")
+ # If there's changes that aren't in migrations yet, tell them how to fix it.
+ autodetector = MigrationAutodetector(
+ executor.loader.graph.project_state(),
+ ProjectState.from_app_cache(cache),
+ )
+ changes = autodetector.changes(graph=executor.loader.graph)
+ if changes:
+ self.stdout.write(self.style.NOTICE(" Your models have changes that are not yet reflected in a migration, and so won't be applied."))
+ self.stdout.write(self.style.NOTICE(" Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them."))
else:
executor.migrate(targets, plan, fake=options.get("fake", False))
View
14 django/core/management/commands/runserver.py
@@ -10,6 +10,8 @@
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
@@ -99,10 +101,10 @@ def inner_run(self, *args, **options):
self.stdout.write("Validating models...\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')
-
self.stdout.write((
"%(started_at)s\n"
"Django version %(version)s, using settings %(settings)r\n"
@@ -144,6 +146,16 @@ def inner_run(self, *args, **options):
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
Please sign in to comment.
Something went wrong with that request. Please try again.