2015-01-28 20:35:27 +08:00
|
|
|
from django.db import migrations, models
|
2016-01-09 16:12:46 +08:00
|
|
|
from django.db.migrations import operations
|
2013-10-03 00:33:41 +08:00
|
|
|
from django.db.migrations.optimizer import MigrationOptimizer
|
2019-01-22 06:29:46 +08:00
|
|
|
from django.db.migrations.serializer import serializer_factory
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase
|
2013-10-03 00:33:41 +08:00
|
|
|
|
2016-04-28 00:43:56 +08:00
|
|
|
from .models import EmptyManager, UnicodeModel
|
2015-01-08 04:14:25 +08:00
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class OptimizerTests(SimpleTestCase):
|
2013-10-03 00:33:41 +08:00
|
|
|
"""
|
|
|
|
Tests the migration autodetector.
|
|
|
|
"""
|
|
|
|
|
2016-06-05 23:28:15 +08:00
|
|
|
def optimize(self, operations, app_label):
|
2013-10-03 00:33:41 +08:00
|
|
|
"""
|
|
|
|
Handy shortcut for getting results + number of loops
|
|
|
|
"""
|
|
|
|
optimizer = MigrationOptimizer()
|
2016-06-05 23:28:15 +08:00
|
|
|
return optimizer.optimize(operations, app_label), optimizer._iterations
|
2013-10-03 00:33:41 +08:00
|
|
|
|
2019-01-22 06:29:46 +08:00
|
|
|
def serialize(self, value):
|
|
|
|
return serializer_factory(value).serialize()[0]
|
|
|
|
|
2016-06-05 23:28:15 +08:00
|
|
|
def assertOptimizesTo(
|
|
|
|
self, operations, expected, exact=None, less_than=None, app_label=None
|
|
|
|
):
|
2020-04-04 10:57:42 +08:00
|
|
|
result, iterations = self.optimize(operations, app_label or "migrations")
|
2019-01-22 06:29:46 +08:00
|
|
|
result = [self.serialize(f) for f in result]
|
|
|
|
expected = [self.serialize(f) for f in expected]
|
2013-10-03 00:33:41 +08:00
|
|
|
self.assertEqual(expected, result)
|
|
|
|
if exact is not None and iterations != exact:
|
2015-09-12 07:33:12 +08:00
|
|
|
raise self.failureException(
|
|
|
|
"Optimization did not take exactly %s iterations (it took %s)"
|
|
|
|
% (exact, iterations)
|
|
|
|
)
|
2013-10-03 00:33:41 +08:00
|
|
|
if less_than is not None and iterations >= less_than:
|
2015-09-12 07:33:12 +08:00
|
|
|
raise self.failureException(
|
|
|
|
"Optimization did not take less than %s iterations (it took %s)"
|
|
|
|
% (less_than, iterations)
|
|
|
|
)
|
2013-10-03 00:33:41 +08:00
|
|
|
|
2016-06-05 23:28:15 +08:00
|
|
|
def assertDoesNotOptimize(self, operations, **kwargs):
|
|
|
|
self.assertOptimizesTo(operations, operations, **kwargs)
|
2015-06-13 06:21:07 +08:00
|
|
|
|
2020-04-04 10:57:42 +08:00
|
|
|
def test_none_app_label(self):
|
|
|
|
optimizer = MigrationOptimizer()
|
|
|
|
with self.assertRaisesMessage(TypeError, "app_label must be a str"):
|
|
|
|
optimizer.optimize([], None)
|
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
def test_single(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The optimizer does nothing on a single operation,
|
2013-10-03 00:33:41 +08:00
|
|
|
and that it does it in just one pass.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[migrations.DeleteModel("Foo")],
|
|
|
|
[migrations.DeleteModel("Foo")],
|
2013-11-07 12:00:48 +08:00
|
|
|
exact=1,
|
2013-10-03 00:33:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_delete_model(self):
|
|
|
|
"""
|
|
|
|
CreateModel and DeleteModel should collapse into nothing.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_create_rename_model(self):
|
|
|
|
"""
|
|
|
|
CreateModel should absorb RenameModels.
|
|
|
|
"""
|
2015-01-08 04:14:25 +08:00
|
|
|
managers = [("objects", EmptyManager())]
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[("name", models.CharField(max_length=255))],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
migrations.RenameModel("Foo", "Bar"),
|
|
|
|
],
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar",
|
|
|
|
[("name", models.CharField(max_length=255))],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
)
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_rename_model_self(self):
|
|
|
|
"""
|
|
|
|
RenameModels should absorb themselves.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.RenameModel("Foo", "Baa"),
|
|
|
|
migrations.RenameModel("Baa", "Bar"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.RenameModel("Foo", "Bar"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2017-01-11 07:38:48 +08:00
|
|
|
def test_create_alter_model_options(self):
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Foo", fields=[]),
|
|
|
|
migrations.AlterModelOptions(
|
|
|
|
name="Foo", options={"verbose_name_plural": "Foozes"}
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", fields=[], options={"verbose_name_plural": "Foozes"}
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2022-03-11 14:03:51 +08:00
|
|
|
def test_create_alter_model_managers(self):
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Foo", fields=[]),
|
|
|
|
migrations.AlterModelManagers(
|
|
|
|
name="Foo",
|
|
|
|
managers=[
|
|
|
|
("objects", models.Manager()),
|
|
|
|
("things", models.Manager()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
fields=[],
|
|
|
|
managers=[
|
|
|
|
("objects", models.Manager()),
|
|
|
|
("things", models.Manager()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-12-18 05:06:58 +08:00
|
|
|
def test_create_model_and_remove_model_options(self):
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"MyModel",
|
|
|
|
fields=[],
|
|
|
|
options={"verbose_name": "My Model"},
|
|
|
|
),
|
|
|
|
migrations.AlterModelOptions("MyModel", options={}),
|
|
|
|
],
|
|
|
|
[migrations.CreateModel("MyModel", fields=[])],
|
|
|
|
)
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"MyModel",
|
|
|
|
fields=[],
|
|
|
|
options={
|
|
|
|
"verbose_name": "My Model",
|
|
|
|
"verbose_name_plural": "My Model plural",
|
|
|
|
},
|
|
|
|
),
|
|
|
|
migrations.AlterModelOptions(
|
|
|
|
"MyModel",
|
|
|
|
options={"verbose_name": "My Model"},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"MyModel",
|
|
|
|
fields=[],
|
|
|
|
options={"verbose_name": "My Model"},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2015-06-13 06:19:05 +08:00
|
|
|
def _test_create_alter_foo_delete_model(self, alter_foo):
|
2013-10-03 00:33:41 +08:00
|
|
|
"""
|
2015-06-13 06:19:05 +08:00
|
|
|
CreateModel, AlterModelTable, AlterUniqueTogether/AlterIndexTogether/
|
|
|
|
AlterOrderWithRespectTo, and DeleteModel should collapse into nothing.
|
2013-10-03 00:33:41 +08:00
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.AlterModelTable("Foo", "woohoo"),
|
2015-06-13 06:19:05 +08:00
|
|
|
alter_foo,
|
2013-10-03 00:33:41 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
2013-10-16 18:09:33 +08:00
|
|
|
|
2015-06-13 06:19:05 +08:00
|
|
|
def test_create_alter_unique_delete_model(self):
|
|
|
|
self._test_create_alter_foo_delete_model(
|
|
|
|
migrations.AlterUniqueTogether("Foo", [["a", "b"]])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:19:05 +08:00
|
|
|
|
|
|
|
def test_create_alter_index_delete_model(self):
|
|
|
|
self._test_create_alter_foo_delete_model(
|
|
|
|
migrations.AlterIndexTogether("Foo", [["a", "b"]])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:19:05 +08:00
|
|
|
|
|
|
|
def test_create_alter_owrt_delete_model(self):
|
|
|
|
self._test_create_alter_foo_delete_model(
|
|
|
|
migrations.AlterOrderWithRespectTo("Foo", "a")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:19:05 +08:00
|
|
|
|
2015-06-13 06:42:09 +08:00
|
|
|
def _test_alter_alter_model(self, alter_foo, alter_bar):
|
|
|
|
"""
|
|
|
|
Two AlterUniqueTogether/AlterIndexTogether/AlterOrderWithRespectTo
|
|
|
|
should collapse into the second.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
alter_foo,
|
|
|
|
alter_bar,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
alter_bar,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_alter_table_model(self):
|
|
|
|
self._test_alter_alter_model(
|
|
|
|
migrations.AlterModelTable("Foo", "a"),
|
|
|
|
migrations.AlterModelTable("Foo", "b"),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_alter_unique_model(self):
|
|
|
|
self._test_alter_alter_model(
|
|
|
|
migrations.AlterUniqueTogether("Foo", [["a", "b"]]),
|
|
|
|
migrations.AlterUniqueTogether("Foo", [["a", "c"]]),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_alter_index_model(self):
|
|
|
|
self._test_alter_alter_model(
|
|
|
|
migrations.AlterIndexTogether("Foo", [["a", "b"]]),
|
|
|
|
migrations.AlterIndexTogether("Foo", [["a", "c"]]),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_alter_owrt_model(self):
|
|
|
|
self._test_alter_alter_model(
|
|
|
|
migrations.AlterOrderWithRespectTo("Foo", "a"),
|
|
|
|
migrations.AlterOrderWithRespectTo("Foo", "b"),
|
|
|
|
)
|
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def test_optimize_through_create(self):
|
|
|
|
"""
|
|
|
|
We should be able to optimize away create/delete through a create or
|
|
|
|
delete of a different model, but only if the create operation does not
|
|
|
|
mention the model at all.
|
|
|
|
"""
|
|
|
|
# These should work
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
migrations.DeleteModel("Bar"),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
migrations.DeleteModel("Bar"),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
2020-04-04 10:57:42 +08:00
|
|
|
# Operations should be optimized if the FK references a model from the
|
|
|
|
# other app.
|
2016-06-05 23:28:15 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("other", models.ForeignKey("testapp.Foo", models.CASCADE))]
|
|
|
|
),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("other", models.ForeignKey("testapp.Foo", models.CASCADE))]
|
|
|
|
),
|
|
|
|
],
|
|
|
|
app_label="otherapp",
|
|
|
|
)
|
|
|
|
# But it shouldn't work if a FK references a model with the same
|
|
|
|
# app_label.
|
2020-04-04 10:57:42 +08:00
|
|
|
self.assertDoesNotOptimize(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("other", models.ForeignKey("Foo", models.CASCADE))]
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2020-04-04 10:57:42 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
)
|
2016-06-05 23:28:15 +08:00
|
|
|
self.assertDoesNotOptimize(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("other", models.ForeignKey("testapp.Foo", models.CASCADE))]
|
|
|
|
),
|
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
app_label="testapp",
|
|
|
|
)
|
2013-10-16 18:09:33 +08:00
|
|
|
# This should not work - bases should block it
|
2020-04-04 10:57:42 +08:00
|
|
|
self.assertDoesNotOptimize(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("size", models.IntegerField())], bases=("Foo",)
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2020-04-04 10:57:42 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
)
|
2017-01-26 02:52:44 +08:00
|
|
|
self.assertDoesNotOptimize(
|
2013-10-16 18:09:33 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
2017-12-29 04:07:29 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("size", models.IntegerField())], bases=("testapp.Foo",)
|
|
|
|
),
|
2013-10-16 18:09:33 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
2020-04-04 10:57:42 +08:00
|
|
|
app_label="testapp",
|
2013-10-16 18:09:33 +08:00
|
|
|
)
|
2016-06-05 23:28:15 +08:00
|
|
|
# The same operations should be optimized if app_label and none of
|
|
|
|
# bases belong to that app.
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
2017-12-29 04:07:29 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("size", models.IntegerField())], bases=("testapp.Foo",)
|
|
|
|
),
|
2016-06-05 23:28:15 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
[
|
2017-12-29 04:07:29 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("size", models.IntegerField())], bases=("testapp.Foo",)
|
|
|
|
),
|
2016-06-05 23:28:15 +08:00
|
|
|
],
|
|
|
|
app_label="otherapp",
|
|
|
|
)
|
|
|
|
# But it shouldn't work if some of bases belongs to the specified app.
|
|
|
|
self.assertDoesNotOptimize(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
2017-12-29 04:07:29 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar", [("size", models.IntegerField())], bases=("testapp.Foo",)
|
|
|
|
),
|
2016-06-05 23:28:15 +08:00
|
|
|
migrations.DeleteModel("Foo"),
|
|
|
|
],
|
|
|
|
app_label="testapp",
|
|
|
|
)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
2017-02-03 11:09:12 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Book", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Person", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"book",
|
|
|
|
"author",
|
|
|
|
models.ForeignKey("test_app.Person", models.CASCADE),
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Review",
|
|
|
|
[("book", models.ForeignKey("test_app.Book", models.CASCADE))],
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Reviewer", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"review",
|
|
|
|
"reviewer",
|
|
|
|
models.ForeignKey("test_app.Reviewer", models.CASCADE),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-02-03 11:09:12 +08:00
|
|
|
migrations.RemoveField("book", "author"),
|
|
|
|
migrations.DeleteModel("Person"),
|
|
|
|
],
|
|
|
|
[
|
2017-02-18 13:55:13 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Book", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
2017-02-03 11:09:12 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Reviewer", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Review",
|
|
|
|
[
|
|
|
|
("book", models.ForeignKey("test_app.Book", models.CASCADE)),
|
|
|
|
(
|
|
|
|
"reviewer",
|
|
|
|
models.ForeignKey("test_app.Reviewer", models.CASCADE),
|
|
|
|
),
|
|
|
|
],
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-02-03 11:09:12 +08:00
|
|
|
],
|
2020-04-04 10:57:42 +08:00
|
|
|
app_label="test_app",
|
2017-02-03 11:09:12 +08:00
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_create_model_add_field(self):
|
|
|
|
"""
|
|
|
|
AddField should optimize into CreateModel.
|
|
|
|
"""
|
2015-01-08 04:14:25 +08:00
|
|
|
managers = [("objects", EmptyManager())]
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[("name", models.CharField(max_length=255))],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
migrations.AddField("Foo", "age", models.IntegerField()),
|
|
|
|
],
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[
|
|
|
|
("name", models.CharField(max_length=255)),
|
|
|
|
("age", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2017-01-31 02:43:20 +08:00
|
|
|
def test_create_model_reordering(self):
|
2014-06-23 02:23:45 +08:00
|
|
|
"""
|
2017-01-31 02:43:20 +08:00
|
|
|
AddField optimizes into CreateModel if it's a FK to a model that's
|
|
|
|
between them (and there's no FK in the other direction), by changing
|
|
|
|
the order of the CreateModel operations.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Link", [("url", models.TextField())]),
|
|
|
|
migrations.AddField(
|
|
|
|
"Foo", "link", models.ForeignKey("migrations.Link", models.CASCADE)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Link", [("url", models.TextField())]),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("name", models.CharField(max_length=255)),
|
|
|
|
("link", models.ForeignKey("migrations.Link", models.CASCADE)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_reordering_circular_fk(self):
|
|
|
|
"""
|
|
|
|
CreateModel reordering behavior doesn't result in an infinite loop if
|
|
|
|
there are FKs in both directions.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Bar", [("url", models.TextField())]),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Bar", "foo_fk", models.ForeignKey("migrations.Foo", models.CASCADE)
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Foo", "bar_fk", models.ForeignKey("migrations.Bar", models.CASCADE)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Bar",
|
|
|
|
[
|
|
|
|
("url", models.TextField()),
|
|
|
|
("foo_fk", models.ForeignKey("migrations.Foo", models.CASCADE)),
|
|
|
|
],
|
2019-01-22 06:29:46 +08:00
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Foo", "bar_fk", models.ForeignKey("migrations.Bar", models.CASCADE)
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-01-31 02:43:20 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_no_reordering_for_unrelated_fk(self):
|
|
|
|
"""
|
|
|
|
CreateModel order remains unchanged if the later AddField operation
|
|
|
|
isn't a FK between them.
|
2014-06-23 02:23:45 +08:00
|
|
|
"""
|
2017-01-26 02:52:44 +08:00
|
|
|
self.assertDoesNotOptimize(
|
2014-06-23 02:23:45 +08:00
|
|
|
[
|
2017-01-31 02:43:20 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Link", [("url", models.TextField())]),
|
|
|
|
migrations.AddField(
|
|
|
|
"Other",
|
|
|
|
"link",
|
|
|
|
models.ForeignKey("migrations.Link", models.CASCADE),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-01-31 02:43:20 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_no_reordering_of_inherited_model(self):
|
|
|
|
"""
|
|
|
|
A CreateModel that inherits from another isn't reordered to avoid
|
|
|
|
moving it earlier than its parent CreateModel operation.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
2013-11-06 21:47:58 +08:00
|
|
|
"Other", [("foo", models.CharField(max_length=255))]
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
2017-01-31 02:43:20 +08:00
|
|
|
"ParentModel", [("bar", models.CharField(max_length=255))]
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2020-12-14 05:41:20 +08:00
|
|
|
migrations.CreateModel(
|
2017-01-31 02:43:20 +08:00
|
|
|
"ChildModel",
|
|
|
|
[("baz", models.CharField(max_length=255))],
|
|
|
|
bases=("migrations.parentmodel",),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-09-12 07:33:12 +08:00
|
|
|
migrations.AddField(
|
2022-02-04 03:24:19 +08:00
|
|
|
"Other",
|
|
|
|
"fk",
|
2017-01-31 02:43:20 +08:00
|
|
|
models.ForeignKey("migrations.ChildModel", models.CASCADE),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ParentModel", [("bar", models.CharField(max_length=255))]
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2020-12-14 05:41:20 +08:00
|
|
|
migrations.CreateModel(
|
2017-01-31 02:43:20 +08:00
|
|
|
"ChildModel",
|
|
|
|
[("baz", models.CharField(max_length=255))],
|
|
|
|
bases=("migrations.parentmodel",),
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Other",
|
|
|
|
[
|
|
|
|
("foo", models.CharField(max_length=255)),
|
|
|
|
(
|
|
|
|
"fk",
|
|
|
|
models.ForeignKey("migrations.ChildModel", models.CASCADE),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-01-31 02:43:20 +08:00
|
|
|
],
|
|
|
|
),
|
2014-06-23 02:23:45 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_add_field_not_through_m2m_through(self):
|
|
|
|
"""
|
|
|
|
AddField should NOT optimize into CreateModel if it's an M2M using a
|
|
|
|
through that's created between them.
|
|
|
|
"""
|
2017-01-26 02:52:44 +08:00
|
|
|
self.assertDoesNotOptimize(
|
2014-06-23 02:23:45 +08:00
|
|
|
[
|
2017-01-31 03:14:38 +08:00
|
|
|
migrations.CreateModel("Employee", []),
|
|
|
|
migrations.CreateModel("Employer", []),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Employment",
|
|
|
|
[
|
|
|
|
(
|
|
|
|
"employee",
|
|
|
|
models.ForeignKey("migrations.Employee", models.CASCADE),
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2017-01-31 03:14:38 +08:00
|
|
|
(
|
|
|
|
"employment",
|
|
|
|
models.ForeignKey("migrations.Employer", models.CASCADE),
|
|
|
|
),
|
|
|
|
],
|
2022-02-04 03:24:19 +08:00
|
|
|
),
|
2015-09-12 07:33:12 +08:00
|
|
|
migrations.AddField(
|
2017-01-31 03:14:38 +08:00
|
|
|
"Employer",
|
|
|
|
"employees",
|
|
|
|
models.ManyToManyField(
|
|
|
|
"migrations.Employee",
|
|
|
|
through="migrations.Employment",
|
|
|
|
),
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2014-06-23 02:23:45 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_create_model_alter_field(self):
|
|
|
|
"""
|
|
|
|
AlterField should optimize into CreateModel.
|
|
|
|
"""
|
2015-01-08 04:14:25 +08:00
|
|
|
managers = [("objects", EmptyManager())]
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[("name", models.CharField(max_length=255))],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
migrations.AlterField("Foo", "name", models.IntegerField()),
|
|
|
|
],
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[
|
|
|
|
("name", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_rename_field(self):
|
|
|
|
"""
|
|
|
|
RenameField should optimize into CreateModel.
|
|
|
|
"""
|
2015-01-08 04:14:25 +08:00
|
|
|
managers = [("objects", EmptyManager())]
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[("name", models.CharField(max_length=255))],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
migrations.RenameField("Foo", "name", "title"),
|
|
|
|
],
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[
|
|
|
|
("title", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_add_field_rename_field(self):
|
|
|
|
"""
|
|
|
|
RenameField should optimize into AddField
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.AddField("Foo", "name", models.CharField(max_length=255)),
|
|
|
|
migrations.RenameField("Foo", "name", "title"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.AddField("Foo", "title", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_field_rename_field(self):
|
|
|
|
"""
|
|
|
|
RenameField should optimize to the other side of AlterField,
|
|
|
|
and into itself.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.AlterField("Foo", "name", models.CharField(max_length=255)),
|
|
|
|
migrations.RenameField("Foo", "name", "title"),
|
|
|
|
migrations.RenameField("Foo", "title", "nom"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.RenameField("Foo", "name", "nom"),
|
|
|
|
migrations.AlterField("Foo", "nom", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2020-12-14 05:41:20 +08:00
|
|
|
def test_swapping_fields_names(self):
|
|
|
|
self.assertDoesNotOptimize(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"MyModel",
|
|
|
|
[
|
|
|
|
("field_a", models.IntegerField()),
|
|
|
|
("field_b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
migrations.RunPython(migrations.RunPython.noop),
|
|
|
|
migrations.RenameField("MyModel", "field_a", "field_c"),
|
|
|
|
migrations.RenameField("MyModel", "field_b", "field_a"),
|
|
|
|
migrations.RenameField("MyModel", "field_c", "field_b"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_create_model_remove_field(self):
|
|
|
|
"""
|
|
|
|
RemoveField should optimize into CreateModel.
|
|
|
|
"""
|
2015-01-08 04:14:25 +08:00
|
|
|
managers = [("objects", EmptyManager())]
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[
|
|
|
|
("name", models.CharField(max_length=255)),
|
|
|
|
("age", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
migrations.RemoveField("Foo", "age"),
|
|
|
|
],
|
|
|
|
[
|
2015-01-08 04:14:25 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
name="Foo",
|
|
|
|
fields=[
|
|
|
|
("name", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
options={"verbose_name": "Foo"},
|
2016-04-28 00:43:56 +08:00
|
|
|
bases=(UnicodeModel,),
|
2015-01-08 04:14:25 +08:00
|
|
|
managers=managers,
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_add_field_alter_field(self):
|
|
|
|
"""
|
|
|
|
AlterField should optimize into AddField.
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.AddField("Foo", "age", models.IntegerField()),
|
|
|
|
migrations.AlterField("Foo", "age", models.FloatField(default=2.4)),
|
|
|
|
],
|
|
|
|
[
|
2014-11-16 03:25:43 +08:00
|
|
|
migrations.AddField(
|
|
|
|
"Foo", name="age", field=models.FloatField(default=2.4)
|
|
|
|
),
|
2013-11-06 21:47:58 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_add_field_delete_field(self):
|
|
|
|
"""
|
|
|
|
RemoveField should cancel AddField
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.AddField("Foo", "age", models.IntegerField()),
|
|
|
|
migrations.RemoveField("Foo", "age"),
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_alter_field_delete_field(self):
|
|
|
|
"""
|
|
|
|
RemoveField should absorb AlterField
|
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.AlterField("Foo", "age", models.IntegerField()),
|
|
|
|
migrations.RemoveField("Foo", "age"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.RemoveField("Foo", "age"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2015-06-13 06:21:07 +08:00
|
|
|
def _test_create_alter_foo_field(self, alter):
|
|
|
|
"""
|
|
|
|
CreateModel, AlterFooTogether/AlterOrderWithRespectTo followed by an
|
2018-07-14 12:32:09 +08:00
|
|
|
add/alter/rename field should optimize to CreateModel with options.
|
2015-06-13 06:21:07 +08:00
|
|
|
"""
|
2018-07-14 12:32:09 +08:00
|
|
|
option_value = getattr(alter, alter.option_name)
|
|
|
|
options = {alter.option_name: option_value}
|
|
|
|
|
2015-06-13 06:21:07 +08:00
|
|
|
# AddField
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.AddField("Foo", "c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("c", models.IntegerField()),
|
2018-07-14 12:32:09 +08:00
|
|
|
],
|
|
|
|
options=options,
|
|
|
|
),
|
2015-06-13 06:21:07 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# AlterField
|
2018-07-14 12:32:09 +08:00
|
|
|
self.assertOptimizesTo(
|
2015-06-13 06:21:07 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.AlterField("Foo", "b", models.CharField(max_length=255)),
|
|
|
|
],
|
2018-07-14 12:32:09 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
options=options,
|
|
|
|
),
|
|
|
|
],
|
2015-06-13 06:21:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.AlterField("Foo", "c", models.CharField(max_length=255)),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("c", models.CharField(max_length=255)),
|
2018-07-14 12:32:09 +08:00
|
|
|
],
|
|
|
|
options=options,
|
|
|
|
),
|
2015-06-13 06:21:07 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# RenameField
|
2018-07-14 12:32:09 +08:00
|
|
|
if isinstance(option_value, str):
|
|
|
|
renamed_options = {alter.option_name: "c"}
|
|
|
|
else:
|
|
|
|
renamed_options = {
|
|
|
|
alter.option_name: {
|
|
|
|
tuple("c" if value == "b" else value for value in item)
|
|
|
|
for item in option_value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertOptimizesTo(
|
2015-06-13 06:21:07 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.RenameField("Foo", "b", "c"),
|
|
|
|
],
|
2018-07-14 12:32:09 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options=renamed_options,
|
|
|
|
),
|
|
|
|
],
|
2015-06-13 06:21:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.RenameField("Foo", "b", "x"),
|
|
|
|
migrations.RenameField("Foo", "x", "c"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
2018-07-14 12:32:09 +08:00
|
|
|
("c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options=renamed_options,
|
|
|
|
),
|
2015-06-13 06:21:07 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.RenameField("Foo", "c", "d"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("d", models.IntegerField()),
|
2018-07-14 12:32:09 +08:00
|
|
|
],
|
|
|
|
options=options,
|
|
|
|
),
|
2015-06-13 06:21:07 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# RemoveField
|
2018-07-14 12:32:09 +08:00
|
|
|
if isinstance(option_value, str):
|
|
|
|
removed_options = None
|
|
|
|
else:
|
|
|
|
removed_options = {
|
|
|
|
alter.option_name: {
|
|
|
|
tuple(value for value in item if value != "b")
|
|
|
|
for item in option_value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.assertOptimizesTo(
|
2015-06-13 06:21:07 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.RemoveField("Foo", "b"),
|
|
|
|
],
|
2018-07-14 12:32:09 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options=removed_options,
|
|
|
|
),
|
|
|
|
],
|
2015-06-13 06:21:07 +08:00
|
|
|
)
|
|
|
|
|
2018-07-14 12:32:09 +08:00
|
|
|
self.assertOptimizesTo(
|
2015-06-13 06:21:07 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
("c", models.IntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
alter,
|
|
|
|
migrations.RemoveField("Foo", "c"),
|
|
|
|
],
|
2018-07-14 12:32:09 +08:00
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo",
|
|
|
|
[
|
|
|
|
("a", models.IntegerField()),
|
|
|
|
("b", models.IntegerField()),
|
|
|
|
],
|
|
|
|
options=options,
|
|
|
|
),
|
|
|
|
],
|
2015-06-13 06:21:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_alter_unique_field(self):
|
|
|
|
self._test_create_alter_foo_field(
|
|
|
|
migrations.AlterUniqueTogether("Foo", [["a", "b"]])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:21:07 +08:00
|
|
|
|
|
|
|
def test_create_alter_index_field(self):
|
|
|
|
self._test_create_alter_foo_field(
|
|
|
|
migrations.AlterIndexTogether("Foo", [["a", "b"]])
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:21:07 +08:00
|
|
|
|
|
|
|
def test_create_alter_owrt_field(self):
|
|
|
|
self._test_create_alter_foo_field(
|
|
|
|
migrations.AlterOrderWithRespectTo("Foo", "b")
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2015-06-13 06:21:07 +08:00
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_optimize_through_fields(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
field-level through checking is working. This should manage to collapse
|
|
|
|
model Foo to nonexistence, and model Bar to a single IntegerField
|
|
|
|
called "width".
|
2013-11-06 21:47:58 +08:00
|
|
|
"""
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
migrations.AddField("Foo", "age", models.IntegerField()),
|
|
|
|
migrations.AddField("Bar", "width", models.IntegerField()),
|
|
|
|
migrations.AlterField("Foo", "age", models.IntegerField()),
|
|
|
|
migrations.RenameField("Bar", "size", "dimensions"),
|
|
|
|
migrations.RemoveField("Foo", "age"),
|
|
|
|
migrations.RenameModel("Foo", "Phou"),
|
|
|
|
migrations.RemoveField("Bar", "dimensions"),
|
|
|
|
migrations.RenameModel("Phou", "Fou"),
|
|
|
|
migrations.DeleteModel("Fou"),
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel("Bar", [("width", models.IntegerField())]),
|
|
|
|
],
|
|
|
|
)
|
2016-01-09 16:12:46 +08:00
|
|
|
|
|
|
|
def test_optimize_elidable_operation(self):
|
|
|
|
elidable_operation = operations.base.Operation()
|
|
|
|
elidable_operation.elidable = True
|
|
|
|
self.assertOptimizesTo(
|
|
|
|
[
|
|
|
|
elidable_operation,
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Foo", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
elidable_operation,
|
|
|
|
migrations.CreateModel("Bar", [("size", models.IntegerField())]),
|
|
|
|
elidable_operation,
|
|
|
|
migrations.RenameModel("Foo", "Phou"),
|
|
|
|
migrations.DeleteModel("Bar"),
|
|
|
|
elidable_operation,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Phou", [("name", models.CharField(max_length=255))]
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|