Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add get_migratable_models util method to ConnectionRouter

  • Loading branch information...
commit 650b6fd90efea058c54800d2ba0e0976547894a3 1 parent 2992f42
Claude Paroz claudep authored
5 django/core/management/commands/flush.py
View
@@ -94,8 +94,5 @@ def emit_post_migrate(verbosity, interactive, database):
# 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)
5 django/core/management/commands/migrate.py
View
@@ -153,10 +153,7 @@ def sync_apps(self, connection, apps):
# 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
]
18 django/core/management/sql.py
View
@@ -10,14 +10,6 @@
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
12 django/db/backends/__init__.py
View
@@ -1272,11 +1272,9 @@ def django_table_names(self, only_existing=False):
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 @@ def installed_models(self, tables):
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 @@ def sequence_list(self):
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})
8 django/db/utils.py
View
@@ -278,3 +278,11 @@ def allow_migrate(self, db, model):
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)]
Please sign in to comment.
Something went wrong with that request. Please try again.