2013-05-11 00:07:13 +08:00
|
|
|
from .base import Operation
|
2013-05-30 00:47:10 +08:00
|
|
|
from django.db import models
|
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-05-11 00:07:13 +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
|
|
|
|
|
|
|
def database_forwards(self, app, schema_editor, from_state, to_state):
|
|
|
|
app_cache = to_state.render()
|
|
|
|
schema_editor.create_model(app_cache.get_model(app, self.name))
|
|
|
|
|
|
|
|
def database_backwards(self, app, schema_editor, from_state, to_state):
|
2013-05-30 00:47:10 +08:00
|
|
|
app_cache = from_state.render()
|
|
|
|
schema_editor.delete_model(app_cache.get_model(app, self.name))
|
|
|
|
|
2013-06-19 23:23:52 +08:00
|
|
|
def describe(self):
|
|
|
|
return "Create model %s" % (self.name, )
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
|
|
|
|
class DeleteModel(Operation):
|
|
|
|
"""
|
|
|
|
Drops a model's table.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
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()
|
|
|
|
schema_editor.delete_model(app_cache.get_model(app_label, self.name))
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
app_cache = to_state.render()
|
|
|
|
schema_editor.create_model(app_cache.get_model(app_label, self.name))
|
2013-06-19 23:23:52 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Delete model %s" % (self.name, )
|