Refs #27860 -- Simplified deleting indexes on PostgreSQL using "IF EXISTS".

This commit is contained in:
Mariusz Felisiak 2017-03-03 16:50:34 +01:00 committed by Tim Graham
parent d82ee32aac
commit 6b47431aaf
1 changed files with 2 additions and 4 deletions

View File

@ -14,6 +14,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_create_index = "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s" sql_create_index = "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s"
sql_create_varchar_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s varchar_pattern_ops)%(extra)s" sql_create_varchar_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s varchar_pattern_ops)%(extra)s"
sql_create_text_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s text_pattern_ops)%(extra)s" sql_create_text_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s text_pattern_ops)%(extra)s"
sql_delete_index = "DROP INDEX IF EXISTS %(name)s"
# Setting the constraint to IMMEDIATE runs any deferred checks to allow # Setting the constraint to IMMEDIATE runs any deferred checks to allow
# dropping it in the same transaction. # dropping it in the same transaction.
@ -117,7 +118,4 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# Removed an index? Drop any PostgreSQL-specific indexes. # Removed an index? Drop any PostgreSQL-specific indexes.
if old_field.unique and not (new_field.db_index or new_field.unique): if old_field.unique and not (new_field.db_index or new_field.unique):
index_to_remove = self._create_index_name(model, [old_field.column], suffix='_like') index_to_remove = self._create_index_name(model, [old_field.column], suffix='_like')
index_names = self._constraint_names(model, [old_field.column], index=True) self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_to_remove))
for index_name in index_names:
if index_name == index_to_remove:
self.execute(self._delete_constraint_sql(self.sql_delete_index, model, index_name))