Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Ticket #15697 - Call get_models() with include_auto_created=True #346

Closed
wants to merge 1 commit into from

4 participants

Ash Christopher Nathan Duthoit Preston Holmes Claude Paroz
Ash Christopher

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.

Ash Christopher ashchristopher 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.
d01486d
Nathan Duthoit

Looks good to me :+1:

Preston Holmes
Collaborator

Looks good, but I'd move the tests to tests/regressiontests/admin_scripts/tests.py - its a bit of a monster file, but that is where all the admin script tests live currently - and it would be best not to create a brand new test app for this case. This is also worth a brief note in the release notes - while it would be an edge case, anyone doing anything automated with the output of those commands, could be surprised by the change in behavior.

Claude Paroz
Collaborator

Thanks, fix pushed in 8010289

Claude Paroz claudep closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 7, 2012
  1. Ash Christopher

    Call get_models() with include_auto_created=True

    ashchristopher authored
    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.
This page is out of date. Refresh to see the latest.
2  django/core/management/sql.py
View
@@ -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  tests/regressiontests/management_commands/__init__.py
View
No changes.
17 tests/regressiontests/management_commands/models.py
View
@@ -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)
45 tests/regressiontests/management_commands/tests.py
View
@@ -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)
Something went wrong with that request. Please try again.