Skip to content

Commit

Permalink
Refs #26889 -- Refactored SchemaEditor to allow backend specific inde…
Browse files Browse the repository at this point in the history
…xes.
  • Loading branch information
jdufresne authored and timgraham committed Jul 15, 2016
1 parent f8bfa80 commit 3f76d14
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
15 changes: 11 additions & 4 deletions django/db/backends/base/schema.py
Expand Up @@ -430,8 +430,7 @@ def add_field(self, model, field):
}
self.execute(sql)
# Add an index, if required
if field.db_index and not field.unique:
self.deferred_sql.append(self._create_index_sql(model, [field]))
self.deferred_sql.extend(self._field_indexes_sql(model, field))
# Add any FK constraints later
if field.remote_field and self.connection.features.supports_foreign_keys and field.db_constraint:
self.deferred_sql.append(self._create_fk_sql(model, field, "_fk_%(to_table)s_%(to_column)s"))
Expand Down Expand Up @@ -897,14 +896,22 @@ def _model_indexes_sql(self, model):
return []
output = []
for field in model._meta.local_fields:
if self._field_should_be_indexed(model, field):
output.append(self._create_index_sql(model, [field], suffix=""))
output.extend(self._field_indexes_sql(model, field))

for field_names in model._meta.index_together:
fields = [model._meta.get_field(field) for field in field_names]
output.append(self._create_index_sql(model, fields, suffix="_idx"))
return output

def _field_indexes_sql(self, model, field):
"""
Return a list of all index SQL statements for the specified field.
"""
output = []
if self._field_should_be_indexed(model, field):
output.append(self._create_index_sql(model, [field]))
return output

def _field_should_be_indexed(self, model, field):
return field.db_index and not field.unique

Expand Down
16 changes: 3 additions & 13 deletions django/db/backends/postgresql/schema.py
Expand Up @@ -17,21 +17,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
def quote_value(self, value):
return psycopg2.extensions.adapt(value)

def add_field(self, model, field):
super(DatabaseSchemaEditor, self).add_field(model, field)
def _field_indexes_sql(self, model, field):
output = super(DatabaseSchemaEditor, self)._field_indexes_sql(model, field)
like_index_statement = self._create_like_index_sql(model, field)
if like_index_statement is not None:
self.deferred_sql.append(like_index_statement)

def _model_indexes_sql(self, model):
output = super(DatabaseSchemaEditor, self)._model_indexes_sql(model)
if not model._meta.managed or model._meta.proxy or model._meta.swapped:
return output

for field in model._meta.local_fields:
like_index_statement = self._create_like_index_sql(model, field)
if like_index_statement is not None:
output.append(like_index_statement)
output.append(like_index_statement)
return output

def _create_like_index_sql(self, model, field):
Expand Down
2 changes: 1 addition & 1 deletion tests/indexes/tests.py
Expand Up @@ -46,7 +46,7 @@ def test_postgresql_text_indexes(self):
from .models import IndexedArticle
index_sql = connection.schema_editor()._model_indexes_sql(IndexedArticle)
self.assertEqual(len(index_sql), 5)
self.assertIn('("headline" varchar_pattern_ops)', index_sql[2])
self.assertIn('("headline" varchar_pattern_ops)', index_sql[1])
self.assertIn('("body" text_pattern_ops)', index_sql[3])
# unique=True and db_index=True should only create the varchar-specific
# index (#19441).
Expand Down

0 comments on commit 3f76d14

Please sign in to comment.