Refs #30913 -- Added BaseDatabaseSchemaEditor._index_condition_sql().

This commit is contained in:
Hannes Ljungberg 2020-04-24 07:03:33 +02:00 committed by Mariusz Felisiak
parent e0757c49ac
commit 104493311e
1 changed files with 17 additions and 14 deletions

View File

@ -951,6 +951,11 @@ class BaseDatabaseSchemaEditor:
return ' ' + self.connection.ops.tablespace_sql(db_tablespace) return ' ' + self.connection.ops.tablespace_sql(db_tablespace)
return '' return ''
def _index_condition_sql(self, condition):
if condition:
return ' WHERE ' + condition
return ''
def _create_index_sql(self, model, fields, *, name=None, suffix='', using='', def _create_index_sql(self, model, fields, *, name=None, suffix='', using='',
db_tablespace=None, col_suffixes=(), sql=None, opclasses=(), db_tablespace=None, col_suffixes=(), sql=None, opclasses=(),
condition=None): condition=None):
@ -977,7 +982,7 @@ class BaseDatabaseSchemaEditor:
using=using, using=using,
columns=self._index_columns(table, columns, col_suffixes, opclasses), columns=self._index_columns(table, columns, col_suffixes, opclasses),
extra=tablespace_sql, extra=tablespace_sql,
condition=(' WHERE ' + condition) if condition else '', condition=self._index_condition_sql(condition),
) )
def _delete_index_sql(self, model, name, sql=None): def _delete_index_sql(self, model, name, sql=None):
@ -1097,20 +1102,18 @@ class BaseDatabaseSchemaEditor:
name = self.quote_name(name) name = self.quote_name(name)
columns = Columns(table, columns, self.quote_name) columns = Columns(table, columns, self.quote_name)
if condition: if condition:
return Statement( if not self.connection.features.supports_partial_indexes:
self.sql_create_unique_index, return None
table=table, sql = self.sql_create_unique_index
name=name,
columns=columns,
condition=' WHERE ' + condition,
) if self.connection.features.supports_partial_indexes else None
else: else:
return Statement( sql = self.sql_create_unique
self.sql_create_unique, return Statement(
table=table, sql,
name=name, table=table,
columns=columns, name=name,
) columns=columns,
condition=self._index_condition_sql(condition),
)
def _delete_unique_sql(self, model, name, condition=None): def _delete_unique_sql(self, model, name, condition=None):
if condition: if condition: