mirror of https://github.com/django/django.git
Fixed #15697 -- Made sqlindexes aware of auto-created tables
Thanks mbertheau for the report and Ash Christopher for the initial patch.
This commit is contained in:
parent
5939864616
commit
8010289ea2
|
@ -133,7 +133,7 @@ def sql_custom(app, style, connection):
|
||||||
def sql_indexes(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."
|
"Returns a list of the CREATE INDEX SQL statements for all models in the given app."
|
||||||
output = []
|
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))
|
output.extend(connection.creation.sql_indexes_for_model(model, style))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
@ -141,7 +141,7 @@ def sql_indexes(app, style, connection):
|
||||||
def sql_destroy_indexes(app, style, connection):
|
def sql_destroy_indexes(app, style, connection):
|
||||||
"Returns a list of the DROP INDEX SQL statements for all models in the given app."
|
"Returns a list of the DROP INDEX SQL statements for all models in the given app."
|
||||||
output = []
|
output = []
|
||||||
for model in models.get_models(app):
|
for model in models.get_models(app, include_auto_created=True):
|
||||||
output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
|
output.extend(connection.creation.sql_destroy_indexes_for_model(model, style))
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,12 @@ from django.db import models
|
||||||
from django.utils.encoding import python_2_unicode_compatible
|
from django.utils.encoding import python_2_unicode_compatible
|
||||||
|
|
||||||
|
|
||||||
|
@python_2_unicode_compatible
|
||||||
|
class Comment(models.Model):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@python_2_unicode_compatible
|
@python_2_unicode_compatible
|
||||||
class Book(models.Model):
|
class Book(models.Model):
|
||||||
title = models.CharField(max_length=100, db_index=True)
|
title = models.CharField(max_length=100, db_index=True)
|
||||||
|
comments = models.ManyToManyField(Comment)
|
||||||
|
|
|
@ -12,47 +12,43 @@ from django.utils import six
|
||||||
|
|
||||||
class SQLCommandsTestCase(TestCase):
|
class SQLCommandsTestCase(TestCase):
|
||||||
"""Tests for several functions in django/core/management/sql.py"""
|
"""Tests for several functions in django/core/management/sql.py"""
|
||||||
|
def count_ddl(self, output, cmd):
|
||||||
|
return len([o for o in output if o.startswith(cmd)])
|
||||||
|
|
||||||
def test_sql_create(self):
|
def test_sql_create(self):
|
||||||
app = models.get_app('commands_sql')
|
app = models.get_app('commands_sql')
|
||||||
output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_create(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
|
create_tables = [o for o in output if o.startswith('CREATE TABLE')]
|
||||||
|
self.assertEqual(len(create_tables), 3)
|
||||||
# Lower so that Oracle's upper case tbl names wont break
|
# Lower so that Oracle's upper case tbl names wont break
|
||||||
sql = output[0].lower()
|
sql = create_tables[-1].lower()
|
||||||
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
six.assertRegex(self, sql, r'^create table .commands_sql_book.*')
|
||||||
|
|
||||||
def test_sql_delete(self):
|
def test_sql_delete(self):
|
||||||
app = models.get_app('commands_sql')
|
app = models.get_app('commands_sql')
|
||||||
output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_delete(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
# Oracle produces DROP SEQUENCE and DROP TABLE for this command.
|
drop_tables = [o for o in output if o.startswith('DROP TABLE')]
|
||||||
if connections[DEFAULT_DB_ALIAS].vendor == 'oracle':
|
self.assertEqual(len(drop_tables), 3)
|
||||||
sql = output[1].lower()
|
# Lower so that Oracle's upper case tbl names wont break
|
||||||
else:
|
sql = drop_tables[-1].lower()
|
||||||
sql = output[0].lower()
|
six.assertRegex(self, sql, r'^drop table .commands_sql_comment.*')
|
||||||
six.assertRegex(self, sql, r'^drop table .commands_sql_book.*')
|
|
||||||
|
|
||||||
def test_sql_indexes(self):
|
def test_sql_indexes(self):
|
||||||
app = models.get_app('commands_sql')
|
app = models.get_app('commands_sql')
|
||||||
output = sql_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
# PostgreSQL creates two indexes
|
# PostgreSQL creates one additional index for CharField
|
||||||
self.assertIn(len(output), [1, 2])
|
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
||||||
self.assertTrue(output[0].startswith("CREATE INDEX"))
|
|
||||||
|
|
||||||
def test_sql_destroy_indexes(self):
|
def test_sql_destroy_indexes(self):
|
||||||
app = models.get_app('commands_sql')
|
app = models.get_app('commands_sql')
|
||||||
output = sql_destroy_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_destroy_indexes(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
# PostgreSQL creates two indexes
|
# PostgreSQL creates one additional index for CharField
|
||||||
self.assertIn(len(output), [1, 2])
|
self.assertIn(self.count_ddl(output, 'DROP INDEX'), [3, 4])
|
||||||
self.assertTrue(output[0].startswith("DROP INDEX"))
|
|
||||||
|
|
||||||
def test_sql_all(self):
|
def test_sql_all(self):
|
||||||
app = models.get_app('commands_sql')
|
app = models.get_app('commands_sql')
|
||||||
output = sql_all(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
output = sql_all(app, no_style(), connections[DEFAULT_DB_ALIAS])
|
||||||
self.assertTrue(output[0].startswith('CREATE TABLE'))
|
|
||||||
if connections[DEFAULT_DB_ALIAS].vendor == 'oracle':
|
self.assertEqual(self.count_ddl(output, 'CREATE TABLE'), 3)
|
||||||
self.assertEqual(len(output), 4) # Oracle creates a table, a sequence, a trigger and an index
|
# PostgreSQL creates one additional index for CharField
|
||||||
self.assertIn('CREATE SEQUENCE', output[1])
|
self.assertIn(self.count_ddl(output, 'CREATE INDEX'), [3, 4])
|
||||||
self.assertIn('CREATE OR REPLACE TRIGGER', output[2])
|
|
||||||
self.assertTrue(output[3].startswith('CREATE INDEX'))
|
|
||||||
else:
|
|
||||||
# PostgreSQL creates two indexes
|
|
||||||
self.assertIn(len(output), [2, 3])
|
|
||||||
self.assertTrue(output[1].startswith('CREATE INDEX'))
|
|
||||||
|
|
Loading…
Reference in New Issue