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:
parent
37cafbfb79
commit
8a03445885
|
@ -113,7 +113,7 @@ class Operation:
|
||||||
|
|
||||||
return router.allow_migrate_model(connection_alias, model)
|
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
|
Return either a list of operations the actual operation should be
|
||||||
replaced with or a boolean that indicates whether or not the specified
|
replaced with or a boolean that indicates whether or not the specified
|
||||||
|
|
|
@ -60,9 +60,9 @@ class FieldOperation(Operation):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def reduce(self, operation, in_between, app_label=None):
|
def reduce(self, operation, app_label=None):
|
||||||
return (
|
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)
|
not operation.references_field(self.model_name, self.name, app_label)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ class AddField(FieldOperation):
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Add field %s to %s" % (self.name, self.model_name)
|
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, FieldOperation) and self.is_same_field_operation(operation):
|
||||||
if isinstance(operation, AlterField):
|
if isinstance(operation, AlterField):
|
||||||
return [
|
return [
|
||||||
|
@ -142,7 +142,7 @@ class AddField(FieldOperation):
|
||||||
field=self.field,
|
field=self.field,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class RemoveField(FieldOperation):
|
class RemoveField(FieldOperation):
|
||||||
|
@ -186,11 +186,11 @@ class RemoveField(FieldOperation):
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Remove field %s from %s" % (self.name, self.model_name)
|
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
|
from .models import DeleteModel
|
||||||
if isinstance(operation, DeleteModel) and operation.name_lower == self.model_name_lower:
|
if isinstance(operation, DeleteModel) and operation.name_lower == self.model_name_lower:
|
||||||
return [operation]
|
return [operation]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class AlterField(FieldOperation):
|
class AlterField(FieldOperation):
|
||||||
|
@ -256,7 +256,7 @@ class AlterField(FieldOperation):
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Alter field %s on %s" % (self.name, self.model_name)
|
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):
|
if isinstance(operation, RemoveField) and self.is_same_field_operation(operation):
|
||||||
return [operation]
|
return [operation]
|
||||||
elif isinstance(operation, RenameField) and self.is_same_field_operation(operation):
|
elif isinstance(operation, RenameField) and self.is_same_field_operation(operation):
|
||||||
|
@ -268,7 +268,7 @@ class AlterField(FieldOperation):
|
||||||
field=self.field,
|
field=self.field,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class RenameField(FieldOperation):
|
class RenameField(FieldOperation):
|
||||||
|
@ -383,7 +383,7 @@ class RenameField(FieldOperation):
|
||||||
name.lower() == self.new_name_lower
|
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
|
if (isinstance(operation, RenameField) and
|
||||||
self.is_same_model_operation(operation) and
|
self.is_same_model_operation(operation) and
|
||||||
self.new_name_lower == operation.old_name_lower):
|
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`
|
# Skip `FieldOperation.reduce` as we want to run `references_field`
|
||||||
# against self.new_name.
|
# against self.new_name.
|
||||||
return (
|
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)
|
not operation.references_field(self.model_name, self.new_name, app_label)
|
||||||
)
|
)
|
||||||
|
|
|
@ -31,9 +31,9 @@ class ModelOperation(Operation):
|
||||||
def references_model(self, name, app_label=None):
|
def references_model(self, name, app_label=None):
|
||||||
return name.lower() == self.name_lower
|
return name.lower() == self.name_lower
|
||||||
|
|
||||||
def reduce(self, operation, in_between, app_label=None):
|
def reduce(self, operation, app_label=None):
|
||||||
return (
|
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)
|
not operation.references_model(self.name, app_label)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ class CreateModel(ModelOperation):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def reduce(self, operation, in_between, app_label=None):
|
def reduce(self, operation, app_label=None):
|
||||||
if (isinstance(operation, DeleteModel) and
|
if (isinstance(operation, DeleteModel) and
|
||||||
self.name_lower == operation.name_lower and
|
self.name_lower == operation.name_lower and
|
||||||
not self.options.get("proxy", False)):
|
not self.options.get("proxy", False)):
|
||||||
|
@ -193,7 +193,7 @@ class CreateModel(ModelOperation):
|
||||||
managers=self.managers,
|
managers=self.managers,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class DeleteModel(ModelOperation):
|
class DeleteModel(ModelOperation):
|
||||||
|
@ -368,7 +368,7 @@ class RenameModel(ModelOperation):
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Rename model %s to %s" % (self.old_name, self.new_name)
|
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
|
if (isinstance(operation, RenameModel) and
|
||||||
self.new_name_lower == operation.old_name_lower):
|
self.new_name_lower == operation.old_name_lower):
|
||||||
return [
|
return [
|
||||||
|
@ -380,7 +380,7 @@ class RenameModel(ModelOperation):
|
||||||
# Skip `ModelOperation.reduce` as we want to run `references_model`
|
# Skip `ModelOperation.reduce` as we want to run `references_model`
|
||||||
# against self.new_name.
|
# against self.new_name.
|
||||||
return (
|
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)
|
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)"
|
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:
|
if isinstance(operation, (AlterModelTable, DeleteModel)) and self.name_lower == operation.name_lower:
|
||||||
return [operation]
|
return [operation]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class ModelOptionOperation(ModelOperation):
|
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:
|
if isinstance(operation, (self.__class__, DeleteModel)) and self.name_lower == operation.name_lower:
|
||||||
return [operation]
|
return [operation]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class FieldRelatedOptionOperation(ModelOptionOperation):
|
class FieldRelatedOptionOperation(ModelOptionOperation):
|
||||||
def reduce(self, operation, in_between, app_label=None):
|
def reduce(self, operation, app_label=None):
|
||||||
if (isinstance(operation, FieldOperation) and
|
if (isinstance(operation, FieldOperation) and
|
||||||
self.name_lower == operation.model_name_lower and
|
self.name_lower == operation.model_name_lower and
|
||||||
not self.references_field(operation.model_name, operation.name)):
|
not self.references_field(operation.model_name, operation.name)):
|
||||||
return [operation, self]
|
return [operation, self]
|
||||||
return super().reduce(operation, in_between, app_label=app_label)
|
return super().reduce(operation, app_label=app_label)
|
||||||
|
|
||||||
|
|
||||||
class AlterUniqueTogether(FieldRelatedOptionOperation):
|
class AlterUniqueTogether(FieldRelatedOptionOperation):
|
||||||
|
|
|
@ -46,12 +46,12 @@ class MigrationOptimizer:
|
||||||
# Compare it to each operation after it
|
# Compare it to each operation after it
|
||||||
for j, other in enumerate(operations[i + 1:]):
|
for j, other in enumerate(operations[i + 1:]):
|
||||||
in_between = operations[i + 1:i + j + 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 isinstance(result, list):
|
||||||
if right:
|
if right:
|
||||||
new_operations.extend(in_between)
|
new_operations.extend(in_between)
|
||||||
new_operations.extend(result)
|
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
|
# Perform a left reduction if all of the in-between
|
||||||
# operations can optimize through other.
|
# operations can optimize through other.
|
||||||
new_operations.extend(result)
|
new_operations.extend(result)
|
||||||
|
|
Loading…
Reference in New Issue