Fixed #26316 -- Factored duplicated code in model/field migration operations.
This commit is contained in:
parent
87994b40b3
commit
b886f166b3
|
@ -26,6 +26,12 @@ class FieldOperation(Operation):
|
||||||
def is_same_field_operation(self, operation):
|
def is_same_field_operation(self, operation):
|
||||||
return self.is_same_model_operation(operation) and self.name_lower == operation.name_lower
|
return self.is_same_model_operation(operation) and self.name_lower == operation.name_lower
|
||||||
|
|
||||||
|
def references_model(self, name, app_label=None):
|
||||||
|
return name.lower() == self.model_name_lower
|
||||||
|
|
||||||
|
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):
|
def reduce(self, operation, in_between, app_label=None):
|
||||||
return (
|
return (
|
||||||
super(FieldOperation, self).reduce(operation, in_between, app_label=app_label) or
|
super(FieldOperation, self).reduce(operation, in_between, app_label=app_label) or
|
||||||
|
@ -89,12 +95,6 @@ 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 references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.model_name_lower
|
|
||||||
|
|
||||||
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):
|
def reduce(self, operation, in_between, 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):
|
||||||
|
@ -156,12 +156,6 @@ 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 references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.model_name_lower
|
|
||||||
|
|
||||||
def references_field(self, model_name, name, app_label=None):
|
|
||||||
return self.references_model(model_name) and name.lower() == self.name_lower
|
|
||||||
|
|
||||||
|
|
||||||
class AlterField(FieldOperation):
|
class AlterField(FieldOperation):
|
||||||
"""
|
"""
|
||||||
|
@ -226,12 +220,6 @@ 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 references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.model_name_lower
|
|
||||||
|
|
||||||
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):
|
def reduce(self, operation, in_between, 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]
|
||||||
|
@ -316,9 +304,6 @@ class RenameField(FieldOperation):
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Rename field %s on %s to %s" % (self.old_name, self.model_name, self.new_name)
|
return "Rename field %s on %s to %s" % (self.old_name, self.model_name, self.new_name)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.model_name_lower
|
|
||||||
|
|
||||||
def references_field(self, model_name, name, app_label=None):
|
def references_field(self, model_name, name, app_label=None):
|
||||||
return self.references_model(model_name) and (
|
return self.references_model(model_name) and (
|
||||||
name.lower() == self.old_name_lower or
|
name.lower() == self.old_name_lower or
|
||||||
|
|
|
@ -20,6 +20,9 @@ class ModelOperation(Operation):
|
||||||
def name_lower(self):
|
def name_lower(self):
|
||||||
return self.name.lower()
|
return self.name.lower()
|
||||||
|
|
||||||
|
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, in_between, app_label=None):
|
||||||
return (
|
return (
|
||||||
super(ModelOperation, self).reduce(operation, in_between, app_label=app_label) or
|
super(ModelOperation, self).reduce(operation, in_between, app_label=app_label) or
|
||||||
|
@ -217,9 +220,6 @@ class DeleteModel(ModelOperation):
|
||||||
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
||||||
schema_editor.create_model(model)
|
schema_editor.create_model(model)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Delete model %s" % (self.name, )
|
return "Delete model %s" % (self.name, )
|
||||||
|
|
||||||
|
@ -421,9 +421,6 @@ class AlterModelTable(ModelOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Rename table for %s to %s" % (self.name, self.table)
|
return "Rename table for %s to %s" % (self.name, self.table)
|
||||||
|
|
||||||
|
@ -490,9 +487,6 @@ class AlterUniqueTogether(FieldRelatedOptionOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def references_field(self, model_name, name, app_label=None):
|
def references_field(self, model_name, name, app_label=None):
|
||||||
return (
|
return (
|
||||||
self.references_model(model_name, app_label) and
|
self.references_model(model_name, app_label) and
|
||||||
|
@ -547,9 +541,6 @@ class AlterIndexTogether(FieldRelatedOptionOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def references_field(self, model_name, name, app_label=None):
|
def references_field(self, model_name, name, app_label=None):
|
||||||
return (
|
return (
|
||||||
self.references_model(model_name, app_label) and
|
self.references_model(model_name, app_label) and
|
||||||
|
@ -609,9 +600,6 @@ class AlterOrderWithRespectTo(FieldRelatedOptionOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
self.database_forwards(app_label, schema_editor, from_state, to_state)
|
self.database_forwards(app_label, schema_editor, from_state, to_state)
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def references_field(self, model_name, name, app_label=None):
|
def references_field(self, model_name, name, app_label=None):
|
||||||
return (
|
return (
|
||||||
self.references_model(model_name, app_label) and
|
self.references_model(model_name, app_label) and
|
||||||
|
@ -674,9 +662,6 @@ class AlterModelOptions(ModelOptionOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Change Meta options on %s" % (self.name, )
|
return "Change Meta options on %s" % (self.name, )
|
||||||
|
|
||||||
|
@ -710,8 +695,5 @@ class AlterModelManagers(ModelOptionOperation):
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def references_model(self, name, app_label=None):
|
|
||||||
return name.lower() == self.name_lower
|
|
||||||
|
|
||||||
def describe(self):
|
def describe(self):
|
||||||
return "Change managers on %s" % (self.name, )
|
return "Change managers on %s" % (self.name, )
|
||||||
|
|
Loading…
Reference in New Issue