1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Fixed #22853: Swapped models are now ignored for migration operations.

This commit is contained in:
Andrew Godwin
2014-06-17 17:45:38 -07:00
parent 91f1b6dcdc
commit 8d2ac948a9
6 changed files with 147 additions and 26 deletions

View File

@@ -1,4 +1,5 @@
from __future__ import unicode_literals
from django.db import router
class Operation(object):
@@ -97,6 +98,17 @@ class Operation(object):
"""
return self.references_model(model_name, app_label)
def allowed_to_migrate(self, connection_alias, model):
"""
Returns if we're allowed to migrate the model. Checks the router,
if it's a proxy, and if it's swapped out.
"""
return (
router.allow_migrate(connection_alias, model) and
not model._meta.proxy and
not model._meta.swapped
)
def __repr__(self):
return "<%s %s%s>" % (
self.__class__.__name__,

View File

@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from django.db import router
from django.db.models.fields import NOT_PROVIDED
from django.utils import six
from .base import Operation
@@ -29,7 +28,7 @@ class AddField(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
field = to_model._meta.get_field_by_name(self.name)[0]
if not self.preserve_default:
field.default = self.field.default
@@ -42,7 +41,7 @@ class AddField(Operation):
def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
if router.allow_migrate(schema_editor.connection.alias, from_model):
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 describe(self):
@@ -81,13 +80,13 @@ class RemoveField(Operation):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.render().get_model(app_label, self.model_name)
if router.allow_migrate(schema_editor.connection.alias, from_model):
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):
from_model = from_state.render().get_model(app_label, self.model_name)
to_model = to_state.render().get_model(app_label, self.model_name)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
def describe(self):
@@ -118,7 +117,7 @@ class AlterField(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
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
@@ -170,7 +169,7 @@ class RenameField(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.old_name)[0],
@@ -180,7 +179,7 @@ class RenameField(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
schema_editor.alter_field(
from_model,
from_model._meta.get_field_by_name(self.new_name)[0],

View File

@@ -1,6 +1,6 @@
from __future__ import unicode_literals
from django.db import models, router
from django.db import models
from django.db.models.options import normalize_together
from django.db.migrations.state import ModelState
from django.db.migrations.operations.base import Operation
@@ -32,13 +32,13 @@ 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)
if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
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)
if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.delete_model(model)
def describe(self):
@@ -85,13 +85,13 @@ class DeleteModel(Operation):
def database_forwards(self, app_label, schema_editor, from_state, to_state):
apps = from_state.render()
model = apps.get_model(app_label, self.name)
if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
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)
if router.allow_migrate(schema_editor.connection.alias, model) and not model._meta.proxy:
if self.allowed_to_migrate(schema_editor.connection.alias, model):
schema_editor.create_model(model)
def references_model(self, name, app_label=None):
@@ -141,7 +141,7 @@ class RenameModel(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, new_model):
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
# Move the main table
schema_editor.alter_db_table(
new_model,
@@ -194,7 +194,7 @@ class AlterModelTable(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, new_model):
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
schema_editor.alter_db_table(
new_model,
old_model._meta.db_table,
@@ -231,7 +231,7 @@ class AlterUniqueTogether(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, new_model):
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
schema_editor.alter_unique_together(
new_model,
getattr(old_model._meta, "unique_together", set()),
@@ -268,7 +268,7 @@ class AlterIndexTogether(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, new_model):
if self.allowed_to_migrate(schema_editor.connection.alias, new_model):
schema_editor.alter_index_together(
new_model,
getattr(old_model._meta, "index_together", set()),
@@ -301,7 +301,7 @@ class AlterOrderWithRespectTo(Operation):
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)
if router.allow_migrate(schema_editor.connection.alias, to_model):
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
# 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])