1
0
mirror of https://github.com/django/django.git synced 2025-10-25 06:36:07 +00:00

Replaced migration state render() by apps cached property

Refs #23745.
This commit is contained in:
Claude Paroz
2014-11-05 10:43:31 +01:00
parent 4ee06ec3fc
commit a159b1facd
9 changed files with 140 additions and 162 deletions

View File

@@ -40,9 +40,9 @@ class AddField(Operation):
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):
to_model = to_state.render().get_model(app_label, self.model_name)
to_model = to_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
field = to_model._meta.get_field_by_name(self.name)[0]
if not self.preserve_default:
field.default = self.field.default
@@ -54,7 +54,7 @@ class AddField(Operation):
field.default = NOT_PROVIDED
def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
@@ -96,14 +96,14 @@ class RemoveField(Operation):
state.models[app_label, self.model_name.lower()].fields = new_fields
def database_forwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
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):
to_model = to_state.render().get_model(app_label, self.model_name)
to_model = to_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
def describe(self):
@@ -152,9 +152,9 @@ class AlterField(Operation):
]
def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.render().get_model(app_label, self.model_name)
to_model = to_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
from_field = from_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
@@ -222,9 +222,9 @@ class RenameField(Operation):
]
def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.render().get_model(app_label, self.model_name)
to_model = to_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.old_name)[0],
@@ -232,9 +232,9 @@ class RenameField(Operation):
)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.render().get_model(app_label, self.model_name)
to_model = to_state.apps.get_model(app_label, self.model_name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.model_name)
from_model = from_state.apps.get_model(app_label, self.model_name)
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.new_name)[0],

View File

@@ -49,14 +49,12 @@ class CreateModel(Operation):
)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
apps = to_state.render()
model = apps.get_model(app_label, self.name)
model = to_state.apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.create_model(model)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
apps = from_state.render()
model = apps.get_model(app_label, self.name)
model = from_state.apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.delete_model(model)
@@ -103,14 +101,12 @@ class DeleteModel(Operation):
del state.models[app_label, self.name.lower()]
def database_forwards(self, app_label, schema_editor, from_state, to_state):
apps = from_state.render()
model = apps.get_model(app_label, self.name)
model = from_state.apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.delete_model(model)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
apps = to_state.render()
model = apps.get_model(app_label, self.name)
model = to_state.apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.create_model(model)
@@ -143,7 +139,7 @@ class RenameModel(Operation):
def state_forwards(self, app_label, state):
# Get all of the related objects we need to repoint
apps = state.render(skip_cache=True)
apps = state.apps
model = apps.get_model(app_label, self.old_name)
related_objects = model._meta.get_all_related_objects()
related_m2m_objects = model._meta.get_all_related_many_to_many_objects()
@@ -168,13 +164,12 @@ class RenameModel(Operation):
field.rel.to = "%s.%s" % (app_label, self.new_name)
new_fields.append((name, field))
state.models[related_key].fields = new_fields
del state.apps # FIXME: this should be replaced by a logic in state (update_model?)
def database_forwards(self, app_label, schema_editor, from_state, to_state):
new_apps = to_state.render()
new_model = new_apps.get_model(app_label, self.new_name)
new_model = to_state.apps.get_model(app_label, self.new_name)
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)
old_model = from_state.apps.get_model(app_label, self.old_name)
# Move the main table
schema_editor.alter_db_table(
new_model,
@@ -194,7 +189,7 @@ class RenameModel(Operation):
related_object.model._meta.app_label,
related_object.model._meta.object_name.lower(),
)
to_field = new_apps.get_model(
to_field = to_state.apps.get_model(
*related_key
)._meta.get_field_by_name(related_object.field.name)[0]
schema_editor.alter_field(
@@ -242,11 +237,9 @@ class AlterModelTable(Operation):
state.models[app_label, self.name.lower()].options["db_table"] = self.table
def database_forwards(self, app_label, schema_editor, from_state, to_state):
new_apps = to_state.render()
new_model = new_apps.get_model(app_label, self.name)
new_model = to_state.apps.get_model(app_label, self.name)
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)
old_model = from_state.apps.get_model(app_label, self.name)
schema_editor.alter_db_table(
new_model,
old_model._meta.db_table,
@@ -299,11 +292,9 @@ class AlterUniqueTogether(Operation):
model_state.options[self.option_name] = self.unique_together
def database_forwards(self, app_label, schema_editor, from_state, to_state):
new_apps = to_state.render()
new_model = new_apps.get_model(app_label, self.name)
new_model = to_state.apps.get_model(app_label, self.name)
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)
old_model = from_state.apps.get_model(app_label, self.name)
schema_editor.alter_unique_together(
new_model,
getattr(old_model._meta, self.option_name, set()),
@@ -348,11 +339,9 @@ class AlterIndexTogether(Operation):
model_state.options[self.option_name] = self.index_together
def database_forwards(self, app_label, schema_editor, from_state, to_state):
new_apps = to_state.render()
new_model = new_apps.get_model(app_label, self.name)
new_model = to_state.apps.get_model(app_label, self.name)
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)
old_model = from_state.apps.get_model(app_label, self.name)
schema_editor.alter_index_together(
new_model,
getattr(old_model._meta, self.option_name, set()),
@@ -394,9 +383,9 @@ class AlterOrderWithRespectTo(Operation):
model_state.options['order_with_respect_to'] = self.order_with_respect_to
def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.render().get_model(app_label, self.name)
to_model = to_state.apps.get_model(app_label, self.name)
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
from_model = from_state.render().get_model(app_label, self.name)
from_model = from_state.apps.get_model(app_label, self.name)
# Remove a field if we need 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])

View File

@@ -166,12 +166,12 @@ class RunPython(Operation):
# object, representing the versioned models as an app registry.
# We could try to override the global cache, but then people will still
# use direct imports, so we go with a documentation approach instead.
self.code(from_state.render(), schema_editor)
self.code(from_state.apps, schema_editor)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
if self.reverse_code is None:
raise NotImplementedError("You cannot reverse this operation")
self.reverse_code(from_state.render(), schema_editor)
self.reverse_code(from_state.apps, schema_editor)
def describe(self):
return "Raw Python operation"