From 856aaaf2b15228bd5babbe44654c3c1ca39e8fd7 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sun, 29 Dec 2013 21:21:23 +0100 Subject: [PATCH] Changed get_migratable_models to use an app config. --- django/core/management/commands/flush.py | 2 +- django/core/management/commands/migrate.py | 2 +- django/core/management/sql.py | 10 +++++----- django/db/backends/__init__.py | 6 +++--- django/db/utils.py | 7 +++---- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index 619df9536c..5b4733ecad 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -94,5 +94,5 @@ Are you sure you want to do this? # respond as if the database had been migrated from scratch. all_models = [] for app_config in apps.get_app_configs(only_with_models_module=True): - all_models.extend(router.get_migratable_models(app_config.models_module, database, include_auto_created=True)) + all_models.extend(router.get_migratable_models(app_config, 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 d7daafd279..5da5e51a48 100644 --- a/django/core/management/commands/migrate.py +++ b/django/core/management/commands/migrate.py @@ -180,7 +180,7 @@ class Command(BaseCommand): # Build the manifest of apps and models that are to be synchronized all_models = [ (app_config.label, - router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True)) + router.get_migratable_models(app_config, connection.alias, include_auto_created=True)) for app_config in apps.get_app_configs(only_with_models_module=True) if app_config.label in app_labels ] diff --git a/django/core/management/sql.py b/django/core/management/sql.py index 6f98282a9c..4cced4f566 100644 --- a/django/core/management/sql.py +++ b/django/core/management/sql.py @@ -31,7 +31,7 @@ def sql_create(app_config, 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 router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app_config, 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(): @@ -78,7 +78,7 @@ def sql_delete(app_config, style, connection): to_delete = set() references_to_delete = {} - app_models = router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True) + app_models = router.get_migratable_models(app_config, 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 @@ -122,7 +122,7 @@ def sql_custom(app_config, style, connection): "Returns a list of the custom table modifying SQL statements for the given app." output = [] - app_models = router.get_migratable_models(app_config.models_module, connection.alias) + app_models = router.get_migratable_models(app_config, connection.alias) for model in app_models: output.extend(custom_sql_for_model(model, style, connection)) @@ -133,7 +133,7 @@ def sql_custom(app_config, style, connection): def sql_indexes(app_config, style, connection): "Returns a list of the CREATE INDEX SQL statements for all models in the given app." output = [] - for model in router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app_config, connection.alias, include_auto_created=True): output.extend(connection.creation.sql_indexes_for_model(model, style)) return output @@ -141,7 +141,7 @@ def sql_indexes(app_config, style, connection): def sql_destroy_indexes(app_config, style, connection): "Returns a list of the DROP INDEX SQL statements for all models in the given app." output = [] - for model in router.get_migratable_models(app_config.models_module, connection.alias, include_auto_created=True): + for model in router.get_migratable_models(app_config, 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 c5f650d503..5b681756e9 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -1272,7 +1272,7 @@ class BaseDatabaseIntrospection(object): from django.db import router tables = set() for app_config in apps.get_app_configs(only_with_models_module=True): - for model in router.get_migratable_models(app_config.models_module, self.connection.alias): + for model in router.get_migratable_models(app_config, self.connection.alias): if not model._meta.managed: continue tables.add(model._meta.db_table) @@ -1293,7 +1293,7 @@ class BaseDatabaseIntrospection(object): from django.db import router all_models = [] for app_config in apps.get_app_configs(only_with_models_module=True): - all_models.extend(router.get_migratable_models(app_config.models_module, self.connection.alias)) + all_models.extend(router.get_migratable_models(app_config, self.connection.alias)) tables = list(map(self.table_name_converter, tables)) return set([ m for m in all_models @@ -1308,7 +1308,7 @@ class BaseDatabaseIntrospection(object): sequence_list = [] for app_config in apps.get_app_configs(only_with_models_module=True): - for model in router.get_migratable_models(app_config.models_module, self.connection.alias): + for model in router.get_migratable_models(app_config, self.connection.alias): if not model._meta.managed: continue if model._meta.swapped: diff --git a/django/db/utils.py b/django/db/utils.py index 85eabb934e..0bcf33f252 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -278,10 +278,9 @@ class ConnectionRouter(object): return allow return True - def get_migratable_models(self, app, db, include_auto_created=False): + def get_migratable_models(self, app_config, db, include_auto_created=False): """ Return app models allowed to be synchronized on provided db. """ - from django.apps import apps - return [model for model in apps.get_models(app, include_auto_created=include_auto_created) - if self.allow_migrate(db, model)] + models = app_config.get_models(include_auto_created=include_auto_created) + return [model for model in models if self.allow_migrate(db, model)]