Refs #26167 -- Made DatabaseSchemaEditor._create_index_sql()'s fields argument optional and kwarg-only.

This commit is contained in:
Hannes Ljungberg 2020-12-23 11:08:42 +01:00 committed by Mariusz Felisiak
parent 3bca95cca2
commit d23cb83c99
5 changed files with 31 additions and 18 deletions

View File

@ -18,9 +18,9 @@ class PostGISSchemaEditor(DatabaseSchemaEditor):
return True return True
return super()._field_should_be_indexed(model, field) return super()._field_should_be_indexed(model, field)
def _create_index_sql(self, model, fields, **kwargs): def _create_index_sql(self, model, *, fields=None, **kwargs):
if len(fields) != 1 or not hasattr(fields[0], 'geodetic'): if fields is None or len(fields) != 1 or not hasattr(fields[0], 'geodetic'):
return super()._create_index_sql(model, fields, **kwargs) return super()._create_index_sql(model, fields=fields, **kwargs)
field = fields[0] field = fields[0]
field_column = self.quote_name(field.column) field_column = self.quote_name(field.column)

View File

@ -407,7 +407,7 @@ class BaseDatabaseSchemaEditor:
# Created indexes # Created indexes
for field_names in news.difference(olds): for field_names in news.difference(olds):
fields = [model._meta.get_field(field) for field in field_names] fields = [model._meta.get_field(field) for field in field_names]
self.execute(self._create_index_sql(model, fields, suffix="_idx")) self.execute(self._create_index_sql(model, fields=fields, suffix='_idx'))
def _delete_composed_index(self, model, fields, constraint_kwargs, sql): def _delete_composed_index(self, model, fields, constraint_kwargs, sql):
meta_constraint_names = {constraint.name for constraint in model._meta.constraints} meta_constraint_names = {constraint.name for constraint in model._meta.constraints}
@ -778,7 +778,7 @@ class BaseDatabaseSchemaEditor:
# False | True | True | False # False | True | True | False
# True | True | True | False # True | True | True | False
if (not old_field.db_index or old_field.unique) and new_field.db_index and not new_field.unique: if (not old_field.db_index or old_field.unique) and new_field.db_index and not new_field.unique:
self.execute(self._create_index_sql(model, [new_field])) self.execute(self._create_index_sql(model, fields=[new_field]))
# Type alteration on primary key? Then we need to alter the column # Type alteration on primary key? Then we need to alter the column
# referring to us. # referring to us.
rels_to_update = [] rels_to_update = []
@ -990,7 +990,7 @@ class BaseDatabaseSchemaEditor:
columns=Columns(model._meta.db_table, columns, self.quote_name), columns=Columns(model._meta.db_table, columns, self.quote_name),
) )
def _create_index_sql(self, model, fields, *, name=None, suffix='', using='', def _create_index_sql(self, model, *, fields=None, name=None, suffix='', using='',
db_tablespace=None, col_suffixes=(), sql=None, opclasses=(), db_tablespace=None, col_suffixes=(), sql=None, opclasses=(),
condition=None, include=None): condition=None, include=None):
""" """
@ -1043,7 +1043,7 @@ class BaseDatabaseSchemaEditor:
for field_names in model._meta.index_together: for field_names in model._meta.index_together:
fields = [model._meta.get_field(field) for field in field_names] fields = [model._meta.get_field(field) for field in field_names]
output.append(self._create_index_sql(model, fields, suffix="_idx")) output.append(self._create_index_sql(model, fields=fields, suffix='_idx'))
for index in model._meta.indexes: for index in model._meta.indexes:
output.append(index.create_sql(model, self)) output.append(index.create_sql(model, self))
@ -1055,7 +1055,7 @@ class BaseDatabaseSchemaEditor:
""" """
output = [] output = []
if self._field_should_be_indexed(model, field): if self._field_should_be_indexed(model, field):
output.append(self._create_index_sql(model, [field])) output.append(self._create_index_sql(model, fields=[field]))
return output return output
def _field_should_be_altered(self, old_field, new_field): def _field_should_be_altered(self, old_field, new_field):

View File

@ -126,7 +126,9 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if first_field.get_internal_type() == 'ForeignKey': if first_field.get_internal_type() == 'ForeignKey':
constraint_names = self._constraint_names(model, [first_field.column], index=True) constraint_names = self._constraint_names(model, [first_field.column], index=True)
if not constraint_names: if not constraint_names:
self.execute(self._create_index_sql(model, [first_field], suffix="")) self.execute(
self._create_index_sql(model, fields=[first_field], suffix='')
)
return super()._delete_composed_index(model, fields, *args) return super()._delete_composed_index(model, fields, *args)
def _set_field_new_type_null_status(self, field, new_type): def _set_field_new_type_null_status(self, field, new_type):

View File

@ -83,9 +83,19 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
if '[' in db_type: if '[' in db_type:
return None return None
if db_type.startswith('varchar'): if db_type.startswith('varchar'):
return self._create_index_sql(model, [field], suffix='_like', opclasses=['varchar_pattern_ops']) return self._create_index_sql(
model,
fields=[field],
suffix='_like',
opclasses=['varchar_pattern_ops'],
)
elif db_type.startswith('text'): elif db_type.startswith('text'):
return self._create_index_sql(model, [field], suffix='_like', opclasses=['text_pattern_ops']) return self._create_index_sql(
model,
fields=[field],
suffix='_like',
opclasses=['text_pattern_ops'],
)
return None return None
def _alter_column_type_sql(self, model, old_field, new_field, new_type): def _alter_column_type_sql(self, model, old_field, new_field, new_type):
@ -215,13 +225,13 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return super()._delete_index_sql(model, name, sql) return super()._delete_index_sql(model, name, sql)
def _create_index_sql( def _create_index_sql(
self, model, fields, *, name=None, suffix='', using='', self, model, *, fields=None, name=None, suffix='', using='',
db_tablespace=None, col_suffixes=(), sql=None, opclasses=(), db_tablespace=None, col_suffixes=(), sql=None, opclasses=(),
condition=None, concurrently=False, include=None, condition=None, concurrently=False, include=None,
): ):
sql = self.sql_create_index if not concurrently else self.sql_create_index_concurrently sql = self.sql_create_index if not concurrently else self.sql_create_index_concurrently
return super()._create_index_sql( return super()._create_index_sql(
model, fields, name=name, suffix=suffix, using=using, db_tablespace=db_tablespace, model, fields=fields, name=name, suffix=suffix, using=using,
col_suffixes=col_suffixes, sql=sql, opclasses=opclasses, condition=condition, db_tablespace=db_tablespace, col_suffixes=col_suffixes, sql=sql,
include=include, opclasses=opclasses, condition=condition, include=include,
) )

View File

@ -66,9 +66,10 @@ class Index:
col_suffixes = [order[1] for order in self.fields_orders] col_suffixes = [order[1] for order in self.fields_orders]
condition = self._get_condition_sql(model, schema_editor) condition = self._get_condition_sql(model, schema_editor)
return schema_editor._create_index_sql( return schema_editor._create_index_sql(
model, fields, name=self.name, using=using, db_tablespace=self.db_tablespace, model, fields=fields, name=self.name, using=using,
col_suffixes=col_suffixes, opclasses=self.opclasses, condition=condition, db_tablespace=self.db_tablespace, col_suffixes=col_suffixes,
include=include, **kwargs, opclasses=self.opclasses, condition=condition, include=include,
**kwargs,
) )
def remove_sql(self, model, schema_editor, **kwargs): def remove_sql(self, model, schema_editor, **kwargs):