2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-09-08 00:03:23 +08:00
|
|
|
from .base import Operation
|
|
|
|
|
|
|
|
|
|
|
|
class SeparateDatabaseAndState(Operation):
|
|
|
|
"""
|
|
|
|
Takes two lists of operations - ones that will be used for the database,
|
|
|
|
and ones that will be used for the state change. This allows operations
|
|
|
|
that don't support state change to have it applied, or have operations
|
|
|
|
that affect the state or not the database, or so on.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, database_operations=None, state_operations=None):
|
|
|
|
self.database_operations = database_operations or []
|
|
|
|
self.state_operations = state_operations or []
|
|
|
|
|
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
for state_operation in self.state_operations:
|
|
|
|
state_operation.state_forwards(app_label, state)
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
# We calculate state separately in here since our state functions aren't useful
|
|
|
|
for database_operation in self.database_operations:
|
|
|
|
to_state = from_state.clone()
|
|
|
|
database_operation.state_forwards(app_label, to_state)
|
|
|
|
database_operation.database_forwards(self, app_label, schema_editor, from_state, to_state)
|
|
|
|
from_state = to_state
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
# We calculate state separately in here since our state functions aren't useful
|
|
|
|
base_state = to_state
|
|
|
|
for pos, database_operation in enumerate(reversed(self.database_operations)):
|
|
|
|
to_state = base_state.clone()
|
2013-11-04 02:08:55 +08:00
|
|
|
for dbop in self.database_operations[:-(pos + 1)]:
|
2013-09-08 00:03:23 +08:00
|
|
|
dbop.state_forwards(app_label, to_state)
|
|
|
|
from_state = base_state.clone()
|
|
|
|
database_operation.state_forwards(app_label, from_state)
|
|
|
|
database_operation.database_backwards(self, app_label, schema_editor, from_state, to_state)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Custom state/database change combination"
|
|
|
|
|
|
|
|
|
|
|
|
class RunSQL(Operation):
|
|
|
|
"""
|
2014-04-26 16:22:48 +08:00
|
|
|
Runs some raw SQL. A reverse SQL statement may be provided.
|
2013-09-08 00:03:23 +08:00
|
|
|
|
|
|
|
Also accepts a list of operations that represent the state change effected
|
|
|
|
by this SQL change, in case it's custom column/table creation/deletion.
|
|
|
|
"""
|
|
|
|
|
2014-04-26 16:22:48 +08:00
|
|
|
def __init__(self, sql, reverse_sql=None, state_operations=None):
|
2013-09-08 00:03:23 +08:00
|
|
|
self.sql = sql
|
|
|
|
self.reverse_sql = reverse_sql
|
|
|
|
self.state_operations = state_operations or []
|
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
@property
|
|
|
|
def reversible(self):
|
|
|
|
return self.reverse_sql is not None
|
|
|
|
|
2013-09-08 00:03:23 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
for state_operation in self.state_operations:
|
|
|
|
state_operation.state_forwards(app_label, state)
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2014-04-26 16:22:48 +08:00
|
|
|
statements = schema_editor.connection.ops.prepare_sql_script(self.sql)
|
2013-09-08 00:03:23 +08:00
|
|
|
for statement in statements:
|
|
|
|
schema_editor.execute(statement)
|
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
if self.reverse_sql is None:
|
|
|
|
raise NotImplementedError("You cannot reverse this operation")
|
2014-04-26 16:22:48 +08:00
|
|
|
statements = schema_editor.connection.ops.prepare_sql_script(self.reverse_sql)
|
2013-09-08 00:03:23 +08:00
|
|
|
for statement in statements:
|
|
|
|
schema_editor.execute(statement)
|
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Raw SQL operation"
|
2013-09-25 20:58:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
class RunPython(Operation):
|
|
|
|
"""
|
|
|
|
Runs Python code in a context suitable for doing versioned ORM operations.
|
|
|
|
"""
|
|
|
|
|
|
|
|
reduces_to_sql = False
|
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
def __init__(self, code, reverse_code=None, atomic=True):
|
|
|
|
self.atomic = atomic
|
2013-09-26 00:20:06 +08:00
|
|
|
# Forwards code
|
2014-02-09 18:54:02 +08:00
|
|
|
if not callable(code):
|
|
|
|
raise ValueError("RunPython must be supplied with a callable")
|
|
|
|
self.code = code
|
2013-09-26 00:20:06 +08:00
|
|
|
# Reverse code
|
|
|
|
if reverse_code is None:
|
|
|
|
self.reverse_code = None
|
|
|
|
else:
|
2014-02-09 18:54:02 +08:00
|
|
|
if not callable(reverse_code):
|
|
|
|
raise ValueError("RunPython must be supplied with callable arguments")
|
2013-09-26 00:20:06 +08:00
|
|
|
self.reverse_code = reverse_code
|
2013-09-25 20:58:07 +08:00
|
|
|
|
2014-02-20 07:44:57 +08:00
|
|
|
@property
|
|
|
|
def reversible(self):
|
|
|
|
return self.reverse_code is not None
|
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
|
|
|
# RunPython objects have no state effect. To add some, combine this
|
|
|
|
# with SeparateDatabaseAndState.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
|
|
|
# We now execute the Python code in a context that contains a 'models'
|
2013-12-24 19:25:17 +08:00
|
|
|
# object, representing the versioned models as an app registry.
|
2013-09-25 20:58:07 +08:00
|
|
|
# We could try to override the global cache, but then people will still
|
|
|
|
# use direct imports, so we go with a documentation approach instead.
|
2014-03-01 01:22:15 +08:00
|
|
|
self.code(from_state.render(), schema_editor)
|
2013-09-25 20:58:07 +08:00
|
|
|
|
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2013-09-26 00:20:06 +08:00
|
|
|
if self.reverse_code is None:
|
|
|
|
raise NotImplementedError("You cannot reverse this operation")
|
2014-03-01 01:22:15 +08:00
|
|
|
self.reverse_code(from_state.render(), schema_editor)
|
2013-09-25 21:37:44 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Raw Python operation"
|