2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2014-06-18 08:45:38 +08:00
|
|
|
from django.db import models
|
2013-09-26 15:25:35 +08:00
|
|
|
from django.db.migrations.operations.base import Operation
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.migrations.state import ModelState
|
|
|
|
from django.db.models.options import normalize_together
|
2013-09-26 15:25:35 +08:00
|
|
|
from django.utils import six
|
2015-01-02 23:37:21 +08:00
|
|
|
from django.utils.functional import cached_property
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
from .fields import (
|
|
|
|
AddField, AlterField, FieldOperation, RemoveField, RenameField,
|
|
|
|
)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
|
|
|
|
class ModelOperation(Operation):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def name_lower(self):
|
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
2016-01-09 16:12:46 +08:00
|
|
|
return (
|
|
|
|
super(ModelOperation, self).reduce(operation, in_between, app_label=app_label) or
|
|
|
|
not operation.references_model(self.name, app_label)
|
|
|
|
)
|
2016-01-09 15:36:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CreateModel(ModelOperation):
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
Create a model's table.
|
|
|
|
"""
|
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
serialization_expand_args = ['fields', 'options', 'managers']
|
2013-09-26 15:25:35 +08:00
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
def __init__(self, name, fields, options=None, bases=None, managers=None):
|
2013-05-30 00:47:10 +08:00
|
|
|
self.fields = fields
|
|
|
|
self.options = options or {}
|
|
|
|
self.bases = bases or (models.Model,)
|
2014-12-13 06:19:58 +08:00
|
|
|
self.managers = managers or []
|
2016-01-09 15:36:09 +08:00
|
|
|
super(CreateModel, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'fields': self.fields,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
if self.options:
|
|
|
|
kwargs['options'] = self.options
|
|
|
|
if self.bases and self.bases != (models.Model,):
|
|
|
|
kwargs['bases'] = self.bases
|
2014-12-13 06:19:58 +08:00
|
|
|
if self.managers and self.managers != [('objects', models.Manager())]:
|
|
|
|
kwargs['managers'] = self.managers
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
2014-11-16 03:25:43 +08:00
|
|
|
kwargs
|
|
|
|
)
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2014-11-06 03:53:39 +08:00
|
|
|
state.add_model(ModelState(
|
2014-06-06 14:34:13 +08:00
|
|
|
app_label,
|
|
|
|
self.name,
|
|
|
|
list(self.fields),
|
|
|
|
dict(self.options),
|
|
|
|
tuple(self.bases),
|
2014-12-13 06:19:58 +08:00
|
|
|
list(self.managers),
|
2014-11-06 03:53:39 +08:00
|
|
|
))
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-07-30 19:34:31 +08:00
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.create_model(model)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-07-30 19:34:31 +08:00
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
model = from_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.delete_model(model)
|
2013-05-30 00:47:10 +08:00
|
|
|
|
2013-06-19 23:23:52 +08:00
|
|
|
def describe(self):
|
2014-06-16 07:01:49 +08:00
|
|
|
return "Create %smodel %s" % ("proxy " if self.options.get("proxy", False) else "", self.name)
|
2013-06-19 23:23:52 +08:00
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def references_model(self, name, app_label=None):
|
|
|
|
strings_to_check = [self.name]
|
|
|
|
# Check we didn't inherit from the model
|
|
|
|
for base in self.bases:
|
|
|
|
if isinstance(base, six.string_types):
|
|
|
|
strings_to_check.append(base.split(".")[-1])
|
|
|
|
# Check we have no FKs/M2Ms with it
|
|
|
|
for fname, field in self.fields:
|
2015-02-26 22:19:17 +08:00
|
|
|
if field.remote_field:
|
|
|
|
if isinstance(field.remote_field.model, six.string_types):
|
|
|
|
strings_to_check.append(field.remote_field.model.split(".")[-1])
|
2013-10-16 18:09:33 +08:00
|
|
|
# Now go over all the strings and compare them
|
|
|
|
for string in strings_to_check:
|
|
|
|
if string.lower() == name.lower():
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
def model_to_key(self, model):
|
|
|
|
"""
|
|
|
|
Take either a model class or an "app_label.ModelName" string
|
|
|
|
and return (app_label, object_name).
|
|
|
|
"""
|
|
|
|
if isinstance(model, six.string_types):
|
|
|
|
return model.split(".", 1)
|
|
|
|
else:
|
|
|
|
return model._meta.app_label, model._meta.object_name
|
|
|
|
|
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
|
|
|
if (isinstance(operation, DeleteModel) and
|
|
|
|
self.name_lower == operation.name_lower and
|
|
|
|
not self.options.get("proxy", False)):
|
|
|
|
return []
|
|
|
|
elif isinstance(operation, RenameModel) and self.name_lower == operation.old_name_lower:
|
|
|
|
return [
|
|
|
|
CreateModel(
|
|
|
|
operation.new_name,
|
|
|
|
fields=self.fields,
|
|
|
|
options=self.options,
|
|
|
|
bases=self.bases,
|
|
|
|
managers=self.managers,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
elif isinstance(operation, FieldOperation) and self.name_lower == operation.model_name_lower:
|
|
|
|
if isinstance(operation, AddField):
|
|
|
|
# Don't allow optimizations of FKs through models they reference
|
|
|
|
if hasattr(operation.field, "remote_field") and operation.field.remote_field:
|
|
|
|
for between in in_between:
|
|
|
|
# Check that it doesn't point to the model
|
|
|
|
app_label, object_name = self.model_to_key(operation.field.remote_field.model)
|
|
|
|
if between.references_model(object_name, app_label):
|
|
|
|
return False
|
|
|
|
# Check that it's not through the model
|
|
|
|
if getattr(operation.field.remote_field, "through", None):
|
|
|
|
app_label, object_name = self.model_to_key(operation.field.remote_field.through)
|
|
|
|
if between.references_model(object_name, app_label):
|
|
|
|
return False
|
|
|
|
return [
|
|
|
|
CreateModel(
|
|
|
|
self.name,
|
|
|
|
fields=self.fields + [(operation.name, operation.field)],
|
|
|
|
options=self.options,
|
|
|
|
bases=self.bases,
|
|
|
|
managers=self.managers,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
elif isinstance(operation, AlterField):
|
|
|
|
return [
|
|
|
|
CreateModel(
|
|
|
|
self.name,
|
|
|
|
fields=[
|
|
|
|
(n, operation.field if n == operation.name else v)
|
|
|
|
for n, v in self.fields
|
|
|
|
],
|
|
|
|
options=self.options,
|
|
|
|
bases=self.bases,
|
|
|
|
managers=self.managers,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
elif isinstance(operation, RemoveField):
|
|
|
|
return [
|
|
|
|
CreateModel(
|
|
|
|
self.name,
|
|
|
|
fields=[
|
|
|
|
(n, v)
|
|
|
|
for n, v in self.fields
|
|
|
|
if n.lower() != operation.name_lower
|
|
|
|
],
|
|
|
|
options=self.options,
|
|
|
|
bases=self.bases,
|
|
|
|
managers=self.managers,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
elif isinstance(operation, RenameField):
|
|
|
|
return [
|
|
|
|
CreateModel(
|
|
|
|
self.name,
|
|
|
|
fields=[
|
|
|
|
(operation.new_name if n == operation.old_name else n, v)
|
|
|
|
for n, v in self.fields
|
|
|
|
],
|
|
|
|
options=self.options,
|
|
|
|
bases=self.bases,
|
|
|
|
managers=self.managers,
|
|
|
|
),
|
|
|
|
]
|
|
|
|
return super(CreateModel, self).reduce(operation, in_between, app_label=app_label)
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteModel(ModelOperation):
|
2013-05-30 00:47:10 +08:00
|
|
|
"""
|
|
|
|
Drops a model's table.
|
|
|
|
"""
|
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
state.remove_model(app_label, self.name_lower)
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
model = from_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.delete_model(model)
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, model):
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.create_model(model)
|
2013-06-19 23:23:52 +08:00
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2013-10-16 18:09:33 +08:00
|
|
|
|
2013-06-19 23:23:52 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Delete model %s" % (self.name, )
|
2013-06-20 21:54:11 +08:00
|
|
|
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class RenameModel(ModelOperation):
|
2013-11-06 21:47:58 +08:00
|
|
|
"""
|
|
|
|
Renames a model.
|
|
|
|
"""
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, old_name, new_name):
|
2013-11-06 21:47:58 +08:00
|
|
|
self.old_name = old_name
|
|
|
|
self.new_name = new_name
|
2016-01-09 15:36:09 +08:00
|
|
|
super(RenameModel, self).__init__(old_name)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
2015-01-02 23:37:21 +08:00
|
|
|
@cached_property
|
|
|
|
def old_name_lower(self):
|
|
|
|
return self.old_name.lower()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def new_name_lower(self):
|
|
|
|
return self.new_name.lower()
|
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'old_name': self.old_name,
|
|
|
|
'new_name': self.new_name,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2014-11-05 17:43:31 +08:00
|
|
|
apps = state.apps
|
2014-06-08 09:17:31 +08:00
|
|
|
model = apps.get_model(app_label, self.old_name)
|
2014-11-06 03:53:39 +08:00
|
|
|
model._meta.apps = apps
|
2015-01-07 08:16:35 +08:00
|
|
|
# Get all of the related objects we need to repoint
|
|
|
|
all_related_objects = (
|
|
|
|
f for f in model._meta.get_fields(include_hidden=True)
|
2015-04-30 07:39:50 +08:00
|
|
|
if f.auto_created and not f.concrete and (not f.hidden or f.many_to_many)
|
2015-01-07 08:16:35 +08:00
|
|
|
)
|
2014-06-08 09:17:31 +08:00
|
|
|
# Rename the model
|
2015-01-02 23:37:21 +08:00
|
|
|
state.models[app_label, self.new_name_lower] = state.models[app_label, self.old_name_lower]
|
|
|
|
state.models[app_label, self.new_name_lower].name = self.new_name
|
|
|
|
state.remove_model(app_label, self.old_name_lower)
|
2014-06-08 09:17:31 +08:00
|
|
|
# Repoint the FKs and M2Ms pointing to us
|
2015-01-07 08:16:35 +08:00
|
|
|
for related_object in all_related_objects:
|
2015-02-17 03:42:11 +08:00
|
|
|
if related_object.model is not model:
|
|
|
|
# The model being renamed does not participate in this relation
|
|
|
|
# directly. Rather, a superclass does.
|
|
|
|
continue
|
2014-08-04 03:02:40 +08:00
|
|
|
# Use the new related key for self referential related objects.
|
2015-01-07 08:16:35 +08:00
|
|
|
if related_object.related_model == model:
|
2015-01-02 23:37:21 +08:00
|
|
|
related_key = (app_label, self.new_name_lower)
|
2014-08-04 03:02:40 +08:00
|
|
|
else:
|
|
|
|
related_key = (
|
2015-01-07 08:16:35 +08:00
|
|
|
related_object.related_model._meta.app_label,
|
2015-01-02 23:37:21 +08:00
|
|
|
related_object.related_model._meta.model_name,
|
2014-08-04 03:02:40 +08:00
|
|
|
)
|
2014-06-08 09:17:31 +08:00
|
|
|
new_fields = []
|
|
|
|
for name, field in state.models[related_key].fields:
|
|
|
|
if name == related_object.field.name:
|
|
|
|
field = field.clone()
|
2015-02-26 22:19:17 +08:00
|
|
|
field.remote_field.model = "%s.%s" % (app_label, self.new_name)
|
2014-06-08 09:17:31 +08:00
|
|
|
new_fields.append((name, field))
|
|
|
|
state.models[related_key].fields = new_fields
|
2014-11-06 03:53:39 +08:00
|
|
|
state.reload_model(*related_key)
|
2015-01-02 23:37:21 +08:00
|
|
|
state.reload_model(app_label, self.new_name_lower)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
new_model = to_state.apps.get_model(app_label, self.new_name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, new_model):
|
2014-11-05 17:43:31 +08:00
|
|
|
old_model = from_state.apps.get_model(app_label, self.old_name)
|
2014-06-08 09:17:31 +08:00
|
|
|
# Move the main table
|
2013-11-06 21:47:58 +08:00
|
|
|
schema_editor.alter_db_table(
|
|
|
|
new_model,
|
|
|
|
old_model._meta.db_table,
|
|
|
|
new_model._meta.db_table,
|
|
|
|
)
|
2014-06-08 09:17:31 +08:00
|
|
|
# Alter the fields pointing to us
|
2015-01-07 08:16:35 +08:00
|
|
|
for related_object in old_model._meta.related_objects:
|
|
|
|
if related_object.related_model == old_model:
|
2014-08-04 03:02:40 +08:00
|
|
|
model = new_model
|
2015-01-02 23:37:21 +08:00
|
|
|
related_key = (app_label, self.new_name_lower)
|
2014-08-04 03:02:40 +08:00
|
|
|
else:
|
2015-01-07 08:16:35 +08:00
|
|
|
model = related_object.related_model
|
2014-08-04 03:02:40 +08:00
|
|
|
related_key = (
|
2015-01-07 08:16:35 +08:00
|
|
|
related_object.related_model._meta.app_label,
|
2015-01-02 23:37:21 +08:00
|
|
|
related_object.related_model._meta.model_name,
|
2014-08-04 03:02:40 +08:00
|
|
|
)
|
2014-11-05 17:43:31 +08:00
|
|
|
to_field = to_state.apps.get_model(
|
2014-08-04 03:02:40 +08:00
|
|
|
*related_key
|
2015-01-07 08:16:35 +08:00
|
|
|
)._meta.get_field(related_object.field.name)
|
2014-06-08 09:17:31 +08:00
|
|
|
schema_editor.alter_field(
|
2014-08-04 03:02:40 +08:00
|
|
|
model,
|
2014-06-08 09:17:31 +08:00
|
|
|
related_object.field,
|
|
|
|
to_field,
|
|
|
|
)
|
2015-01-15 22:56:07 +08:00
|
|
|
# Rename M2M fields whose name is based on this model's name.
|
|
|
|
fields = zip(old_model._meta.local_many_to_many, new_model._meta.local_many_to_many)
|
|
|
|
for (old_field, new_field) in fields:
|
|
|
|
# Skip self-referential fields as these are renamed above.
|
2015-02-26 22:19:17 +08:00
|
|
|
if new_field.model == new_field.related_model or not new_field.remote_field.through._meta.auto_created:
|
2015-01-15 22:56:07 +08:00
|
|
|
continue
|
|
|
|
# Rename the M2M table that's based on this model's name.
|
2015-02-26 22:19:17 +08:00
|
|
|
old_m2m_model = old_field.remote_field.through
|
|
|
|
new_m2m_model = new_field.remote_field.through
|
2015-01-15 22:56:07 +08:00
|
|
|
schema_editor.alter_db_table(
|
|
|
|
new_m2m_model,
|
|
|
|
old_m2m_model._meta.db_table,
|
|
|
|
new_m2m_model._meta.db_table,
|
|
|
|
)
|
|
|
|
# Rename the column in the M2M table that's based on this
|
|
|
|
# model's name.
|
|
|
|
schema_editor.alter_field(
|
|
|
|
new_m2m_model,
|
|
|
|
old_m2m_model._meta.get_field(old_model._meta.model_name),
|
|
|
|
new_m2m_model._meta.get_field(new_model._meta.model_name),
|
|
|
|
)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2015-01-02 23:37:21 +08:00
|
|
|
self.new_name_lower, self.old_name_lower = self.old_name_lower, self.new_name_lower
|
2014-06-08 09:17:31 +08:00
|
|
|
self.new_name, self.old_name = self.old_name, self.new_name
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-06-08 09:17:31 +08:00
|
|
|
self.database_forwards(app_label, schema_editor, from_state, to_state)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
|
|
|
self.new_name_lower, self.old_name_lower = self.old_name_lower, self.new_name_lower
|
2014-06-08 09:17:31 +08:00
|
|
|
self.new_name, self.old_name = self.old_name, self.new_name
|
2013-11-06 21:47:58 +08:00
|
|
|
|
|
|
|
def references_model(self, name, app_label=None):
|
|
|
|
return (
|
2015-01-02 23:37:21 +08:00
|
|
|
name.lower() == self.old_name_lower or
|
|
|
|
name.lower() == self.new_name_lower
|
2013-11-06 21:47:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Rename model %s to %s" % (self.old_name, self.new_name)
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
|
|
|
if (isinstance(operation, RenameModel) and
|
|
|
|
self.new_name_lower == operation.old_name_lower):
|
|
|
|
return [
|
|
|
|
RenameModel(
|
|
|
|
self.old_name,
|
|
|
|
operation.new_name,
|
|
|
|
),
|
|
|
|
]
|
2016-01-09 16:12:46 +08:00
|
|
|
# Skip `ModelOperation.reduce` as we want to run `references_model`
|
|
|
|
# against self.new_name.
|
|
|
|
return (
|
|
|
|
super(ModelOperation, self).reduce(operation, in_between, app_label=app_label) or
|
|
|
|
not operation.references_model(self.new_name, app_label)
|
|
|
|
)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
|
|
|
|
class AlterModelTable(ModelOperation):
|
2013-06-20 21:54:11 +08:00
|
|
|
"""
|
|
|
|
Renames a model's table
|
|
|
|
"""
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name, table):
|
2013-06-20 21:54:11 +08:00
|
|
|
self.table = table
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterModelTable, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'table': self.table,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2013-06-20 21:54:11 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
state.models[app_label, self.name_lower].options["db_table"] = self.table
|
|
|
|
state.reload_model(app_label, self.name_lower)
|
2013-06-20 21:54:11 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
new_model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, new_model):
|
2014-11-05 17:43:31 +08:00
|
|
|
old_model = from_state.apps.get_model(app_label, self.name)
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.alter_db_table(
|
|
|
|
new_model,
|
|
|
|
old_model._meta.db_table,
|
|
|
|
new_model._meta.db_table,
|
|
|
|
)
|
2014-10-22 21:59:00 +08:00
|
|
|
# Rename M2M fields whose name is based on this model's db_table
|
|
|
|
for (old_field, new_field) in zip(old_model._meta.local_many_to_many, new_model._meta.local_many_to_many):
|
2015-02-26 22:19:17 +08:00
|
|
|
if new_field.remote_field.through._meta.auto_created:
|
2014-10-22 21:59:00 +08:00
|
|
|
schema_editor.alter_db_table(
|
2015-02-26 22:19:17 +08:00
|
|
|
new_field.remote_field.through,
|
|
|
|
old_field.remote_field.through._meta.db_table,
|
|
|
|
new_field.remote_field.through._meta.db_table,
|
2014-10-22 21:59:00 +08:00
|
|
|
)
|
2013-06-20 21:54:11 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2013-10-16 18:09:33 +08:00
|
|
|
|
2013-06-20 21:54:11 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Rename table for %s to %s" % (self.name, self.table)
|
2013-07-02 18:19:02 +08:00
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
|
|
|
if isinstance(operation, (AlterModelTable, DeleteModel)) and self.name_lower == operation.name_lower:
|
|
|
|
return [operation]
|
|
|
|
return super(AlterModelTable, self).reduce(operation, in_between, app_label=app_label)
|
|
|
|
|
2013-07-02 18:19:02 +08:00
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class ModelOptionOperation(ModelOperation):
|
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
|
|
|
if isinstance(operation, (self.__class__, DeleteModel)) and self.name_lower == operation.name_lower:
|
|
|
|
return [operation]
|
|
|
|
return super(ModelOptionOperation, self).reduce(operation, in_between, app_label=app_label)
|
|
|
|
|
|
|
|
|
|
|
|
class FieldRelatedOptionOperation(ModelOptionOperation):
|
|
|
|
def reduce(self, operation, in_between, app_label=None):
|
|
|
|
if (isinstance(operation, FieldOperation) and
|
|
|
|
self.name_lower == operation.model_name_lower and
|
|
|
|
not self.references_field(operation.model_name, operation.name)):
|
|
|
|
return [operation, self]
|
|
|
|
return super(FieldRelatedOptionOperation, self).reduce(operation, in_between, app_label=app_label)
|
|
|
|
|
|
|
|
|
|
|
|
class AlterUniqueTogether(FieldRelatedOptionOperation):
|
2013-07-02 18:19:02 +08:00
|
|
|
"""
|
2014-06-25 20:53:09 +08:00
|
|
|
Changes the value of unique_together to the target one.
|
2013-07-02 18:19:02 +08:00
|
|
|
Input value of unique_together must be a set of tuples.
|
|
|
|
"""
|
2014-06-25 20:53:09 +08:00
|
|
|
option_name = "unique_together"
|
2013-07-02 18:19:02 +08:00
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name, unique_together):
|
2014-03-02 03:06:15 +08:00
|
|
|
unique_together = normalize_together(unique_together)
|
2014-09-10 05:28:45 +08:00
|
|
|
self.unique_together = set(tuple(cons) for cons in unique_together)
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterUniqueTogether, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'unique_together': self.unique_together,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2013-07-02 18:19:02 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
model_state = state.models[app_label, self.name_lower]
|
2014-06-25 20:53:09 +08:00
|
|
|
model_state.options[self.option_name] = self.unique_together
|
2015-01-02 23:37:21 +08:00
|
|
|
state.reload_model(app_label, self.name_lower)
|
2013-07-02 18:19:02 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
new_model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, new_model):
|
2014-11-05 17:43:31 +08:00
|
|
|
old_model = from_state.apps.get_model(app_label, self.name)
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.alter_unique_together(
|
|
|
|
new_model,
|
2014-06-25 20:53:09 +08:00
|
|
|
getattr(old_model._meta, self.option_name, set()),
|
|
|
|
getattr(new_model._meta, self.option_name, set()),
|
2013-07-30 19:34:31 +08:00
|
|
|
)
|
2013-07-02 18:19:02 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2013-10-16 18:09:33 +08:00
|
|
|
|
2015-06-13 06:21:07 +08:00
|
|
|
def references_field(self, model_name, name, app_label=None):
|
|
|
|
return (
|
|
|
|
self.references_model(model_name, app_label) and
|
|
|
|
(
|
|
|
|
not self.unique_together or
|
|
|
|
any((name in together) for together in self.unique_together)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-02 18:19:02 +08:00
|
|
|
def describe(self):
|
2014-07-14 20:16:35 +08:00
|
|
|
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.unique_together or ''))
|
2013-07-03 01:02:01 +08:00
|
|
|
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class AlterIndexTogether(FieldRelatedOptionOperation):
|
2013-07-03 01:02:01 +08:00
|
|
|
"""
|
|
|
|
Changes the value of index_together to the target one.
|
|
|
|
Input value of index_together must be a set of tuples.
|
|
|
|
"""
|
2014-06-25 20:53:09 +08:00
|
|
|
option_name = "index_together"
|
2013-07-03 01:02:01 +08:00
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name, index_together):
|
2014-03-02 03:06:15 +08:00
|
|
|
index_together = normalize_together(index_together)
|
2014-09-10 05:28:45 +08:00
|
|
|
self.index_together = set(tuple(cons) for cons in index_together)
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterIndexTogether, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'index_together': self.index_together,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2013-07-03 01:02:01 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
model_state = state.models[app_label, self.name_lower]
|
2014-06-25 20:53:09 +08:00
|
|
|
model_state.options[self.option_name] = self.index_together
|
2015-01-02 23:37:21 +08:00
|
|
|
state.reload_model(app_label, self.name_lower)
|
2013-07-03 01:02:01 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
new_model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, new_model):
|
2014-11-05 17:43:31 +08:00
|
|
|
old_model = from_state.apps.get_model(app_label, self.name)
|
2013-07-30 19:34:31 +08:00
|
|
|
schema_editor.alter_index_together(
|
|
|
|
new_model,
|
2014-06-25 20:53:09 +08:00
|
|
|
getattr(old_model._meta, self.option_name, set()),
|
|
|
|
getattr(new_model._meta, self.option_name, set()),
|
2013-07-30 19:34:31 +08:00
|
|
|
)
|
2013-07-03 01:02:01 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
return self.database_forwards(app_label, schema_editor, from_state, to_state)
|
|
|
|
|
2013-10-16 18:09:33 +08:00
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2013-10-16 18:09:33 +08:00
|
|
|
|
2015-06-13 06:21:07 +08:00
|
|
|
def references_field(self, model_name, name, app_label=None):
|
|
|
|
return (
|
|
|
|
self.references_model(model_name, app_label) and
|
|
|
|
(
|
|
|
|
not self.index_together or
|
|
|
|
any((name in together) for together in self.index_together)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-07-03 01:02:01 +08:00
|
|
|
def describe(self):
|
2014-07-14 20:16:35 +08:00
|
|
|
return "Alter %s for %s (%s constraint(s))" % (self.option_name, self.name, len(self.index_together or ''))
|
2014-06-16 03:34:02 +08:00
|
|
|
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class AlterOrderWithRespectTo(FieldRelatedOptionOperation):
|
2014-06-16 05:55:44 +08:00
|
|
|
"""
|
|
|
|
Represents a change with the order_with_respect_to option.
|
|
|
|
"""
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name, order_with_respect_to):
|
2014-06-16 05:55:44 +08:00
|
|
|
self.order_with_respect_to = order_with_respect_to
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterOrderWithRespectTo, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'order_with_respect_to': self.order_with_respect_to,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2014-06-16 05:55:44 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
model_state = state.models[app_label, self.name_lower]
|
2014-06-16 05:55:44 +08:00
|
|
|
model_state.options['order_with_respect_to'] = self.order_with_respect_to
|
2015-01-02 23:37:21 +08:00
|
|
|
state.reload_model(app_label, self.name_lower)
|
2014-06-16 05:55:44 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-11-05 17:43:31 +08:00
|
|
|
to_model = to_state.apps.get_model(app_label, self.name)
|
2015-02-19 15:27:58 +08:00
|
|
|
if self.allow_migrate_model(schema_editor.connection.alias, to_model):
|
2014-11-05 17:43:31 +08:00
|
|
|
from_model = from_state.apps.get_model(app_label, self.name)
|
2014-06-16 05:55:44 +08:00
|
|
|
# Remove a field if we need to
|
|
|
|
if from_model._meta.order_with_respect_to and not to_model._meta.order_with_respect_to:
|
2015-01-07 08:16:35 +08:00
|
|
|
schema_editor.remove_field(from_model, from_model._meta.get_field("_order"))
|
2014-06-16 05:55:44 +08:00
|
|
|
# Add a field if we need to (altering the column is untouched as
|
|
|
|
# it's likely a rename)
|
|
|
|
elif to_model._meta.order_with_respect_to and not from_model._meta.order_with_respect_to:
|
2015-01-07 08:16:35 +08:00
|
|
|
field = to_model._meta.get_field("_order")
|
2014-12-16 08:22:28 +08:00
|
|
|
if not field.has_default():
|
|
|
|
field.default = 0
|
2014-06-16 05:55:44 +08:00
|
|
|
schema_editor.add_field(
|
|
|
|
from_model,
|
|
|
|
field,
|
|
|
|
)
|
|
|
|
|
|
|
|
def database_backwards(self, 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):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2014-06-16 05:55:44 +08:00
|
|
|
|
2015-06-13 06:21:07 +08:00
|
|
|
def references_field(self, model_name, name, app_label=None):
|
|
|
|
return (
|
|
|
|
self.references_model(model_name, app_label) and
|
|
|
|
(
|
|
|
|
self.order_with_respect_to is None or
|
|
|
|
name == self.order_with_respect_to
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-06-16 05:55:44 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Set order_with_respect_to on %s to %s" % (self.name, self.order_with_respect_to)
|
|
|
|
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class AlterModelOptions(ModelOptionOperation):
|
2014-06-16 03:34:02 +08:00
|
|
|
"""
|
|
|
|
Sets new model options that don't directly affect the database schema
|
|
|
|
(like verbose_name, permissions, ordering). Python code in migrations
|
|
|
|
may still need them.
|
|
|
|
"""
|
|
|
|
|
2014-07-29 01:47:28 +08:00
|
|
|
# Model options we want to compare and preserve in an AlterModelOptions op
|
|
|
|
ALTER_OPTION_KEYS = [
|
|
|
|
"get_latest_by",
|
2014-12-24 01:27:49 +08:00
|
|
|
"managed",
|
2014-07-29 01:47:28 +08:00
|
|
|
"ordering",
|
|
|
|
"permissions",
|
|
|
|
"default_permissions",
|
|
|
|
"select_on_save",
|
|
|
|
"verbose_name",
|
|
|
|
"verbose_name_plural",
|
|
|
|
]
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, name, options):
|
2014-06-16 03:34:02 +08:00
|
|
|
self.options = options
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterModelOptions, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'name': self.name,
|
|
|
|
'options': self.options,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
2014-11-23 01:57:13 +08:00
|
|
|
[],
|
|
|
|
kwargs
|
2014-11-16 03:25:43 +08:00
|
|
|
)
|
|
|
|
|
2014-06-16 03:34:02 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
model_state = state.models[app_label, self.name_lower]
|
2014-06-16 03:34:02 +08:00
|
|
|
model_state.options = dict(model_state.options)
|
|
|
|
model_state.options.update(self.options)
|
2014-07-29 01:47:28 +08:00
|
|
|
for key in self.ALTER_OPTION_KEYS:
|
|
|
|
if key not in self.options and key in model_state.options:
|
|
|
|
del model_state.options[key]
|
2015-01-02 23:37:21 +08:00
|
|
|
state.reload_model(app_label, self.name_lower)
|
2014-06-16 03:34:02 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2014-06-16 03:34:02 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Change Meta options on %s" % (self.name, )
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
|
2016-01-09 15:36:09 +08:00
|
|
|
class AlterModelManagers(ModelOptionOperation):
|
2014-12-13 06:19:58 +08:00
|
|
|
"""
|
|
|
|
Alters the model's managers
|
|
|
|
"""
|
|
|
|
|
|
|
|
serialization_expand_args = ['managers']
|
|
|
|
|
|
|
|
def __init__(self, name, managers):
|
|
|
|
self.managers = managers
|
2016-01-09 15:36:09 +08:00
|
|
|
super(AlterModelManagers, self).__init__(name)
|
2015-01-02 23:37:21 +08:00
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
|
|
|
[self.name, self.managers],
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
2015-01-02 23:37:21 +08:00
|
|
|
model_state = state.models[app_label, self.name_lower]
|
2014-12-13 06:19:58 +08:00
|
|
|
model_state.managers = list(self.managers)
|
2015-12-14 14:20:10 +08:00
|
|
|
state.reload_model(app_label, self.name_lower)
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def references_model(self, name, app_label=None):
|
2015-01-02 23:37:21 +08:00
|
|
|
return name.lower() == self.name_lower
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Change managers on %s" % (self.name, )
|