summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/postgresql/schema.py')
-rw-r--r--django/db/backends/postgresql/schema.py38
1 files changed, 7 insertions, 31 deletions
diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py
index c0a785c7e1..0c8548a5d6 100644
--- a/django/db/backends/postgresql/schema.py
+++ b/django/db/backends/postgresql/schema.py
@@ -140,13 +140,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
return sequence["name"]
return None
- def _is_changing_type_of_indexed_text_column(self, old_field, old_type, new_type):
- return (old_field.db_index or old_field.unique) and (
- (old_type.startswith("varchar") and not new_type.startswith("varchar"))
- or (old_type.startswith("text") and not new_type.startswith("text"))
- or (old_type.startswith("citext") and not new_type.startswith("citext"))
- )
-
def _alter_column_type_sql(
self, model, old_field, new_field, new_type, old_collation, new_collation
):
@@ -154,7 +147,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# different type.
old_db_params = old_field.db_parameters(connection=self.connection)
old_type = old_db_params["type"]
- if self._is_changing_type_of_indexed_text_column(old_field, old_type, new_type):
+ if (old_field.db_index or old_field.unique) and (
+ (old_type.startswith("varchar") and not new_type.startswith("varchar"))
+ or (old_type.startswith("text") and not new_type.startswith("text"))
+ or (old_type.startswith("citext") and not new_type.startswith("citext"))
+ ):
index_name = self._create_index_name(
model._meta.db_table, [old_field.column], suffix="_like"
)
@@ -258,25 +255,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
model, old_field, new_field, new_type, old_collation, new_collation
)
- def _new_index_should_be_added(self, old_field, new_field):
- return not (old_field.db_index or old_field.unique) and (
- new_field.db_index or new_field.unique
- )
-
- def _deleted_index_should_be_recreated(
- self, old_field, new_field, old_type, new_type
- ):
- if (
- not old_field.unique
- and (
- not new_field.db_index
- or (new_field.unique and not new_field.primary_key)
- )
- ) or (
- self._is_changing_type_of_indexed_text_column(old_field, old_type, new_type)
- ):
- return True
-
def _alter_field(
self,
model,
@@ -299,10 +277,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
strict,
)
# Added an index? Create any PostgreSQL-specific indexes.
- if self._new_index_should_be_added(
- old_field, new_field
- ) or self._deleted_index_should_be_recreated(
- old_field, new_field, old_type, new_type
+ if (not (old_field.db_index or old_field.unique) and new_field.db_index) or (
+ not old_field.unique and new_field.unique
):
like_index_statement = self._create_like_index_sql(model, new_field)
if like_index_statement is not None: