special.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from __future__ import unicode_literals
  2. from .base import Operation
  3. class SeparateDatabaseAndState(Operation):
  4. """
  5. Takes two lists of operations - ones that will be used for the database,
  6. and ones that will be used for the state change. This allows operations
  7. that don't support state change to have it applied, or have operations
  8. that affect the state or not the database, or so on.
  9. """
  10. def __init__(self, database_operations=None, state_operations=None):
  11. self.database_operations = database_operations or []
  12. self.state_operations = state_operations or []
  13. def state_forwards(self, app_label, state):
  14. for state_operation in self.state_operations:
  15. state_operation.state_forwards(app_label, state)
  16. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  17. # We calculate state separately in here since our state functions aren't useful
  18. for database_operation in self.database_operations:
  19. to_state = from_state.clone()
  20. database_operation.state_forwards(app_label, to_state)
  21. database_operation.database_forwards(app_label, schema_editor, from_state, to_state)
  22. from_state = to_state
  23. def database_backwards(self, app_label, schema_editor, from_state, to_state):
  24. # We calculate state separately in here since our state functions aren't useful
  25. base_state = to_state
  26. for pos, database_operation in enumerate(reversed(self.database_operations)):
  27. to_state = base_state.clone()
  28. for dbop in self.database_operations[:-(pos + 1)]:
  29. dbop.state_forwards(app_label, to_state)
  30. from_state = base_state.clone()
  31. database_operation.state_forwards(app_label, from_state)
  32. database_operation.database_backwards(app_label, schema_editor, from_state, to_state)
  33. def describe(self):
  34. return "Custom state/database change combination"
  35. class RunSQL(Operation):
  36. """
  37. Runs some raw SQL. A reverse SQL statement may be provided.
  38. Also accepts a list of operations that represent the state change effected
  39. by this SQL change, in case it's custom column/table creation/deletion.
  40. """
  41. def __init__(self, sql, reverse_sql=None, state_operations=None):
  42. self.sql = sql
  43. self.reverse_sql = reverse_sql
  44. self.state_operations = state_operations or []
  45. @property
  46. def reversible(self):
  47. return self.reverse_sql is not None
  48. def state_forwards(self, app_label, state):
  49. for state_operation in self.state_operations:
  50. state_operation.state_forwards(app_label, state)
  51. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  52. statements = schema_editor.connection.ops.prepare_sql_script(self.sql)
  53. for statement in statements:
  54. schema_editor.execute(statement)
  55. def database_backwards(self, app_label, schema_editor, from_state, to_state):
  56. if self.reverse_sql is None:
  57. raise NotImplementedError("You cannot reverse this operation")
  58. statements = schema_editor.connection.ops.prepare_sql_script(self.reverse_sql)
  59. for statement in statements:
  60. schema_editor.execute(statement)
  61. def describe(self):
  62. return "Raw SQL operation"
  63. class RunPython(Operation):
  64. """
  65. Runs Python code in a context suitable for doing versioned ORM operations.
  66. """
  67. reduces_to_sql = False
  68. def __init__(self, code, reverse_code=None, atomic=True):
  69. self.atomic = atomic
  70. # Forwards code
  71. if not callable(code):
  72. raise ValueError("RunPython must be supplied with a callable")
  73. self.code = code
  74. # Reverse code
  75. if reverse_code is None:
  76. self.reverse_code = None
  77. else:
  78. if not callable(reverse_code):
  79. raise ValueError("RunPython must be supplied with callable arguments")
  80. self.reverse_code = reverse_code
  81. @property
  82. def reversible(self):
  83. return self.reverse_code is not None
  84. def state_forwards(self, app_label, state):
  85. # RunPython objects have no state effect. To add some, combine this
  86. # with SeparateDatabaseAndState.
  87. pass
  88. def database_forwards(self, app_label, schema_editor, from_state, to_state):
  89. # We now execute the Python code in a context that contains a 'models'
  90. # object, representing the versioned models as an app registry.
  91. # We could try to override the global cache, but then people will still
  92. # use direct imports, so we go with a documentation approach instead.
  93. self.code(from_state.render(), schema_editor)
  94. def database_backwards(self, app_label, schema_editor, from_state, to_state):
  95. if self.reverse_code is None:
  96. raise NotImplementedError("You cannot reverse this operation")
  97. self.reverse_code(from_state.render(), schema_editor)
  98. def describe(self):
  99. return "Raw Python operation"