Permalink
Browse files

Call get_models() with include_auto_created=True

When we call get_models() without the include_auto_created=True
argument, we will not display indexes that are auto-created by django.
It will only show the indexes that are explicitly defined by the user.
  • Loading branch information...
ashchristopher committed Sep 7, 2012
1 parent 335a9f9 commit d01486d7b34795b929cba04d28327878f1502861
@@ -128,7 +128,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 models.get_models(app):
+ for model in models.get_models(app, include_auto_created=True):
output.extend(connection.creation.sql_indexes_for_model(model, style))
return output
@@ -0,0 +1,17 @@
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+@python_2_unicode_compatible
+class Comment(models.Model):
+ pass
+
+
+@python_2_unicode_compatible
+class Post(models.Model):
+ comments = models.ManyToManyField('Comment')
+
+
+@python_2_unicode_compatible
+class UserPost(models.Model):
+ uuid = models.CharField(max_length=120, db_index=True)
@@ -0,0 +1,45 @@
+from django.test import TestCase
+from django.core import management
+from django.utils.six import StringIO
+from django.core.management import color
+
+
+class TestSqlIndexesTestCase(TestCase):
+
+ def _call_command(self, app):
+ new_io = StringIO()
+
+ kwargs = {
+ 'stdout': new_io,
+ 'stderr': new_io,
+ }
+
+ management.call_command('sqlindexes', app, **kwargs)
+
+ return new_io
+
+ def _statements_including(self, statements, key):
+ return [i for i in statements if key in i]
+
+ def setUp(self):
+ # need to force the output not to use ANSI color
+ self.old_supports_color = color.supports_color
+ color.supports_color = lambda: False
+
+ def tearDown(self):
+ # need to make sure that we replace the new supports_color()
+ # method with the original one
+ color.supports_color = self.old_supports_color
+
+ def test_user_defined_indexes(self):
+ output = self._call_command('management_commands')
+ output = output.getvalue()
+ create_statements = [stmt for stmt in output.split('\n') if 'management_commands' in stmt]
+
+ # make sure we have 2 index creations for Post.comments
+ num_post_comments_indexes = len(self._statements_including(create_statements, 'management_commands_post_comments'))
+ self.assertEqual(num_post_comments_indexes, 2)
+
+ # make sure we have 1 user-defined index creation for UserPost.uuid
+ num_user_indexes = len(self._statements_including(create_statements, 'management_commands_userpost'))
+ self.assertEqual(num_user_indexes, 1)

0 comments on commit d01486d

Please sign in to comment.