Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #16 from akaariai/ticket_18218

Made table_names() output sorted.
  • Loading branch information...
commit 97c899284000df8d733f5cff7ec8e8406a455c8c 2 parents 75743c1 + 527cce8
Adrian Holovaty authored April 28, 2012
2  django/core/management/commands/inspectdb.py
@@ -42,7 +42,7 @@ def handle_inspection(self, options):
42 42
         yield 'from %s import models' % self.db_module
43 43
         yield ''
44 44
         known_models = []
45  
-        for table_name in connection.introspection.get_table_list(cursor):
  45
+        for table_name in connection.introspection.table_names(cursor):
46 46
             yield 'class %s(models.Model):' % table2model(table_name)
47 47
             known_models.append(table2model(table_name))
48 48
             try:
2  django/core/management/sql.py
@@ -63,7 +63,7 @@ def sql_delete(app, style, connection):
63 63
 
64 64
     # Figure out which tables already exist
65 65
     if cursor:
66  
-        table_names = connection.introspection.get_table_list(cursor)
  66
+        table_names = connection.introspection.table_names(cursor)
67 67
     else:
68 68
         table_names = []
69 69
 
21  django/db/backends/__init__.py
@@ -898,10 +898,23 @@ def table_name_converter(self, name):
898 898
         """
899 899
         return name
900 900
 
901  
-    def table_names(self):
902  
-        "Returns a list of names of all tables that exist in the database."
903  
-        cursor = self.connection.cursor()
904  
-        return self.get_table_list(cursor)
  901
+    def table_names(self, cursor=None):
  902
+        """
  903
+        Returns a list of names of all tables that exist in the database.
  904
+        The returned table list is sorted by Python's default sorting. We
  905
+        do NOT use database's ORDER BY here to avoid subtle differences
  906
+        in sorting order between databases.
  907
+        """
  908
+        if cursor is None:
  909
+            cursor = self.connection.cursor()
  910
+        return sorted(self.get_table_list(cursor))
  911
+
  912
+    def get_table_list(self, cursor):
  913
+        """
  914
+        Returns an unsorted list of names of all tables that exist in the
  915
+        database.
  916
+        """
  917
+        raise NotImplementedError
905 918
 
906 919
     def django_table_names(self, only_existing=False):
907 920
         """
2  django/db/backends/mysql/base.py
@@ -454,7 +454,7 @@ def check_constraints(self, table_names=None):
454 454
         """
455 455
         cursor = self.cursor()
456 456
         if table_names is None:
457  
-            table_names = self.introspection.get_table_list(cursor)
  457
+            table_names = self.introspection.table_names(cursor)
458 458
         for table_name in table_names:
459 459
             primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
460 460
             if not primary_key_column_name:
2  django/db/backends/sqlite3/base.py
@@ -295,7 +295,7 @@ def check_constraints(self, table_names=None):
295 295
         """
296 296
         cursor = self.cursor()
297 297
         if table_names is None:
298  
-            table_names = self.introspection.get_table_list(cursor)
  298
+            table_names = self.introspection.table_names(cursor)
299 299
         for table_name in table_names:
300 300
             primary_key_column_name = self.introspection.get_primary_key_column(cursor, table_name)
301 301
             if not primary_key_column_name:
1  tests/regressiontests/introspection/tests.py
@@ -40,6 +40,7 @@ class IntrospectionTests(TestCase):
40 40
 
41 41
     def test_table_names(self):
42 42
         tl = connection.introspection.table_names()
  43
+        self.assertEqual(tl, sorted(tl))
43 44
         self.assertTrue(Reporter._meta.db_table in tl,
44 45
                      "'%s' isn't in table_list()." % Reporter._meta.db_table)
45 46
         self.assertTrue(Article._meta.db_table in tl,

0 notes on commit 97c8992

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