summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-05-03 13:06:19 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-05-03 13:09:49 +0200
commit290fd5ecece400490ad6bb557720d3b76f647eaf (patch)
treeaa48ff5a54e5b3ae3ae77e885dc1f328bc81d71c /tests
parent4c68482c3ebe07c598edc4fa4e1796c7c773d768 (diff)
[4.2.x] Fixed #34529, Refs #34525 -- Reduced index operations with Meta.indexes/index_together when optimizing migrations.
This makes squashing migrations an available path for changing Meta.index_together, which is deprecated, to Meta.indexes. Follow up to f81032572107846922745b68d5b7191058fdd5f5. Backport of 8e2460d599aec95f8cfe514d3cc8acdd4ca4b1fb from main.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_autodetector.py143
-rw-r--r--tests/migrations/test_optimizer.py178
2 files changed, 254 insertions, 67 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index 82ddb17543..472403cb68 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -2266,10 +2266,9 @@ class AutodetectorTests(BaseAutodetectorTests):
changes,
"eggs",
0,
- ["CreateModel", "CreateModel", "AddIndex", "AlterUniqueTogether"],
+ ["CreateModel", "CreateModel"],
)
self.assertNotIn("unique_together", changes["eggs"][0].operations[0].options)
- self.assertNotIn("unique_together", changes["eggs"][0].operations[1].options)
self.assertMigrationDependencies(changes, "eggs", 0, [])
def test_alter_db_table_add(self):
@@ -2565,6 +2564,9 @@ class AutodetectorTests(BaseAutodetectorTests):
def test_create_model_with_indexes(self):
"""Test creation of new model with indexes already defined."""
+ added_index = models.Index(
+ fields=["name"], name="create_model_with_indexes_idx"
+ )
author = ModelState(
"otherapp",
"Author",
@@ -2573,25 +2575,25 @@ class AutodetectorTests(BaseAutodetectorTests):
("name", models.CharField(max_length=200)),
],
{
- "indexes": [
- models.Index(fields=["name"], name="create_model_with_indexes_idx")
- ]
+ "indexes": [added_index],
},
)
changes = self.get_changes([], [author])
- added_index = models.Index(
- fields=["name"], name="create_model_with_indexes_idx"
- )
# Right number of migrations?
self.assertEqual(len(changes["otherapp"]), 1)
# Right number of actions?
migration = changes["otherapp"][0]
- self.assertEqual(len(migration.operations), 2)
+ self.assertEqual(len(migration.operations), 1)
# Right actions order?
- self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel", "AddIndex"])
+ self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Author")
self.assertOperationAttributes(
- changes, "otherapp", 0, 1, model_name="author", index=added_index
+ changes,
+ "otherapp",
+ 0,
+ 0,
+ name="Author",
+ options={"indexes": [added_index]},
)
def test_add_indexes(self):
@@ -3984,62 +3986,69 @@ class AutodetectorTests(BaseAutodetectorTests):
},
)
- def test_add_model_order_with_respect_to_index_constraint(self):
- tests = [
- (
- "AddIndex",
- {
- "indexes": [
- models.Index(fields=["_order"], name="book_order_idx"),
- ]
- },
- ),
- (
- "AddConstraint",
- {
- "constraints": [
- models.CheckConstraint(
- check=models.Q(_order__gt=1),
- name="book_order_gt_1",
- ),
- ]
- },
- ),
- ]
- for operation, extra_option in tests:
- with self.subTest(operation=operation):
- after = ModelState(
- "testapp",
- "Author",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ],
- options={
- "order_with_respect_to": "book",
- **extra_option,
- },
- )
- changes = self.get_changes([], [self.book, after])
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(
- changes,
- "testapp",
- 0,
- [
- "CreateModel",
- operation,
- ],
- )
- self.assertOperationAttributes(
- changes,
- "testapp",
- 0,
- 0,
- name="Author",
- options={"order_with_respect_to": "book"},
- )
+ def test_add_model_order_with_respect_to_constraint(self):
+ after = ModelState(
+ "testapp",
+ "Author",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("name", models.CharField(max_length=200)),
+ ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
+ ],
+ options={
+ "order_with_respect_to": "book",
+ "constraints": [
+ models.CheckConstraint(
+ check=models.Q(_order__gt=1), name="book_order_gt_1"
+ ),
+ ],
+ },
+ )
+ changes = self.get_changes([], [self.book, after])
+ self.assertNumberMigrations(changes, "testapp", 1)
+ self.assertOperationTypes(
+ changes,
+ "testapp",
+ 0,
+ ["CreateModel", "AddConstraint"],
+ )
+ self.assertOperationAttributes(
+ changes,
+ "testapp",
+ 0,
+ 0,
+ name="Author",
+ options={"order_with_respect_to": "book"},
+ )
+
+ def test_add_model_order_with_respect_to_index(self):
+ after = ModelState(
+ "testapp",
+ "Author",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("name", models.CharField(max_length=200)),
+ ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
+ ],
+ options={
+ "order_with_respect_to": "book",
+ "indexes": [models.Index(fields=["_order"], name="book_order_idx")],
+ },
+ )
+ changes = self.get_changes([], [self.book, after])
+ self.assertNumberMigrations(changes, "testapp", 1)
+ self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
+ self.assertOperationAttributes(
+ changes,
+ "testapp",
+ 0,
+ 0,
+ name="Author",
+ options={
+ "order_with_respect_to": "book",
+ "indexes": [models.Index(fields=["_order"], name="book_order_idx")],
+ },
+ )
def test_set_alter_order_with_respect_to_index_constraint_unique_together(self):
tests = [
diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py
index 6485009eb4..2992f9cad7 100644
--- a/tests/migrations/test_optimizer.py
+++ b/tests/migrations/test_optimizer.py
@@ -1152,3 +1152,181 @@ class OptimizerTests(SimpleTestCase):
),
]
)
+
+ def test_create_model_add_index(self):
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [models.Index(fields=["age"], name="idx_pony_age")],
+ },
+ ),
+ migrations.AddIndex(
+ "Pony",
+ models.Index(fields=["weight"], name="idx_pony_weight"),
+ ),
+ ],
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [
+ models.Index(fields=["age"], name="idx_pony_age"),
+ models.Index(fields=["weight"], name="idx_pony_weight"),
+ ],
+ },
+ ),
+ ],
+ )
+
+ def test_create_model_remove_index(self):
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [
+ models.Index(fields=["age"], name="idx_pony_age"),
+ models.Index(fields=["weight"], name="idx_pony_weight"),
+ ],
+ },
+ ),
+ migrations.RemoveIndex("Pony", "idx_pony_age"),
+ ],
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [
+ models.Index(fields=["weight"], name="idx_pony_weight"),
+ ],
+ },
+ ),
+ ],
+ )
+
+ def test_create_model_remove_index_together_rename_index(self):
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "index_together": [("age", "weight")],
+ },
+ ),
+ migrations.RenameIndex(
+ "Pony", new_name="idx_pony_age_weight", old_fields=("age", "weight")
+ ),
+ ],
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [
+ models.Index(
+ fields=["age", "weight"], name="idx_pony_age_weight"
+ ),
+ ],
+ },
+ ),
+ ],
+ )
+
+ def test_create_model_index_together_rename_index(self):
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ("height", models.IntegerField()),
+ ("rank", models.IntegerField()),
+ ],
+ options={
+ "index_together": [("age", "weight"), ("height", "rank")],
+ },
+ ),
+ migrations.RenameIndex(
+ "Pony", new_name="idx_pony_age_weight", old_fields=("age", "weight")
+ ),
+ ],
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ("height", models.IntegerField()),
+ ("rank", models.IntegerField()),
+ ],
+ options={
+ "index_together": {("height", "rank")},
+ "indexes": [
+ models.Index(
+ fields=["age", "weight"], name="idx_pony_age_weight"
+ ),
+ ],
+ },
+ ),
+ ],
+ )
+
+ def test_create_model_rename_index_no_old_fields(self):
+ self.assertOptimizesTo(
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [models.Index(fields=["age"], name="idx_pony_age")],
+ },
+ ),
+ migrations.RenameIndex(
+ "Pony", new_name="idx_pony_age_new", old_name="idx_pony_age"
+ ),
+ ],
+ [
+ migrations.CreateModel(
+ name="Pony",
+ fields=[
+ ("weight", models.IntegerField()),
+ ("age", models.IntegerField()),
+ ],
+ options={
+ "indexes": [models.Index(fields=["age"], name="idx_pony_age")],
+ },
+ ),
+ migrations.RenameIndex(
+ "Pony", new_name="idx_pony_age_new", old_name="idx_pony_age"
+ ),
+ ],
+ )