2013-05-11 00:07:13 +08:00
|
|
|
from .base import Operation
|
2013-07-30 19:34:31 +08:00
|
|
|
from django.db import models, router
|
2013-05-11 00:07:13 +08:00
|
|
|
from django.db.migrations.state import ModelState
|
|
|
|
|
|
|
|
|
|
|
|
class CreateModel(Operation):
|
|
|
|
"""
|
|
|
|
Create a model's table.
|
|
|
|
"""
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def __init__(self, name, fields, options=None, bases=None):
|
2013-07-25 23:31:34 +08:00
|
|
|
self.name = name
|
2013-05-30 00:47:10 +08:00
|
|
|
self.fields = fields
|
|
|
|
self.options = options or {}
|
|
|
|
self.bases = bases or (models.Model,)
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
state.models[app_label, self.name.lower()] = ModelState(app_label, self.name, self.fields, self.options, self.bases)
|
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):
|
2013-05-11 00:07:13 +08:00
|
|
|
app_cache = to_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
model = app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, model):
|
|
|
|
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):
|
2013-05-30 00:47:10 +08:00
|
|
|
app_cache = from_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
model = app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, model):
|
|
|
|
schema_editor.delete_model(model)
|
2013-05-30 00:47:10 +08:00
|
|
|
|
2013-06-19 23:23:52 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Create model %s" % (self.name, )
|
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
def __eq__(self, other):
|
|
|
|
return (
|
|
|
|
(self.__class__ == other.__class__) and
|
|
|
|
(self.name == other.name) and
|
|
|
|
(self.options == other.options) and
|
|
|
|
(self.bases == other.bases) and
|
|
|
|
([(k, f.deconstruct()[1:]) for k, f in self.fields] == [(k, f.deconstruct()[1:]) for k, f in other.fields])
|
|
|
|
)
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
class DeleteModel(Operation):
|
|
|
|
"""
|
|
|
|
Drops a model's table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
2013-07-25 23:31:34 +08:00
|
|
|
self.name = name
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
del state.models[app_label, self.name.lower()]
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
app_cache = from_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
model = app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, model):
|
|
|
|
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):
|
|
|
|
app_cache = to_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
model = app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, model):
|
|
|
|
schema_editor.create_model(model)
|
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
|
|
|
|
|
|
|
|
|
|
|
class AlterModelTable(Operation):
|
|
|
|
"""
|
|
|
|
Renames a model's table
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, table):
|
2013-07-25 23:31:34 +08:00
|
|
|
self.name = name
|
2013-06-20 21:54:11 +08:00
|
|
|
self.table = table
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
state.models[app_label, self.name.lower()].options["db_table"] = self.table
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
old_app_cache = from_state.render()
|
|
|
|
new_app_cache = to_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
old_model = old_app_cache.get_model(app_label, self.name)
|
|
|
|
new_model = new_app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, new_model):
|
|
|
|
schema_editor.alter_db_table(
|
|
|
|
new_model,
|
|
|
|
old_model._meta.db_table,
|
|
|
|
new_model._meta.db_table,
|
|
|
|
)
|
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)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Rename table for %s to %s" % (self.name, self.table)
|
2013-07-02 18:19:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AlterUniqueTogether(Operation):
|
|
|
|
"""
|
2013-07-03 01:02:01 +08:00
|
|
|
Changes the value of index_together to the target one.
|
2013-07-02 18:19:02 +08:00
|
|
|
Input value of unique_together must be a set of tuples.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, unique_together):
|
2013-07-25 23:31:34 +08:00
|
|
|
self.name = name
|
2013-07-02 18:19:02 +08:00
|
|
|
self.unique_together = set(tuple(cons) for cons in unique_together)
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
model_state = state.models[app_label, self.name.lower()]
|
|
|
|
model_state.options["unique_together"] = self.unique_together
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
old_app_cache = from_state.render()
|
|
|
|
new_app_cache = to_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
old_model = old_app_cache.get_model(app_label, self.name)
|
|
|
|
new_model = new_app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, new_model):
|
|
|
|
schema_editor.alter_unique_together(
|
|
|
|
new_model,
|
|
|
|
getattr(old_model._meta, "unique_together", set()),
|
|
|
|
getattr(new_model._meta, "unique_together", set()),
|
|
|
|
)
|
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)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Alter unique_together for %s (%s constraints)" % (self.name, len(self.unique_together))
|
2013-07-03 01:02:01 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AlterIndexTogether(Operation):
|
|
|
|
"""
|
|
|
|
Changes the value of index_together to the target one.
|
|
|
|
Input value of index_together must be a set of tuples.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name, index_together):
|
2013-07-25 23:31:34 +08:00
|
|
|
self.name = name
|
2013-07-03 01:02:01 +08:00
|
|
|
self.index_together = set(tuple(cons) for cons in index_together)
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
model_state = state.models[app_label, self.name.lower()]
|
|
|
|
model_state.options["index_together"] = self.index_together
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
old_app_cache = from_state.render()
|
|
|
|
new_app_cache = to_state.render()
|
2013-07-30 19:34:31 +08:00
|
|
|
old_model = old_app_cache.get_model(app_label, self.name)
|
|
|
|
new_model = new_app_cache.get_model(app_label, self.name)
|
|
|
|
if router.allow_migrate(schema_editor.connection.alias, new_model):
|
|
|
|
schema_editor.alter_index_together(
|
|
|
|
new_model,
|
|
|
|
getattr(old_model._meta, "index_together", set()),
|
|
|
|
getattr(new_model._meta, "index_together", set()),
|
|
|
|
)
|
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)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Alter index_together for %s (%s constraints)" % (self.name, len(self.index_together))
|