2014-06-18 08:45:38 +08:00
|
|
|
from django.db import router
|
2014-05-06 01:50:51 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Operation:
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
Base class for migration operations.
|
|
|
|
|
|
|
|
It's responsible for both mutating the in-memory model state
|
|
|
|
(see db/migrations/state.py) to represent what it performs, as well
|
|
|
|
as actually performing it against a live database.
|
|
|
|
|
|
|
|
Note that some operations won't modify memory state at all (e.g. data
|
|
|
|
copying operations), and some will need their modifications to be
|
|
|
|
optionally specified by the user (e.g. custom Python code snippets)
|
2013-11-06 21:47:58 +08:00
|
|
|
|
2013-11-07 12:00:48 +08:00
|
|
|
Due to the way this class deals with deconstruction, it should be
|
2013-11-06 21:47:58 +08:00
|
|
|
considered immutable.
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
# If this migration can be run in reverse.
|
|
|
|
# Some operations are impossible to reverse, like deleting data.
|
|
|
|
reversible = True
|
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
# Can this migration be represented as SQL? (things like RunPython cannot)
|
|
|
|
reduces_to_sql = True
|
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
# Should this operation be forced as atomic even on backends with no
|
|
|
|
# DDL transaction support (i.e., does it have no DDL, like RunPython)
|
|
|
|
atomic = False
|
|
|
|
|
2016-01-09 16:12:46 +08:00
|
|
|
# Should this operation be considered safe to elide and optimize across?
|
|
|
|
elidable = False
|
|
|
|
|
2013-09-26 15:25:35 +08:00
|
|
|
serialization_expand_args = []
|
|
|
|
|
2013-06-07 22:28:38 +08:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
# We capture the arguments to make returning them trivial
|
|
|
|
self = object.__new__(cls)
|
|
|
|
self._constructor_args = (args, kwargs)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def deconstruct(self):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return a 3-tuple of class import path (or just name if it lives
|
2013-06-07 22:28:38 +08:00
|
|
|
under django.db.migrations), positional arguments, and keyword
|
|
|
|
arguments.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
self.__class__.__name__,
|
|
|
|
self._constructor_args[0],
|
|
|
|
self._constructor_args[1],
|
|
|
|
)
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def state_forwards(self, app_label, state):
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Take the state from the previous migration, and mutate it
|
2013-05-11 00:07:13 +08:00
|
|
|
so that it matches what this migration would perform.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Operation must provide a state_forwards() method')
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def database_forwards(self, app_label, schema_editor, from_state, to_state):
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Perform the mutation on the database schema in the normal
|
2013-05-11 00:07:13 +08:00
|
|
|
(forwards) direction.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Operation must provide a database_forwards() method')
|
2013-05-11 00:07:13 +08:00
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def database_backwards(self, app_label, schema_editor, from_state, to_state):
|
2013-05-11 00:07:13 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Perform the mutation on the database schema in the reverse
|
2013-05-11 00:07:13 +08:00
|
|
|
direction - e.g. if this were CreateModel, it would in fact
|
|
|
|
drop the model's table.
|
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of Operation must provide a database_backwards() method')
|
2013-06-19 23:23:52 +08:00
|
|
|
|
|
|
|
def describe(self):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Output a brief summary of what the action does.
|
2013-06-19 23:23:52 +08:00
|
|
|
"""
|
|
|
|
return "%s: %s" % (self.__class__.__name__, self._constructor_args)
|
2013-10-03 00:33:41 +08:00
|
|
|
|
2020-04-04 11:05:16 +08:00
|
|
|
def references_model(self, name, app_label):
|
2013-10-16 18:09:33 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return True if there is a chance this operation references the given
|
2020-04-04 11:05:16 +08:00
|
|
|
model name (as a string), with an app label for accuracy.
|
2013-10-16 18:09:33 +08:00
|
|
|
|
|
|
|
Used for optimization. If in doubt, return True;
|
|
|
|
returning a false positive will merely make the optimizer a little
|
|
|
|
less efficient, while returning a false negative may result in an
|
|
|
|
unusable optimized migration.
|
|
|
|
"""
|
|
|
|
return True
|
|
|
|
|
2020-04-04 11:05:16 +08:00
|
|
|
def references_field(self, model_name, name, app_label):
|
2013-11-06 21:47:58 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Return True if there is a chance this operation references the given
|
2020-04-04 11:05:16 +08:00
|
|
|
field name, with an app label for accuracy.
|
2013-11-06 21:47:58 +08:00
|
|
|
|
|
|
|
Used for optimization. If in doubt, return True.
|
|
|
|
"""
|
|
|
|
return self.references_model(model_name, app_label)
|
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
def allow_migrate_model(self, connection_alias, model):
|
2014-06-18 08:45:38 +08:00
|
|
|
"""
|
2018-08-02 00:55:53 +08:00
|
|
|
Return whether or not a model may be migrated.
|
2015-02-19 15:27:58 +08:00
|
|
|
|
|
|
|
This is a thin wrapper around router.allow_migrate_model() that
|
|
|
|
preemptively rejects any proxy, swapped out, or unmanaged model.
|
2014-06-18 08:45:38 +08:00
|
|
|
"""
|
2015-04-05 00:07:46 +08:00
|
|
|
if not model._meta.can_migrate(connection_alias):
|
2015-01-09 01:10:10 +08:00
|
|
|
return False
|
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
return router.allow_migrate_model(connection_alias, model)
|
2014-06-18 08:45:38 +08:00
|
|
|
|
2020-04-04 10:57:42 +08:00
|
|
|
def reduce(self, operation, app_label):
|
2016-01-09 15:36:09 +08:00
|
|
|
"""
|
|
|
|
Return either a list of operations the actual operation should be
|
|
|
|
replaced with or a boolean that indicates whether or not the specified
|
|
|
|
operation can be optimized across.
|
|
|
|
"""
|
2016-01-09 16:12:46 +08:00
|
|
|
if self.elidable:
|
|
|
|
return [operation]
|
|
|
|
elif operation.elidable:
|
|
|
|
return [self]
|
2016-01-09 15:36:09 +08:00
|
|
|
return False
|
|
|
|
|
2013-10-03 00:33:41 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<%s %s%s>" % (
|
|
|
|
self.__class__.__name__,
|
|
|
|
", ".join(map(repr, self._constructor_args[0])),
|
|
|
|
",".join(" %s=%r" % x for x in self._constructor_args[1].items()),
|
|
|
|
)
|