2014-05-06 01:50:51 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
from django.db import router
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
|
2015-02-07 08:52:26 +08:00
|
|
|
serialization_expand_args = ['database_operations', 'state_operations']
|
|
|
|
|
2014-11-23 13:29:23 +08:00
|
|
|
def __init__(self, database_operations=None, state_operations=None):
|
2013-09-08 00:03:23 +08:00
|
|
|
self.database_operations = database_operations or []
|
|
|
|
self.state_operations = state_operations or []
|
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
|
|
|
kwargs = {}
|
|
|
|
if self.database_operations:
|
|
|
|
kwargs['database_operations'] = self.database_operations
|
|
|
|
if self.state_operations:
|
|
|
|
kwargs['state_operations'] = self.state_operations
|
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
|
|
|
[],
|
|
|
|
kwargs
|
|
|
|
)
|
|
|
|
|
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):
|
|
|
|
# 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)
|
2014-09-01 17:30:00 +08:00
|
|
|
database_operation.database_forwards(app_label, schema_editor, from_state, to_state)
|
2013-09-08 00:03:23 +08:00
|
|
|
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
|
2015-12-08 23:18:00 +08:00
|
|
|
to_states = {}
|
|
|
|
for dbop in self.database_operations:
|
|
|
|
to_states[dbop] = to_state
|
|
|
|
to_state = to_state.clone()
|
|
|
|
dbop.state_forwards(app_label, to_state)
|
|
|
|
# to_state now has the states of all the database_operations applied
|
|
|
|
# which is the from_state for the backwards migration of the last
|
|
|
|
# operation.
|
|
|
|
for database_operation in reversed(self.database_operations):
|
|
|
|
from_state = to_state
|
|
|
|
to_state = to_states[database_operation]
|
2014-09-01 17:30:00 +08:00
|
|
|
database_operation.database_backwards(app_label, schema_editor, from_state, to_state)
|
2013-09-08 00:03:23 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2015-01-09 01:00:04 +08:00
|
|
|
noop = ''
|
2013-09-08 00:03:23 +08:00
|
|
|
|
2016-01-09 16:12:46 +08:00
|
|
|
def __init__(self, sql, reverse_sql=None, state_operations=None, hints=None, elidable=False):
|
2013-09-08 00:03:23 +08:00
|
|
|
self.sql = sql
|
|
|
|
self.reverse_sql = reverse_sql
|
|
|
|
self.state_operations = state_operations or []
|
2015-01-09 01:10:10 +08:00
|
|
|
self.hints = hints or {}
|
2016-01-09 16:12:46 +08:00
|
|
|
self.elidable = elidable
|
2013-09-08 00:03:23 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'sql': self.sql,
|
|
|
|
}
|
2014-11-16 03:25:43 +08:00
|
|
|
if self.reverse_sql is not None:
|
|
|
|
kwargs['reverse_sql'] = self.reverse_sql
|
|
|
|
if self.state_operations:
|
|
|
|
kwargs['state_operations'] = self.state_operations
|
2015-01-09 01:10:10 +08:00
|
|
|
if self.hints:
|
|
|
|
kwargs['hints'] = self.hints
|
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-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):
|
2015-02-19 15:27:58 +08:00
|
|
|
if router.allow_migrate(schema_editor.connection.alias, app_label, **self.hints):
|
2015-01-09 01:10:10 +08:00
|
|
|
self._run_sql(schema_editor, self.sql)
|
2013-09-08 00:03:23 +08:00
|
|
|
|
|
|
|
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")
|
2015-02-19 15:27:58 +08:00
|
|
|
if router.allow_migrate(schema_editor.connection.alias, app_label, **self.hints):
|
2015-01-09 01:10:10 +08:00
|
|
|
self._run_sql(schema_editor, self.reverse_sql)
|
2013-09-08 00:03:23 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Raw SQL operation"
|
2013-09-25 20:58:07 +08:00
|
|
|
|
2015-01-09 01:00:04 +08:00
|
|
|
def _run_sql(self, schema_editor, sqls):
|
|
|
|
if isinstance(sqls, (list, tuple)):
|
|
|
|
for sql in sqls:
|
2014-09-16 08:25:02 +08:00
|
|
|
params = None
|
|
|
|
if isinstance(sql, (list, tuple)):
|
|
|
|
elements = len(sql)
|
|
|
|
if elements == 2:
|
|
|
|
sql, params = sql
|
|
|
|
else:
|
|
|
|
raise ValueError("Expected a 2-tuple but got %d" % elements)
|
|
|
|
schema_editor.execute(sql, params=params)
|
2015-01-09 01:00:04 +08:00
|
|
|
elif sqls != RunSQL.noop:
|
|
|
|
statements = schema_editor.connection.ops.prepare_sql_script(sqls)
|
2014-09-16 08:25:02 +08:00
|
|
|
for statement in statements:
|
|
|
|
schema_editor.execute(statement, params=None)
|
|
|
|
|
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
|
|
|
|
|
2016-01-31 04:46:28 +08:00
|
|
|
def __init__(self, code, reverse_code=None, atomic=None, hints=None, elidable=False):
|
2014-05-08 05:28:34 +08:00
|
|
|
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
|
2015-01-09 01:10:10 +08:00
|
|
|
self.hints = hints or {}
|
2016-01-09 16:12:46 +08:00
|
|
|
self.elidable = elidable
|
2013-09-25 20:58:07 +08:00
|
|
|
|
2014-11-16 03:25:43 +08:00
|
|
|
def deconstruct(self):
|
2014-11-23 01:57:13 +08:00
|
|
|
kwargs = {
|
|
|
|
'code': self.code,
|
|
|
|
}
|
|
|
|
if self.reverse_code is not None:
|
2014-11-16 03:25:43 +08:00
|
|
|
kwargs['reverse_code'] = self.reverse_code
|
2016-01-31 04:46:28 +08:00
|
|
|
if self.atomic is not None:
|
2014-11-16 03:25:43 +08:00
|
|
|
kwargs['atomic'] = self.atomic
|
2015-01-09 01:10:10 +08:00
|
|
|
if self.hints:
|
|
|
|
kwargs['hints'] = self.hints
|
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
|
|
|
|
)
|
|
|
|
|
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):
|
2015-02-19 15:27:58 +08:00
|
|
|
if router.allow_migrate(schema_editor.connection.alias, app_label, **self.hints):
|
2015-01-09 01:10:10 +08:00
|
|
|
# We now execute the Python code in a context that contains a 'models'
|
|
|
|
# object, representing the versioned models as an app registry.
|
|
|
|
# We could try to override the global cache, but then people will still
|
|
|
|
# use direct imports, so we go with a documentation approach instead.
|
|
|
|
self.code(from_state.apps, 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")
|
2015-02-19 15:27:58 +08:00
|
|
|
if router.allow_migrate(schema_editor.connection.alias, app_label, **self.hints):
|
2015-01-09 01:10:10 +08:00
|
|
|
self.reverse_code(from_state.apps, schema_editor)
|
2013-09-25 21:37:44 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
return "Raw Python operation"
|
2015-01-09 01:00:04 +08:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def noop(apps, schema_editor):
|
|
|
|
return None
|