From 098c07a03286b5ed133102733e67a83061647ea0 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 1 Sep 2016 16:19:29 -0400 Subject: Fixed #27142, #27110 -- Made makemigrations consistency checks respect database routers. Partially reverted refs #27054 except for one of the tests as this solution supersedes that one. Thanks Shai Berger for the review. --- django/core/management/commands/makemigrations.py | 14 ++++++++++---- django/db/migrations/loader.py | 8 +------- 2 files changed, 11 insertions(+), 11 deletions(-) (limited to 'django') diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index f9f50cc2ad..2fb2d713ca 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -4,8 +4,9 @@ import warnings from itertools import takewhile from django.apps import apps +from django.conf import settings from django.core.management.base import BaseCommand, CommandError -from django.db import connections +from django.db import DEFAULT_DB_ALIAS, connections, router from django.db.migrations import Migration from django.db.migrations.autodetector import MigrationAutodetector from django.db.migrations.loader import MigrationLoader @@ -94,9 +95,14 @@ class Command(BaseCommand): loader = MigrationLoader(None, ignore_no_migrations=True) # Raise an error if any migrations are applied before their dependencies. - for db in connections: - connection = connections[db] - if connection.settings_dict['ENGINE'] != 'django.db.backends.dummy': + consistency_check_labels = set(config.label for config in apps.get_app_configs()) + # Non-default databases are only checked if database routers used. + aliases_to_check = connections if settings.DATABASE_ROUTERS else [DEFAULT_DB_ALIAS] + for alias in sorted(aliases_to_check): + connection = connections[alias] + if (connection.settings_dict['ENGINE'] != 'django.db.backends.dummy' and + # At least one app must be migrated to the database. + any(router.allow_migrate(connection.alias, label) for label in consistency_check_labels)): loader.check_consistent_history(connection) # Before anything else, see if there's conflicting apps and drop out diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py index c78aaed573..ce6fd9e3c0 100644 --- a/django/db/migrations/loader.py +++ b/django/db/migrations/loader.py @@ -6,7 +6,6 @@ from importlib import import_module from django.apps import apps from django.conf import settings -from django.db.migrations.exceptions import MigrationSchemaMissing from django.db.migrations.graph import MigrationGraph from django.db.migrations.recorder import MigrationRecorder from django.utils import six @@ -280,12 +279,7 @@ class MigrationLoader(object): unapplied dependencies. """ recorder = MigrationRecorder(connection) - try: - applied = recorder.applied_migrations() - except MigrationSchemaMissing: - # Skip check if the django_migrations table is missing and can't be - # created. - return + applied = recorder.applied_migrations() for migration in applied: # If the migration is unknown, skip it. if migration not in self.graph.nodes: -- cgit v1.2.1