Kaydet (Commit) 49f4c9f4 authored tarafından Simon Charette's avatar Simon Charette

Fixed #26064 -- Moved operation reduction logic to their own class.

Thanks to Markus Holtermann and Tim Graham for their review.
üst 6b258bef
......@@ -111,6 +111,14 @@ class Operation(object):
return router.allow_migrate_model(connection_alias, model)
def reduce(self, operation, in_between, 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
operation can be optimized across.
"""
return False
def __repr__(self):
return "<%s %s%s>" % (
self.__class__.__name__,
......
......@@ -7,24 +7,38 @@ from django.utils.functional import cached_property
from .base import Operation
class AddField(Operation):
"""
Adds a field to a model.
"""
def __init__(self, model_name, name, field, preserve_default=True):
class FieldOperation(Operation):
def __init__(self, model_name, name):
self.model_name = model_name
self.name = name
self.field = field
self.preserve_default = preserve_default
@cached_property
def model_name_lower(self):
return self.model_name.lower()
@cached_property
def name_lower(self):
return self.name.lower()
@cached_property
def model_name_lower(self):
return self.model_name.lower()
def is_same_model_operation(self, operation):
return self.model_name_lower == operation.model_name_lower
def is_same_field_operation(self, operation):
return self.is_same_model_operation(operation) and self.name_lower == operation.name_lower
def reduce(self, operation, in_between, app_label=None):
return not operation.references_field(self.model_name, self.name, app_label)
class AddField(FieldOperation):
"""
Adds a field to a model.
"""
def __init__(self, model_name, name, field, preserve_default=True):
self.field = field
self.preserve_default = preserve_default
super(AddField, self).__init__(model_name, name)
def deconstruct(self):
kwargs = {
......@@ -78,24 +92,34 @@ class AddField(Operation):
def references_field(self, model_name, name, app_label=None):
return self.references_model(model_name) and name.lower() == self.name_lower
def reduce(self, operation, in_between, app_label=None):
if isinstance(operation, FieldOperation) and self.is_same_field_operation(operation):
if isinstance(operation, AlterField):
return [
AddField(
model_name=self.model_name,
name=operation.name,
field=operation.field,
),
]
elif isinstance(operation, RemoveField):
return []
elif isinstance(operation, RenameField):
return [
AddField(
model_name=self.model_name,
name=operation.new_name,
field=self.field,
),
]
return super(AddField, self).reduce(operation, in_between, app_label=app_label)
class RemoveField(Operation):
class RemoveField(FieldOperation):
"""
Removes a field from a model.
"""
def __init__(self, model_name, name):
self.model_name = model_name
self.name = name
@cached_property
def name_lower(self):
return self.name.lower()
@cached_property
def model_name_lower(self):
return self.model_name.lower()
def deconstruct(self):
kwargs = {
'model_name': self.model_name,
......@@ -136,24 +160,15 @@ class RemoveField(Operation):
return self.references_model(model_name) and name.lower() == self.name_lower
class AlterField(Operation):
class AlterField(FieldOperation):
"""
Alters a field's database column (e.g. null, max_length) to the provided new field
"""
def __init__(self, model_name, name, field, preserve_default=True):
self.model_name = model_name
self.name = name
self.field = field
self.preserve_default = preserve_default
@cached_property
def name_lower(self):
return self.name.lower()
@cached_property
def model_name_lower(self):
return self.model_name.lower()
super(AlterField, self).__init__(model_name, name)
def deconstruct(self):
kwargs = {
......@@ -214,16 +229,30 @@ class AlterField(Operation):
def references_field(self, model_name, name, app_label=None):
return self.references_model(model_name) and name.lower() == self.name_lower
class RenameField(Operation):
def reduce(self, operation, in_between, 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):
return [
operation,
AlterField(
model_name=self.model_name,
name=operation.new_name,
field=self.field,
),
]
return super(AlterField, self).reduce(operation, in_between, app_label=app_label)
class RenameField(FieldOperation):
"""
Renames a field on the model. Might affect db_column too.
"""
def __init__(self, model_name, old_name, new_name):
self.model_name = model_name
self.old_name = old_name
self.new_name = new_name
super(RenameField, self).__init__(model_name, old_name)
@cached_property
def old_name_lower(self):
......@@ -233,10 +262,6 @@ class RenameField(Operation):
def new_name_lower(self):
return self.new_name.lower()
@cached_property
def model_name_lower(self):
return self.model_name.lower()
def deconstruct(self):
kwargs = {
'model_name': self.model_name,
......@@ -296,3 +321,16 @@ class RenameField(Operation):
name.lower() == self.old_name_lower or
name.lower() == self.new_name_lower
)
def reduce(self, operation, in_between, app_label=None):
if (isinstance(operation, RenameField) and
self.is_same_model_operation(operation) and
self.new_name_lower == operation.old_name_lower):
return [
RenameField(
self.model_name,
self.old_name,
operation.new_name,
),
]
return not operation.references_field(self.model_name, self.new_name, app_label)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment