diff --git a/django/db/migrations/operations/base.py b/django/db/migrations/operations/base.py index 3fb1002c445..a29e5230c7c 100644 --- a/django/db/migrations/operations/base.py +++ b/django/db/migrations/operations/base.py @@ -113,7 +113,7 @@ class Operation: return router.allow_migrate_model(connection_alias, model) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): """ Return either a list of operations the actual operation should be replaced with or a boolean that indicates whether or not the specified diff --git a/django/db/migrations/operations/fields.py b/django/db/migrations/operations/fields.py index 601f9ac43a1..402dae2829b 100644 --- a/django/db/migrations/operations/fields.py +++ b/django/db/migrations/operations/fields.py @@ -60,9 +60,9 @@ class FieldOperation(Operation): return True return False - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): return ( - super().reduce(operation, in_between, app_label=app_label) or + super().reduce(operation, app_label=app_label) or not operation.references_field(self.model_name, self.name, app_label) ) @@ -122,7 +122,7 @@ class AddField(FieldOperation): def describe(self): return "Add field %s to %s" % (self.name, self.model_name) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if isinstance(operation, FieldOperation) and self.is_same_field_operation(operation): if isinstance(operation, AlterField): return [ @@ -142,7 +142,7 @@ class AddField(FieldOperation): field=self.field, ), ] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class RemoveField(FieldOperation): @@ -186,11 +186,11 @@ class RemoveField(FieldOperation): def describe(self): return "Remove field %s from %s" % (self.name, self.model_name) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): from .models import DeleteModel if isinstance(operation, DeleteModel) and operation.name_lower == self.model_name_lower: return [operation] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class AlterField(FieldOperation): @@ -256,7 +256,7 @@ class AlterField(FieldOperation): def describe(self): return "Alter field %s on %s" % (self.name, self.model_name) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if isinstance(operation, RemoveField) and self.is_same_field_operation(operation): return [operation] elif isinstance(operation, RenameField) and self.is_same_field_operation(operation): @@ -268,7 +268,7 @@ class AlterField(FieldOperation): field=self.field, ), ] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class RenameField(FieldOperation): @@ -383,7 +383,7 @@ class RenameField(FieldOperation): name.lower() == self.new_name_lower ) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if (isinstance(operation, RenameField) and self.is_same_model_operation(operation) and self.new_name_lower == operation.old_name_lower): @@ -397,6 +397,6 @@ class RenameField(FieldOperation): # Skip `FieldOperation.reduce` as we want to run `references_field` # against self.new_name. return ( - super(FieldOperation, self).reduce(operation, in_between, app_label=app_label) or + super(FieldOperation, self).reduce(operation, app_label=app_label) or not operation.references_field(self.model_name, self.new_name, app_label) ) diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index b2d3f70feab..ad436cf611a 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -31,9 +31,9 @@ class ModelOperation(Operation): def references_model(self, name, app_label=None): return name.lower() == self.name_lower - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): return ( - super().reduce(operation, in_between, app_label=app_label) or + super().reduce(operation, app_label=app_label) or not operation.references_model(self.name, app_label) ) @@ -117,7 +117,7 @@ class CreateModel(ModelOperation): return True return False - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if (isinstance(operation, DeleteModel) and self.name_lower == operation.name_lower and not self.options.get("proxy", False)): @@ -193,7 +193,7 @@ class CreateModel(ModelOperation): managers=self.managers, ), ] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class DeleteModel(ModelOperation): @@ -368,7 +368,7 @@ class RenameModel(ModelOperation): def describe(self): return "Rename model %s to %s" % (self.old_name, self.new_name) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if (isinstance(operation, RenameModel) and self.new_name_lower == operation.old_name_lower): return [ @@ -380,7 +380,7 @@ class RenameModel(ModelOperation): # Skip `ModelOperation.reduce` as we want to run `references_model` # against self.new_name. return ( - super(ModelOperation, self).reduce(operation, in_between, app_label=app_label) or + super(ModelOperation, self).reduce(operation, app_label=app_label) or not operation.references_model(self.new_name, app_label) ) @@ -434,26 +434,26 @@ class AlterModelTable(ModelOperation): self.table if self.table is not None else "(default)" ) - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if isinstance(operation, (AlterModelTable, DeleteModel)) and self.name_lower == operation.name_lower: return [operation] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class ModelOptionOperation(ModelOperation): - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if isinstance(operation, (self.__class__, DeleteModel)) and self.name_lower == operation.name_lower: return [operation] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class FieldRelatedOptionOperation(ModelOptionOperation): - def reduce(self, operation, in_between, app_label=None): + def reduce(self, operation, app_label=None): if (isinstance(operation, FieldOperation) and self.name_lower == operation.model_name_lower and not self.references_field(operation.model_name, operation.name)): return [operation, self] - return super().reduce(operation, in_between, app_label=app_label) + return super().reduce(operation, app_label=app_label) class AlterUniqueTogether(FieldRelatedOptionOperation): diff --git a/django/db/migrations/optimizer.py b/django/db/migrations/optimizer.py index 7974dba66b0..d1b9d8a0314 100644 --- a/django/db/migrations/optimizer.py +++ b/django/db/migrations/optimizer.py @@ -46,12 +46,12 @@ class MigrationOptimizer: # Compare it to each operation after it for j, other in enumerate(operations[i + 1:]): in_between = operations[i + 1:i + j + 1] - result = operation.reduce(other, in_between, app_label) + result = operation.reduce(other, app_label) if isinstance(result, list): if right: new_operations.extend(in_between) new_operations.extend(result) - elif all(op.reduce(other, [], app_label) is True for op in in_between): + elif all(op.reduce(other, app_label) is True for op in in_between): # Perform a left reduction if all of the in-between # operations can optimize through other. new_operations.extend(result)