2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-12-04 21:56:22 +08:00
|
|
|
from django.db.models.fields import NOT_PROVIDED
|
2014-03-11 10:52:29 +08:00
|
|
|
from django.utils import six
|
2013-05-30 00:47:10 +08:00
|
|
|
from .base import Operation
|
|
|
|
|
|
|
|
|
|
|
|
class AddField(Operation):
|
|
|
|
"""
|
|
|
|
Adds a field to a model.
|
|
|
|
"""
|
|
|
|
|
2013-12-04 21:56:22 +08:00
|
|
|
def __init__(self, model_name, name, field, preserve_default=True):
|
2013-11-06 21:47:58 +08:00
|
|
|
self.model_name = model_name
|
2013-05-30 00:47:10 +08:00
|
|
|
self.name = name
|
2013-06-19 23:41:04 +08:00
|
|
|
self.field = field
|
2013-12-04 21:56:22 +08:00
|
|
|
self.preserve_default = preserve_default
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
2013-12-04 21:56:22 +08:00
|
|
|
# If preserve default is off, don't use the default for future state
|
|
|
|
if not self.preserve_default:
|
|
|
|
field = self.field.clone()
|
|
|
|
field.default = NOT_PROVIDED
|
|
|
|
else:
|
|
|
|
field = self.field
|
|
|
|
state.models[app_label, self.model_name.lower()].fields.append((self.name, field))
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2013-12-11 22:19:05 +08:00
|
|
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
2013-05-31 01:24:20 +08:00
|
|
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
2014-01-20 01:42:29 +08:00
|
|
|
field = to_model._meta.get_field_by_name(self.name)[0]
|
|
|
|
if not self.preserve_default:
|
|
|
|
field.default = self.field.default
|
|
|
|
schema_editor.add_field(
|
|
|
|
from_model,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
if not self.preserve_default:
|
|
|
|
field.default = NOT_PROVIDED
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2013-05-31 01:24:20 +08:00
|
|
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
|
2013-05-30 00:47:10 +08:00
|
|
|
|
2013-06-19 23:23:52 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Add field %s to %s" % (self.name, self.model_name)
|
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
(self.__class__ == other.__class__) and
|
|
|
|
(self.name == other.name) and
|
2013-11-06 21:47:58 +08:00
|
|
|
(self.model_name.lower() == other.model_name.lower()) and
|
2013-10-03 00:33:41 +08:00
|
|
|
(self.field.deconstruct()[1:] == other.field.deconstruct()[1:])
|
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
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()
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
class RemoveField(Operation):
|
|
|
|
"""
|
|
|
|
Removes a field from a model.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, model_name, name):
|
2013-11-06 21:47:58 +08:00
|
|
|
self.model_name = model_name
|
2013-05-30 00:47:10 +08:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
new_fields = []
|
|
|
|
for name, instance in state.models[app_label, self.model_name.lower()].fields:
|
|
|
|
if name != self.name:
|
|
|
|
new_fields.append((name, instance))
|
|
|
|
state.models[app_label, self.model_name.lower()].fields = new_fields
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2013-05-31 01:24:20 +08:00
|
|
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, from_model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2013-12-11 22:19:05 +08:00
|
|
|
from_model = from_state.render().get_model(app_label, self.model_name)
|
2013-05-31 01:24:20 +08:00
|
|
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
2013-12-11 22:19:05 +08:00
|
|
|
schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
|
2013-06-19 23:23:52 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Remove field %s from %s" % (self.name, self.model_name)
|
2013-06-20 22:12:59 +08:00
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
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()
|
|
|
|
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
class AlterField(Operation):
|
|
|
|
"""
|
|
|
|
Alters a field's database column (e.g. null, max_length) to the provided new field
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, model_name, name, field):
|
2013-11-06 21:47:58 +08:00
|
|
|
self.model_name = model_name
|
2013-06-20 22:12:59 +08:00
|
|
|
self.name = name
|
|
|
|
self.field = field
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
state.models[app_label, self.model_name.lower()].fields = [
|
|
|
|
(n, self.field if n == self.name else f) for n, f in state.models[app_label, self.model_name.lower()].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)
|
|
|
|
to_model = to_state.render().get_model(app_label, self.model_name)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
2014-03-11 10:52:29 +08:00
|
|
|
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
|
|
|
|
# set it equal to the other field side. Bandaid fix for AlterField
|
|
|
|
# migrations that are part of a RenameModel change.
|
|
|
|
if from_field.rel and from_field.rel.to:
|
|
|
|
if isinstance(from_field.rel.to, six.string_types):
|
|
|
|
from_field.rel.to = to_field.rel.to
|
2014-08-07 09:52:03 +08:00
|
|
|
elif to_field.rel and isinstance(to_field.rel.to, six.string_types):
|
2014-03-11 10:52:29 +08:00
|
|
|
to_field.rel.to = from_field.rel.to
|
|
|
|
schema_editor.alter_field(from_model, from_field, to_field)
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
self.database_forwards(app_label, schema_editor, from_state, to_state)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Alter field %s on %s" % (self.name, self.model_name)
|
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
(self.__class__ == other.__class__) and
|
|
|
|
(self.name == other.name) and
|
2013-11-06 21:47:58 +08:00
|
|
|
(self.model_name.lower() == other.model_name.lower()) and
|
2013-10-03 00:33:41 +08:00
|
|
|
(self.field.deconstruct()[1:] == other.field.deconstruct()[1:])
|
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
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()
|
|
|
|
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
class RenameField(Operation):
|
|
|
|
"""
|
|
|
|
Renames a field on the model. Might affect db_column too.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, model_name, old_name, new_name):
|
2013-11-06 21:47:58 +08:00
|
|
|
self.model_name = model_name
|
2013-06-20 22:12:59 +08:00
|
|
|
self.old_name = old_name
|
|
|
|
self.new_name = new_name
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
2014-07-21 18:36:34 +08:00
|
|
|
# Rename the field
|
2013-06-20 22:12:59 +08:00
|
|
|
state.models[app_label, self.model_name.lower()].fields = [
|
|
|
|
(self.new_name if n == self.old_name else n, f) for n, f in state.models[app_label, self.model_name.lower()].fields
|
|
|
|
]
|
2014-07-21 18:36:34 +08:00
|
|
|
# Fix unique_together to refer to the new field
|
|
|
|
options = state.models[app_label, self.model_name.lower()].options
|
|
|
|
if "unique_together" in options:
|
|
|
|
options['unique_together'] = [
|
|
|
|
[self.new_name if n == self.old_name else n for n in unique]
|
|
|
|
for unique in options['unique_together']
|
|
|
|
]
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
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)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.alter_field(
|
2013-12-11 22:19:05 +08:00
|
|
|
from_model,
|
2013-07-30 19:34:31 +08:00
|
|
|
from_model._meta.get_field_by_name(self.old_name)[0],
|
|
|
|
to_model._meta.get_field_by_name(self.new_name)[0],
|
|
|
|
)
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
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)
|
2014-06-18 08:45:38 +08:00
|
|
|
if self.allowed_to_migrate(schema_editor.connection.alias, to_model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.alter_field(
|
2013-12-11 22:19:05 +08:00
|
|
|
from_model,
|
2013-07-30 19:34:31 +08:00
|
|
|
from_model._meta.get_field_by_name(self.new_name)[0],
|
|
|
|
to_model._meta.get_field_by_name(self.old_name)[0],
|
|
|
|
)
|
2013-06-20 22:12:59 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Rename field %s on %s to %s" % (self.old_name, self.model_name, self.new_name)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
|
|
|
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.old_name.lower() or
|
2013-11-07 12:00:48 +08:00
|
|
|
name.lower() == self.new_name.lower()
|
2013-11-06 21:47:58 +08:00
|
|
|
)
|