diff --git a/django/db/migrations/optimizer.py b/django/db/migrations/optimizer.py index 1cade67457..c39bf15453 100644 --- a/django/db/migrations/optimizer.py +++ b/django/db/migrations/optimizer.py @@ -211,6 +211,7 @@ class MigrationOptimizer(object): fields=operation.fields, options=operation.options, bases=operation.bases, + managers=operation.managers, ) ] @@ -247,6 +248,7 @@ class MigrationOptimizer(object): fields=operation.fields + [(other.name, other.field)], options=operation.options, bases=operation.bases, + managers=operation.managers, ) ] @@ -261,6 +263,7 @@ class MigrationOptimizer(object): ], options=operation.options, bases=operation.bases, + managers=operation.managers, ) ] @@ -275,6 +278,7 @@ class MigrationOptimizer(object): ], options=operation.options, bases=operation.bases, + managers=operation.managers, ) ] @@ -290,6 +294,7 @@ class MigrationOptimizer(object): ], options=operation.options, bases=operation.bases, + managers=operation.managers, ) ] diff --git a/tests/migrations/models.py b/tests/migrations/models.py index 1e6ab07f75..6f02e8088e 100644 --- a/tests/migrations/models.py +++ b/tests/migrations/models.py @@ -52,6 +52,10 @@ class UnmigratedModel(models.Model): pass +class EmptyManager(models.Manager): + use_in_migrations = True + + class FoodQuerySet(models.query.QuerySet): pass diff --git a/tests/migrations/test_optimizer.py b/tests/migrations/test_optimizer.py index dba07c2878..cb89a85c2d 100644 --- a/tests/migrations/test_optimizer.py +++ b/tests/migrations/test_optimizer.py @@ -5,6 +5,8 @@ from django.db.migrations.optimizer import MigrationOptimizer from django.db import migrations from django.db import models +from .models import CustomModelBase, EmptyManager + class OptimizerTests(TestCase): """ @@ -55,13 +57,26 @@ class OptimizerTests(TestCase): """ CreateModel should absorb RenameModels. """ + managers = [('objects', EmptyManager())] self.assertOptimizesTo( [ - migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]), + migrations.CreateModel( + name="Foo", + fields=[("name", models.CharField(max_length=255))], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), migrations.RenameModel("Foo", "Bar"), ], [ - migrations.CreateModel("Bar", [("name", models.CharField(max_length=255))]), + migrations.CreateModel( + "Bar", + [("name", models.CharField(max_length=255))], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ) ], ) @@ -159,16 +174,29 @@ class OptimizerTests(TestCase): """ AddField should optimize into CreateModel. """ + managers = [('objects', EmptyManager())] self.assertOptimizesTo( [ - migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]), + migrations.CreateModel( + name="Foo", + fields=[("name", models.CharField(max_length=255))], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), migrations.AddField("Foo", "age", models.IntegerField()), ], [ - migrations.CreateModel("Foo", [ - ("name", models.CharField(max_length=255)), - ("age", models.IntegerField()), - ]), + migrations.CreateModel( + name="Foo", + fields=[ + ("name", models.CharField(max_length=255)), + ("age", models.IntegerField()), + ], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), ], ) @@ -214,15 +242,28 @@ class OptimizerTests(TestCase): """ AlterField should optimize into CreateModel. """ + managers = [('objects', EmptyManager())] self.assertOptimizesTo( [ - migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]), + migrations.CreateModel( + name="Foo", + fields=[("name", models.CharField(max_length=255))], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), migrations.AlterField("Foo", "name", models.IntegerField()), ], [ - migrations.CreateModel("Foo", [ - ("name", models.IntegerField()), - ]), + migrations.CreateModel( + name="Foo", + fields=[ + ("name", models.IntegerField()), + ], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), ], ) @@ -230,15 +271,28 @@ class OptimizerTests(TestCase): """ RenameField should optimize into CreateModel. """ + managers = [('objects', EmptyManager())] self.assertOptimizesTo( [ - migrations.CreateModel("Foo", [("name", models.CharField(max_length=255))]), + migrations.CreateModel( + name="Foo", + fields=[("name", models.CharField(max_length=255))], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), migrations.RenameField("Foo", "name", "title"), ], [ - migrations.CreateModel("Foo", [ - ("title", models.CharField(max_length=255)), - ]), + migrations.CreateModel( + name="Foo", + fields=[ + ("title", models.CharField(max_length=255)), + ], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), ], ) @@ -277,18 +331,31 @@ class OptimizerTests(TestCase): """ RemoveField should optimize into CreateModel. """ + managers = [('objects', EmptyManager())] self.assertOptimizesTo( [ - migrations.CreateModel("Foo", [ - ("name", models.CharField(max_length=255)), - ("age", models.IntegerField()), - ]), + migrations.CreateModel( + name="Foo", + fields=[ + ("name", models.CharField(max_length=255)), + ("age", models.IntegerField()), + ], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), migrations.RemoveField("Foo", "age"), ], [ - migrations.CreateModel("Foo", [ - ("name", models.CharField(max_length=255)), - ]), + migrations.CreateModel( + name="Foo", + fields=[ + ("name", models.CharField(max_length=255)), + ], + options={'verbose_name': 'Foo'}, + bases=(CustomModelBase), + managers=managers, + ), ], )