You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 lines
4.3 KiB

  1. from __future__ import unicode_literals
  2. from django.db import router
  3. class Operation(object):
  4. """
  5. Base class for migration operations.
  6. It's responsible for both mutating the in-memory model state
  7. (see db/migrations/state.py) to represent what it performs, as well
  8. as actually performing it against a live database.
  9. Note that some operations won't modify memory state at all (e.g. data
  10. copying operations), and some will need their modifications to be
  11. optionally specified by the user (e.g. custom Python code snippets)
  12. Due to the way this class deals with deconstruction, it should be
  13. considered immutable.
  14. """
  15. # If this migration can be run in reverse.
  16. # Some operations are impossible to reverse, like deleting data.
  17. reversible = True
  18. # Can this migration be represented as SQL? (things like RunPython cannot)
  19. reduces_to_sql = True
  20. # Should this operation be forced as atomic even on backends with no
  21. # DDL transaction support (i.e., does it have no DDL, like RunPython)
  22. atomic = False
  23. serialization_expand_args = []
  24. def __new__(cls, *args, **kwargs):
  25. # We capture the arguments to make returning them trivial
  26. self = object.__new__(cls)
  27. self._constructor_args = (args, kwargs)
  28. return self
  29. def deconstruct(self):
  30. """
  31. Returns a 3-tuple of class import path (or just name if it lives
  32. under django.db.migrations), positional arguments, and keyword
  33. arguments.
  34. """
  35. return (
  36. self.__class__.__name__,
  37. self._constructor_args[0],
  38. self._constructor_args[1],
  39. )
  40. def state_forwards(self, app_label, state):
  41. """
  42. Takes the state from the previous migration, and mutates it
  43. so that it matches what this migration would perform.
  44. """
  45. raise NotImplementedError('subclasses of Operation must provide a state_forwards() method')
  46. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  47. """
  48. Performs the mutation on the database schema in the normal
  49. (forwards) direction.
  50. """
  51. raise NotImplementedError('subclasses of Operation must provide a database_forwards() method')
  52. def database_backwards(self, app_label, schema_editor, from_state, to_state):
  53. """
  54. Performs the mutation on the database schema in the reverse
  55. direction - e.g. if this were CreateModel, it would in fact
  56. drop the model's table.
  57. """
  58. raise NotImplementedError('subclasses of Operation must provide a database_backwards() method')
  59. def describe(self):
  60. """
  61. Outputs a brief summary of what the action does.
  62. """
  63. return "%s: %s" % (self.__class__.__name__, self._constructor_args)
  64. def references_model(self, name, app_label=None):
  65. """
  66. Returns True if there is a chance this operation references the given
  67. model name (as a string), with an optional app label for accuracy.
  68. Used for optimization. If in doubt, return True;
  69. returning a false positive will merely make the optimizer a little
  70. less efficient, while returning a false negative may result in an
  71. unusable optimized migration.
  72. """
  73. return True
  74. def references_field(self, model_name, name, app_label=None):
  75. """
  76. Returns True if there is a chance this operation references the given
  77. field name, with an optional app label for accuracy.
  78. Used for optimization. If in doubt, return True.
  79. """
  80. return self.references_model(model_name, app_label)
  81. def allow_migrate_model(self, connection_alias, model):
  82. """
  83. Returns if we're allowed to migrate the model.
  84. This is a thin wrapper around router.allow_migrate_model() that
  85. preemptively rejects any proxy, swapped out, or unmanaged model.
  86. """
  87. if not model._meta.can_migrate(connection_alias):
  88. return False
  89. return router.allow_migrate_model(connection_alias, model)
  90. def __repr__(self):
  91. return "<%s %s%s>" % (
  92. self.__class__.__name__,
  93. ", ".join(map(repr, self._constructor_args[0])),
  94. ",".join(" %s=%r" % x for x in self._constructor_args[1].items()),
  95. )