Add get_migratable_models util method to ConnectionRouter

This commit is contained in:
Claude Paroz 2013-10-16 17:58:21 +02:00
parent 2992f42861
commit 650b6fd90e
5 changed files with 18 additions and 30 deletions

View File

@ -94,8 +94,5 @@ Are you sure you want to do this?
# respond as if the database had been migrated from scratch. # respond as if the database had been migrated from scratch.
all_models = [] all_models = []
for app in models.get_apps(): for app in models.get_apps():
all_models.extend([ all_models.extend(router.get_migratable_models(app, database, include_auto_created=True))
m for m in models.get_models(app, include_auto_created=True)
if router.allow_migrate(database, m)
])
emit_post_migrate_signal(set(all_models), verbosity, interactive, database) emit_post_migrate_signal(set(all_models), verbosity, interactive, database)

View File

@ -153,10 +153,7 @@ class Command(BaseCommand):
# Build the manifest of apps and models that are to be synchronized # Build the manifest of apps and models that are to be synchronized
all_models = [ all_models = [
(app.__name__.split('.')[-2], (app.__name__.split('.')[-2],
[ router.get_migratable_models(app, connection.alias, include_auto_created=True))
m for m in models.get_models(app, include_auto_created=True)
if router.allow_migrate(connection.alias, m)
])
for app in models.get_apps() if app.__name__.split('.')[-2] in apps for app in models.get_apps() if app.__name__.split('.')[-2] in apps
] ]

View File

@ -10,14 +10,6 @@ from django.core.management.base import CommandError
from django.db import models, router 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): def sql_create(app, style, connection):
"Returns a list of the CREATE TABLE SQL statements for the given app." "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) known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models)
pending_references = {} 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) output, references = connection.creation.sql_create_model(model, style, known_models)
final_output.extend(output) final_output.extend(output)
for refto, refs in references.items(): for refto, refs in references.items():
@ -85,7 +77,7 @@ def sql_delete(app, style, connection):
to_delete = set() to_delete = set()
references_to_delete = {} 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: for model in app_models:
if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names:
# The table exists, so it needs to be dropped # 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." "Returns a list of the custom table modifying SQL statements for the given app."
output = [] output = []
app_models = filtered_app_models(app, connection.alias) app_models = router.get_migratable_models(app, connection.alias)
for model in app_models: for model in app_models:
output.extend(custom_sql_for_model(model, style, connection)) 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): def sql_indexes(app, style, connection):
"Returns a list of the CREATE INDEX SQL statements for all models in the given app." "Returns a list of the CREATE INDEX SQL statements for all models in the given app."
output = [] 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)) output.extend(connection.creation.sql_indexes_for_model(model, style))
return output return output
@ -148,7 +140,7 @@ def sql_indexes(app, style, connection):
def sql_destroy_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." "Returns a list of the DROP INDEX SQL statements for all models in the given app."
output = [] 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)) output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
return output return output

View File

@ -1272,11 +1272,9 @@ class BaseDatabaseIntrospection(object):
from django.db import models, router from django.db import models, router
tables = set() tables = set()
for app in models.get_apps(): 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: if not model._meta.managed:
continue continue
if not router.allow_migrate(self.connection.alias, model):
continue
tables.add(model._meta.db_table) tables.add(model._meta.db_table)
tables.update(f.m2m_db_table() for f in model._meta.local_many_to_many) tables.update(f.m2m_db_table() for f in model._meta.local_many_to_many)
tables = list(tables) tables = list(tables)
@ -1294,9 +1292,7 @@ class BaseDatabaseIntrospection(object):
from django.db import models, router from django.db import models, router
all_models = [] all_models = []
for app in models.get_apps(): for app in models.get_apps():
for model in models.get_models(app): all_models.extend(router.get_migratable_models(app, self.connection.alias))
if router.allow_migrate(self.connection.alias, model):
all_models.append(model)
tables = list(map(self.table_name_converter, tables)) tables = list(map(self.table_name_converter, tables))
return set([ return set([
m for m in all_models m for m in all_models
@ -1311,13 +1307,11 @@ class BaseDatabaseIntrospection(object):
sequence_list = [] sequence_list = []
for app in apps: 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: if not model._meta.managed:
continue continue
if model._meta.swapped: if model._meta.swapped:
continue continue
if not router.allow_migrate(self.connection.alias, model):
continue
for f in model._meta.local_fields: for f in model._meta.local_fields:
if isinstance(f, models.AutoField): if isinstance(f, models.AutoField):
sequence_list.append({'table': model._meta.db_table, 'column': f.column}) sequence_list.append({'table': model._meta.db_table, 'column': f.column})

View File

@ -278,3 +278,11 @@ class ConnectionRouter(object):
if allow is not None: if allow is not None:
return allow return allow
return True 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)]