Author: jpellerin
Date: 2006-07-10 14:36:16 -0500 (Mon, 10 Jul 2006)
New Revision: 3314

Modified:
   django/branches/multiple-db-support/django/core/management.py
Log:
[multi-db] Minor refactoring: moved collation of sql statements by connection 
name to function.

Modified: django/branches/multiple-db-support/django/core/management.py
===================================================================
--- django/branches/multiple-db-support/django/core/management.py       
2006-07-10 19:27:40 UTC (rev 3313)
+++ django/branches/multiple-db-support/django/core/management.py       
2006-07-10 19:36:16 UTC (rev 3314)
@@ -49,6 +49,9 @@
 if sys.platform == 'win32' or not sys.stdout.isatty():
     disable_termcolors()
 
+# singleton representing the default connection
+_default = object()
+
 def _is_valid_dir_name(s):
     return bool(re.search(r'^\w+$', s))
 
@@ -85,9 +88,6 @@
     "Returns a list of the CREATE TABLE SQL statements for the given app."
     from django.db import models
 
-    # singleton representing the default connection
-    _default = object()
-
     # final output will be divided by comments into sections for each
     # named connection, if there are any named connections
     connection_output = {}
@@ -134,19 +134,7 @@
         for refklass, statements in many_many.items():
             output.extend(statements)
 
-    if len(connection_output.keys()) == 1:
-        # all for the default connection
-        for statements in connection_output.values():
-            final_output.extend(statements)
-    else:
-        for connection_name, statements in connection_output.items():
-            if connection_name is _default:
-                connection_name = '(default)'
-            final_output.append(' -- The following statements are for 
connection: %s' % connection_name)
-            final_output.extend(statements)
-            final_output.append(' -- END statements for %s\n' %
-                                connection_name)
-            
+    final_output = _collate(connection_output)
     # Handle references to tables that are from other apps
     # but don't exist physically
     not_installed_models = set(pending_references.keys())
@@ -426,13 +414,18 @@
 
 def get_sql_indexes(app):
     "Returns a list of the CREATE INDEX SQL statements for the given app."
-    from django.db import backend, models
-    output = []
+    from django.db import models
+    connection_output = {}
 
     for klass in models.get_models(app):
-        builder = klass._meta.connection_info.get_creation_module().builder
+        opts = klass._meta
+        connection_name = opts.db_connection or _default
+        output = connection_output.setdefault(connection_name, [])
+        info = opts.connection_info
+        builder = info.get_creation_module().builder
         output.extend(map(str, builder.get_create_indexes(klass, style)))
-    return output
+    return _collate(connection_output)
+
 get_sql_indexes.help_doc = "Prints the CREATE INDEX SQL statements for the 
given model module name(s)."
 get_sql_indexes.args = APP_ARGS
 
@@ -442,6 +435,24 @@
 get_sql_all.help_doc = "Prints the CREATE TABLE, initial-data and CREATE INDEX 
SQL statements for the given model module name(s)."
 get_sql_all.args = APP_ARGS
 
+def _collate(connection_output):
+    final_output = []
+    if len(connection_output.keys()) == 1:
+        # all for the default connection
+        for statements in connection_output.values():
+            final_output.extend(statements)
+    else:
+        for connection_name, statements in connection_output.items():
+            if not statements:
+                continue
+            if connection_name is _default:
+                connection_name = '(default)'
+            final_output.append(' -- The following statements are for 
connection: %s' % connection_name)
+            final_output.extend(statements)
+            final_output.append(' -- END statements for %s\n' %
+                                connection_name)
+    return map(str, final_output)
+
 def syncdb():
     "Creates the database tables for all apps in INSTALLED_APPS whose tables 
haven't already been created."
     from django.db import connection, transaction, models, get_creation_module


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates
-~----------~----~----~----~------~----~------~--~---

Reply via email to