Removed in_between from Operation.reduce()'s signature.

It isn't used since FieldOperation.references_model() takes into
account models referenced by the field it's operating on.
This commit is contained in:
Simon Charette 2017-02-18 01:18:01 -05:00 committed by Tim Graham
parent 37cafbfb79
commit 8a03445885
4 changed files with 25 additions and 25 deletions

View File

@ -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

View File

@ -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)
)

View File

@ -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):

View File

@ -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)