Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add get_migratable_models util method to ConnectionRouter

  • Loading branch information...
commit 650b6fd90efea058c54800d2ba0e0976547894a3 1 parent 2992f42
Claude Paroz authored October 16, 2013
5  django/core/management/commands/flush.py
@@ -94,8 +94,5 @@ def emit_post_migrate(verbosity, interactive, database):
94 94
         # respond as if the database had been migrated from scratch.
95 95
         all_models = []
96 96
         for app in models.get_apps():
97  
-            all_models.extend([
98  
-                m for m in models.get_models(app, include_auto_created=True)
99  
-                if router.allow_migrate(database, m)
100  
-            ])
  97
+            all_models.extend(router.get_migratable_models(app, database, include_auto_created=True))
101 98
         emit_post_migrate_signal(set(all_models), verbosity, interactive, database)
5  django/core/management/commands/migrate.py
@@ -153,10 +153,7 @@ def sync_apps(self, connection, apps):
153 153
         # Build the manifest of apps and models that are to be synchronized
154 154
         all_models = [
155 155
             (app.__name__.split('.')[-2],
156  
-                [
157  
-                    m for m in models.get_models(app, include_auto_created=True)
158  
-                    if router.allow_migrate(connection.alias, m)
159  
-                ])
  156
+                router.get_migratable_models(app, connection.alias, include_auto_created=True))
160 157
             for app in models.get_apps() if app.__name__.split('.')[-2] in apps
161 158
         ]
162 159
 
18  django/core/management/sql.py
@@ -10,14 +10,6 @@
10 10
 from django.db import models, router
11 11
 
12 12
 
13  
-def filtered_app_models(app, db_alias, include_auto_created=False):
14  
-    """
15  
-    Return app models allowed to be synchronized on provided db.
16  
-    """
17  
-    return [model for model in models.get_models(app, include_auto_created=include_auto_created)
18  
-            if router.allow_migrate(db_alias, model)]
19  
-
20  
-
21 13
 def sql_create(app, style, connection):
22 14
     "Returns a list of the CREATE TABLE SQL statements for the given app."
23 15
 
@@ -38,7 +30,7 @@ def sql_create(app, style, connection):
38 30
     known_models = set(model for model in connection.introspection.installed_models(tables) if model not in app_models)
39 31
     pending_references = {}
40 32
 
41  
-    for model in filtered_app_models(app, connection.alias, include_auto_created=True):
  33
+    for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
42 34
         output, references = connection.creation.sql_create_model(model, style, known_models)
43 35
         final_output.extend(output)
44 36
         for refto, refs in references.items():
@@ -85,7 +77,7 @@ def sql_delete(app, style, connection):
85 77
     to_delete = set()
86 78
 
87 79
     references_to_delete = {}
88  
-    app_models = filtered_app_models(app, connection.alias, include_auto_created=True)
  80
+    app_models = router.get_migratable_models(app, connection.alias, include_auto_created=True)
89 81
     for model in app_models:
90 82
         if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names:
91 83
             # The table exists, so it needs to be dropped
@@ -129,7 +121,7 @@ def sql_custom(app, style, connection):
129 121
     "Returns a list of the custom table modifying SQL statements for the given app."
130 122
     output = []
131 123
 
132  
-    app_models = filtered_app_models(app, connection.alias)
  124
+    app_models = router.get_migratable_models(app, connection.alias)
133 125
 
134 126
     for model in app_models:
135 127
         output.extend(custom_sql_for_model(model, style, connection))
@@ -140,7 +132,7 @@ def sql_custom(app, style, connection):
140 132
 def sql_indexes(app, style, connection):
141 133
     "Returns a list of the CREATE INDEX SQL statements for all models in the given app."
142 134
     output = []
143  
-    for model in filtered_app_models(app, connection.alias, include_auto_created=True):
  135
+    for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
144 136
         output.extend(connection.creation.sql_indexes_for_model(model, style))
145 137
     return output
146 138
 
@@ -148,7 +140,7 @@ def sql_indexes(app, style, connection):
148 140
 def sql_destroy_indexes(app, style, connection):
149 141
     "Returns a list of the DROP INDEX SQL statements for all models in the given app."
150 142
     output = []
151  
-    for model in filtered_app_models(app, connection.alias, include_auto_created=True):
  143
+    for model in router.get_migratable_models(app, connection.alias, include_auto_created=True):
152 144
         output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
153 145
     return output
154 146
 
12  django/db/backends/__init__.py
@@ -1272,11 +1272,9 @@ def django_table_names(self, only_existing=False):
1272 1272
         from django.db import models, router
1273 1273
         tables = set()
1274 1274
         for app in models.get_apps():
1275  
-            for model in models.get_models(app):
  1275
+            for model in router.get_migratable_models(app, self.connection.alias):
1276 1276
                 if not model._meta.managed:
1277 1277
                     continue
1278  
-                if not router.allow_migrate(self.connection.alias, model):
1279  
-                    continue
1280 1278
                 tables.add(model._meta.db_table)
1281 1279
                 tables.update(f.m2m_db_table() for f in model._meta.local_many_to_many)
1282 1280
         tables = list(tables)
@@ -1294,9 +1292,7 @@ def installed_models(self, tables):
1294 1292
         from django.db import models, router
1295 1293
         all_models = []
1296 1294
         for app in models.get_apps():
1297  
-            for model in models.get_models(app):
1298  
-                if router.allow_migrate(self.connection.alias, model):
1299  
-                    all_models.append(model)
  1295
+            all_models.extend(router.get_migratable_models(app, self.connection.alias))
1300 1296
         tables = list(map(self.table_name_converter, tables))
1301 1297
         return set([
1302 1298
             m for m in all_models
@@ -1311,13 +1307,11 @@ def sequence_list(self):
1311 1307
         sequence_list = []
1312 1308
 
1313 1309
         for app in apps:
1314  
-            for model in models.get_models(app):
  1310
+            for model in router.get_migratable_models(app, self.connection.alias):
1315 1311
                 if not model._meta.managed:
1316 1312
                     continue
1317 1313
                 if model._meta.swapped:
1318 1314
                     continue
1319  
-                if not router.allow_migrate(self.connection.alias, model):
1320  
-                    continue
1321 1315
                 for f in model._meta.local_fields:
1322 1316
                     if isinstance(f, models.AutoField):
1323 1317
                         sequence_list.append({'table': model._meta.db_table, 'column': f.column})
8  django/db/utils.py
@@ -278,3 +278,11 @@ def allow_migrate(self, db, model):
278 278
                 if allow is not None:
279 279
                     return allow
280 280
         return True
  281
+
  282
+    def get_migratable_models(self, app, db, include_auto_created=False):
  283
+        """
  284
+        Return app models allowed to be synchronized on provided db.
  285
+        """
  286
+        from .models import get_models
  287
+        return [model for model in get_models(app, include_auto_created=include_auto_created)
  288
+                if self.allow_migrate(db, model)]

0 notes on commit 650b6fd

Please sign in to comment.
Something went wrong with that request. Please try again.