summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorHannes Ljungberg <hannes.ljungberg@gmail.com>2021-10-15 22:17:26 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-10-18 09:37:46 +0200
commitf5802a21c401b92764a9f3e2886144f3c5d77573 (patch)
treec5b8cc3293f2bb64286ae2422836b5c171ec843c /tests
parentfdc1c6435c8fb9e720169ef0aebf87c33f1d86c2 (diff)
[3.2.x] Fixed #33194 -- Fixed migrations when altering a field with functional indexes on SQLite.
This adjusts Expressions.rename_table_references() to only update alias when needed. Regression in 83fcfc9ec8610540948815e127101f1206562ead. Co-authored-by: Simon Charette <charettes@users.noreply.github.com> Backport of 86971c40909430a798e4e55b140004c4b1fb02ff from main.
Diffstat (limited to 'tests')
-rw-r--r--tests/backends/test_ddl_references.py22
-rw-r--r--tests/migrations/test_operations.py19
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/backends/test_ddl_references.py b/tests/backends/test_ddl_references.py
index bd4036ee33..ab3ba6ccd2 100644
--- a/tests/backends/test_ddl_references.py
+++ b/tests/backends/test_ddl_references.py
@@ -5,6 +5,7 @@ from django.db.backends.ddl_references import (
from django.db.models import ExpressionList, F
from django.db.models.functions import Upper
from django.db.models.indexes import IndexExpression
+from django.db.models.sql import Query
from django.test import SimpleTestCase, TransactionTestCase
from .models import Person
@@ -229,6 +230,27 @@ class ExpressionsTests(TransactionTestCase):
str(self.expressions),
)
+ def test_rename_table_references_without_alias(self):
+ compiler = Query(Person, alias_cols=False).get_compiler(connection=connection)
+ table = Person._meta.db_table
+ expressions = Expressions(
+ table=table,
+ expressions=ExpressionList(
+ IndexExpression(Upper('last_name')),
+ IndexExpression(F('first_name')),
+ ).resolve_expression(compiler.query),
+ compiler=compiler,
+ quote_value=self.editor.quote_value,
+ )
+ expressions.rename_table_references(table, 'other')
+ self.assertIs(expressions.references_table(table), False)
+ self.assertIs(expressions.references_table('other'), True)
+ expected_str = '(UPPER(%s)), %s' % (
+ self.editor.quote_name('last_name'),
+ self.editor.quote_name('first_name'),
+ )
+ self.assertEqual(str(expressions), expected_str)
+
def test_rename_column_references(self):
table = Person._meta.db_table
self.expressions.rename_column_references(table, 'first_name', 'other')
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 984aefa23d..9d8e744b5c 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -2010,6 +2010,25 @@ class OperationTests(OperationTestBase):
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'model_name': 'Pony', 'name': index_name})
+ @skipUnlessDBFeature('supports_expression_indexes')
+ def test_alter_field_with_func_index(self):
+ app_label = 'test_alfuncin'
+ index_name = f'{app_label}_pony_idx'
+ table_name = f'{app_label}_pony'
+ project_state = self.set_up_test_model(
+ app_label,
+ indexes=[models.Index(Abs('pink'), name=index_name)],
+ )
+ operation = migrations.AlterField('Pony', 'pink', models.IntegerField(null=True))
+ new_state = project_state.clone()
+ operation.state_forwards(app_label, new_state)
+ with connection.schema_editor() as editor:
+ operation.database_forwards(app_label, editor, project_state, new_state)
+ self.assertIndexNameExists(table_name, index_name)
+ with connection.schema_editor() as editor:
+ operation.database_backwards(app_label, editor, new_state, project_state)
+ self.assertIndexNameExists(table_name, index_name)
+
def test_alter_field_with_index(self):
"""
Test AlterField operation with an index to ensure indexes created via