From 650b6fd90efea058c54800d2ba0e0976547894a3 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 16 Oct 2013 17:58:21 +0200 Subject: [PATCH] Add get_migratable_models util method to ConnectionRouter --- django/core/management/commands/flush.py | 5 +---- django/core/management/commands/migrate.py | 5 +---- django/core/management/sql.py | 18 +++++------------- django/db/backends/__init__.py | 12 +++--------- django/db/utils.py | 8 ++++++++ 5 files changed, 18 insertions(+), 30 deletions(-) diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index ea0952cb53..130338a55a 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -94,8 +94,5 @@ Are you sure you want to do this? # respond as if the database had been migrated from scratch. all_models = [] for app in models.get_apps(): - all_models.extend([ - m for m in models.get_models(app, include_auto_created=True) - if router.allow_migrate(database, m) - ]) + all_models.extend(router.get_migratable_models(app, database, include_auto_created=True)) emit_post_migrate_signal(set(all_models), verbosity, interactive, database) diff --git a/django/core/management/commands/migrate.py b/django/core/management/commands/migrate.py index f351adaaba..14239b1452 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -153,10 +153,7 @@ class Command(BaseCommand): # Build the manifest of apps and models that are to be synchronized all_models = [ (app.__name__.split('.')[-2], - [ - m for m in models.get_models(app, include_auto_created=True) - if router.allow_migrate(connection.alias, m) - ]) + router.get_migratable_models(app, connection.alias, include_auto_created=True)) for app in models.get_apps() if app.__name__.split('.')[-2] in apps ] diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 69772d9fa6..6f60d73ae9 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -10,14 +10,6 @@ from django.core.management.base import CommandError from django.db import models, router -def filtered_app_models(app, db_alias, include_auto_created=False): - """ - Return app models allowed to be synchronized on provided db. - """ - return [model for model in models.get_models(app, include_auto_created=include_auto_created) - if router.allow_migrate(db_alias, model)] - - def sql_create(app, style, connection): "Returns a list of the CREATE TABLE SQL statements for the given app." @@ -38,7 +30,7 @@ def sql_create(app, style, connection): known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models) pending_references = {} - for model in filtered_app_models(app, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app, connection.alias, include_auto_created=True): output, references = connection.creation.sql_create_model(model, style, known_models) final_output.extend(output) for refto, refs in references.items(): @@ -85,7 +77,7 @@ def sql_delete(app, style, connection): to_delete = set() references_to_delete = {} - app_models = filtered_app_models(app, connection.alias, include_auto_created=True) + app_models = router.get_migratable_models(app, connection.alias, include_auto_created=True) for model in app_models: if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: # The table exists, so it needs to be dropped @@ -129,7 +121,7 @@ def sql_custom(app, style, connection): "Returns a list of the custom table modifying SQL statements for the given app." output = [] - app_models = filtered_app_models(app, connection.alias) + app_models = router.get_migratable_models(app, connection.alias) for model in app_models: output.extend(custom_sql_for_model(model, style, connection)) @@ -140,7 +132,7 @@ def sql_custom(app, style, connection): def sql_indexes(app, style, connection): "Returns a list of the CREATE INDEX SQL statements for all models in the given app." output = [] - for model in filtered_app_models(app, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app, connection.alias, include_auto_created=True): output.extend(connection.creation.sql_indexes_for_model(model, style)) return output @@ -148,7 +140,7 @@ def sql_indexes(app, style, connection): def sql_destroy_indexes(app, style, connection): "Returns a list of the DROP INDEX SQL statements for all models in the given app." output = [] - for model in filtered_app_models(app, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app, connection.alias, include_auto_created=True): output.extend(connection.creation.sql_destroy_indexes_for_model(model, style)) return output diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 00c5776d59..a23751caf5 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -1272,11 +1272,9 @@ class BaseDatabaseIntrospection(object): from django.db import models, router tables = set() for app in models.get_apps(): - for model in models.get_models(app): + for model in router.get_migratable_models(app, self.connection.alias): if not model._meta.managed: continue - if not router.allow_migrate(self.connection.alias, model): - continue tables.add(model._meta.db_table) tables.update(f.m2m_db_table() for f in model._meta.local_many_to_many) tables = list(tables) @@ -1294,9 +1292,7 @@ class BaseDatabaseIntrospection(object): from django.db import models, router all_models = [] for app in models.get_apps(): - for model in models.get_models(app): - if router.allow_migrate(self.connection.alias, model): - all_models.append(model) + all_models.extend(router.get_migratable_models(app, self.connection.alias)) tables = list(map(self.table_name_converter, tables)) return set([ m for m in all_models @@ -1311,13 +1307,11 @@ class BaseDatabaseIntrospection(object): sequence_list = [] for app in apps: - for model in models.get_models(app): + for model in router.get_migratable_models(app, self.connection.alias): if not model._meta.managed: continue if model._meta.swapped: continue - if not router.allow_migrate(self.connection.alias, model): - continue for f in model._meta.local_fields: if isinstance(f, models.AutoField): sequence_list.append({'table': model._meta.db_table, 'column': f.column}) diff --git a/django/db/utils.py b/django/db/utils.py index 100fc5e878..3b3799bbd7 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -278,3 +278,11 @@ class ConnectionRouter(object): if allow is not None: return allow return True + + def get_migratable_models(self, app, db, include_auto_created=False): + """ + Return app models allowed to be synchronized on provided db. + """ + from .models import get_models + return [model for model in get_models(app, include_auto_created=include_auto_created) + if self.allow_migrate(db, model)]