[1.7.x] Avoided unneeded calls to state.render() in migrations.
Backport of 19ae13d9ed
from master
This commit is contained in:
parent
4fbeec5631
commit
49d4095767
|
@ -26,9 +26,9 @@ class AddField(Operation):
|
||||||
state.models[app_label, self.model_name.lower()].fields.append((self.name, field))
|
state.models[app_label, self.model_name.lower()].fields.append((self.name, field))
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.model_name)
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||||
field = to_model._meta.get_field_by_name(self.name)[0]
|
field = to_model._meta.get_field_by_name(self.name)[0]
|
||||||
if not self.preserve_default:
|
if not self.preserve_default:
|
||||||
field.default = self.field.default
|
field.default = self.field.default
|
||||||
|
@ -84,9 +84,9 @@ class RemoveField(Operation):
|
||||||
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
|
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
|
||||||
|
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.model_name)
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||||
schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
|
schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
|
||||||
|
|
||||||
def describe(self):
|
def describe(self):
|
||||||
|
@ -121,9 +121,9 @@ class AlterField(Operation):
|
||||||
]
|
]
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.model_name)
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||||
from_field = from_model._meta.get_field_by_name(self.name)[0]
|
from_field = from_model._meta.get_field_by_name(self.name)[0]
|
||||||
to_field = to_model._meta.get_field_by_name(self.name)[0]
|
to_field = to_model._meta.get_field_by_name(self.name)[0]
|
||||||
# If the field is a relatedfield with an unresolved rel.to, just
|
# If the field is a relatedfield with an unresolved rel.to, just
|
||||||
|
@ -186,9 +186,9 @@ class RenameField(Operation):
|
||||||
]
|
]
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.model_name)
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||||
schema_editor.alter_field(
|
schema_editor.alter_field(
|
||||||
from_model,
|
from_model,
|
||||||
from_model._meta.get_field_by_name(self.old_name)[0],
|
from_model._meta.get_field_by_name(self.old_name)[0],
|
||||||
|
@ -196,9 +196,9 @@ class RenameField(Operation):
|
||||||
)
|
)
|
||||||
|
|
||||||
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.model_name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.model_name)
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
||||||
schema_editor.alter_field(
|
schema_editor.alter_field(
|
||||||
from_model,
|
from_model,
|
||||||
from_model._meta.get_field_by_name(self.new_name)[0],
|
from_model._meta.get_field_by_name(self.new_name)[0],
|
||||||
|
|
|
@ -141,11 +141,11 @@ class RenameModel(Operation):
|
||||||
state.models[related_key].fields = new_fields
|
state.models[related_key].fields = new_fields
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
old_apps = from_state.render()
|
|
||||||
new_apps = to_state.render()
|
new_apps = to_state.render()
|
||||||
old_model = old_apps.get_model(app_label, self.old_name)
|
|
||||||
new_model = new_apps.get_model(app_label, self.new_name)
|
new_model = new_apps.get_model(app_label, self.new_name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
||||||
|
old_apps = from_state.render()
|
||||||
|
old_model = old_apps.get_model(app_label, self.old_name)
|
||||||
# Move the main table
|
# Move the main table
|
||||||
schema_editor.alter_db_table(
|
schema_editor.alter_db_table(
|
||||||
new_model,
|
new_model,
|
||||||
|
@ -202,11 +202,11 @@ class AlterModelTable(Operation):
|
||||||
state.models[app_label, self.name.lower()].options["db_table"] = self.table
|
state.models[app_label, self.name.lower()].options["db_table"] = self.table
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
old_apps = from_state.render()
|
|
||||||
new_apps = to_state.render()
|
new_apps = to_state.render()
|
||||||
old_model = old_apps.get_model(app_label, self.name)
|
|
||||||
new_model = new_apps.get_model(app_label, self.name)
|
new_model = new_apps.get_model(app_label, self.name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
||||||
|
old_apps = from_state.render()
|
||||||
|
old_model = old_apps.get_model(app_label, self.name)
|
||||||
schema_editor.alter_db_table(
|
schema_editor.alter_db_table(
|
||||||
new_model,
|
new_model,
|
||||||
old_model._meta.db_table,
|
old_model._meta.db_table,
|
||||||
|
@ -248,11 +248,11 @@ class AlterUniqueTogether(Operation):
|
||||||
model_state.options[self.option_name] = self.unique_together
|
model_state.options[self.option_name] = self.unique_together
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
old_apps = from_state.render()
|
|
||||||
new_apps = to_state.render()
|
new_apps = to_state.render()
|
||||||
old_model = old_apps.get_model(app_label, self.name)
|
|
||||||
new_model = new_apps.get_model(app_label, self.name)
|
new_model = new_apps.get_model(app_label, self.name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
||||||
|
old_apps = from_state.render()
|
||||||
|
old_model = old_apps.get_model(app_label, self.name)
|
||||||
schema_editor.alter_unique_together(
|
schema_editor.alter_unique_together(
|
||||||
new_model,
|
new_model,
|
||||||
getattr(old_model._meta, self.option_name, set()),
|
getattr(old_model._meta, self.option_name, set()),
|
||||||
|
@ -286,11 +286,11 @@ class AlterIndexTogether(Operation):
|
||||||
model_state.options[self.option_name] = self.index_together
|
model_state.options[self.option_name] = self.index_together
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
old_apps = from_state.render()
|
|
||||||
new_apps = to_state.render()
|
new_apps = to_state.render()
|
||||||
old_model = old_apps.get_model(app_label, self.name)
|
|
||||||
new_model = new_apps.get_model(app_label, self.name)
|
new_model = new_apps.get_model(app_label, self.name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
|
||||||
|
old_apps = from_state.render()
|
||||||
|
old_model = old_apps.get_model(app_label, self.name)
|
||||||
schema_editor.alter_index_together(
|
schema_editor.alter_index_together(
|
||||||
new_model,
|
new_model,
|
||||||
getattr(old_model._meta, self.option_name, set()),
|
getattr(old_model._meta, self.option_name, set()),
|
||||||
|
@ -321,9 +321,9 @@ class AlterOrderWithRespectTo(Operation):
|
||||||
model_state.options['order_with_respect_to'] = self.order_with_respect_to
|
model_state.options['order_with_respect_to'] = self.order_with_respect_to
|
||||||
|
|
||||||
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
||||||
from_model = from_state.render().get_model(app_label, self.name)
|
|
||||||
to_model = to_state.render().get_model(app_label, self.name)
|
to_model = to_state.render().get_model(app_label, self.name)
|
||||||
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
||||||
|
from_model = from_state.render().get_model(app_label, self.name)
|
||||||
# Remove a field if we need to
|
# Remove a field if we need to
|
||||||
if from_model._meta.order_with_respect_to and not to_model._meta.order_with_respect_to:
|
if from_model._meta.order_with_respect_to and not to_model._meta.order_with_respect_to:
|
||||||
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name("_order")[0])
|
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name("_order")[0])
|
||||||
|
|
Loading…
Reference in New Issue