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.
 
 
 
 

285 lines
11 KiB

  1. from __future__ import unicode_literals
  2. import warnings
  3. from collections import deque
  4. from functools import total_ordering
  5. from django.db.migrations.state import ProjectState
  6. from django.utils.datastructures import OrderedSet
  7. from django.utils.encoding import python_2_unicode_compatible
  8. from .exceptions import CircularDependencyError, NodeNotFoundError
  9. RECURSION_DEPTH_WARNING = (
  10. "Maximum recursion depth exceeded while generating migration graph, "
  11. "falling back to iterative approach. If you're experiencing performance issues, "
  12. "consider squashing migrations as described at "
  13. "https://docs.djangoproject.com/en/dev/topics/migrations/#squashing-migrations."
  14. )
  15. @python_2_unicode_compatible
  16. @total_ordering
  17. class Node(object):
  18. """
  19. A single node in the migration graph. Contains direct links to adjacent
  20. nodes in either direction.
  21. """
  22. def __init__(self, key):
  23. self.key = key
  24. self.children = set()
  25. self.parents = set()
  26. def __eq__(self, other):
  27. return self.key == other
  28. def __lt__(self, other):
  29. return self.key < other
  30. def __hash__(self):
  31. return hash(self.key)
  32. def __getitem__(self, item):
  33. return self.key[item]
  34. def __str__(self):
  35. return str(self.key)
  36. def __repr__(self):
  37. return '<Node: (%r, %r)>' % self.key
  38. def add_child(self, child):
  39. self.children.add(child)
  40. def add_parent(self, parent):
  41. self.parents.add(parent)
  42. # Use manual caching, @cached_property effectively doubles the
  43. # recursion depth for each recursion.
  44. def ancestors(self):
  45. # Use self.key instead of self to speed up the frequent hashing
  46. # when constructing an OrderedSet.
  47. if '_ancestors' not in self.__dict__:
  48. ancestors = deque([self.key])
  49. for parent in sorted(self.parents):
  50. ancestors.extendleft(reversed(parent.ancestors()))
  51. self.__dict__['_ancestors'] = list(OrderedSet(ancestors))
  52. return self.__dict__['_ancestors']
  53. # Use manual caching, @cached_property effectively doubles the
  54. # recursion depth for each recursion.
  55. def descendants(self):
  56. # Use self.key instead of self to speed up the frequent hashing
  57. # when constructing an OrderedSet.
  58. if '_descendants' not in self.__dict__:
  59. descendants = deque([self.key])
  60. for child in sorted(self.children):
  61. descendants.extendleft(reversed(child.descendants()))
  62. self.__dict__['_descendants'] = list(OrderedSet(descendants))
  63. return self.__dict__['_descendants']
  64. @python_2_unicode_compatible
  65. class MigrationGraph(object):
  66. """
  67. Represents the digraph of all migrations in a project.
  68. Each migration is a node, and each dependency is an edge. There are
  69. no implicit dependencies between numbered migrations - the numbering is
  70. merely a convention to aid file listing. Every new numbered migration
  71. has a declared dependency to the previous number, meaning that VCS
  72. branch merges can be detected and resolved.
  73. Migrations files can be marked as replacing another set of migrations -
  74. this is to support the "squash" feature. The graph handler isn't responsible
  75. for these; instead, the code to load them in here should examine the
  76. migration files and if the replaced migrations are all either unapplied
  77. or not present, it should ignore the replaced ones, load in just the
  78. replacing migration, and repoint any dependencies that pointed to the
  79. replaced migrations to point to the replacing one.
  80. A node should be a tuple: (app_path, migration_name). The tree special-cases
  81. things within an app - namely, root nodes and leaf nodes ignore dependencies
  82. to other apps.
  83. """
  84. def __init__(self):
  85. self.node_map = {}
  86. self.nodes = {}
  87. self.cached = False
  88. def add_node(self, key, implementation):
  89. node = Node(key)
  90. self.node_map[key] = node
  91. self.nodes[key] = implementation
  92. self.clear_cache()
  93. def add_dependency(self, migration, child, parent):
  94. if child not in self.nodes:
  95. raise NodeNotFoundError(
  96. "Migration %s dependencies reference nonexistent child node %r" % (migration, child),
  97. child
  98. )
  99. if parent not in self.nodes:
  100. raise NodeNotFoundError(
  101. "Migration %s dependencies reference nonexistent parent node %r" % (migration, parent),
  102. parent
  103. )
  104. self.node_map[child].add_parent(self.node_map[parent])
  105. self.node_map[parent].add_child(self.node_map[child])
  106. self.clear_cache()
  107. def clear_cache(self):
  108. if self.cached:
  109. for node in self.nodes:
  110. self.node_map[node].__dict__.pop('_ancestors', None)
  111. self.node_map[node].__dict__.pop('_descendants', None)
  112. self.cached = False
  113. def forwards_plan(self, target):
  114. """
  115. Given a node, returns a list of which previous nodes (dependencies)
  116. must be applied, ending with the node itself.
  117. This is the list you would follow if applying the migrations to
  118. a database.
  119. """
  120. if target not in self.nodes:
  121. raise NodeNotFoundError("Node %r not a valid node" % (target, ), target)
  122. # Use parent.key instead of parent to speed up the frequent hashing in ensure_not_cyclic
  123. self.ensure_not_cyclic(target, lambda x: (parent.key for parent in self.node_map[x].parents))
  124. self.cached = True
  125. node = self.node_map[target]
  126. try:
  127. return node.ancestors()
  128. except RuntimeError:
  129. # fallback to iterative dfs
  130. warnings.warn(RECURSION_DEPTH_WARNING, RuntimeWarning)
  131. return self.iterative_dfs(node)
  132. def backwards_plan(self, target):
  133. """
  134. Given a node, returns a list of which dependent nodes (dependencies)
  135. must be unapplied, ending with the node itself.
  136. This is the list you would follow if removing the migrations from
  137. a database.
  138. """
  139. if target not in self.nodes:
  140. raise NodeNotFoundError("Node %r not a valid node" % (target, ), target)
  141. # Use child.key instead of child to speed up the frequent hashing in ensure_not_cyclic
  142. self.ensure_not_cyclic(target, lambda x: (child.key for child in self.node_map[x].children))
  143. self.cached = True
  144. node = self.node_map[target]
  145. try:
  146. return node.descendants()
  147. except RuntimeError:
  148. # fallback to iterative dfs
  149. warnings.warn(RECURSION_DEPTH_WARNING, RuntimeWarning)
  150. return self.iterative_dfs(node, forwards=False)
  151. def iterative_dfs(self, start, forwards=True):
  152. """
  153. Iterative depth first search, for finding dependencies.
  154. """
  155. visited = deque()
  156. visited.append(start)
  157. if forwards:
  158. stack = deque(sorted(start.parents))
  159. else:
  160. stack = deque(sorted(start.children))
  161. while stack:
  162. node = stack.popleft()
  163. visited.appendleft(node)
  164. if forwards:
  165. children = sorted(node.parents, reverse=True)
  166. else:
  167. children = sorted(node.children, reverse=True)
  168. # reverse sorting is needed because prepending using deque.extendleft
  169. # also effectively reverses values
  170. stack.extendleft(children)
  171. return list(OrderedSet(visited))
  172. def root_nodes(self, app=None):
  173. """
  174. Returns all root nodes - that is, nodes with no dependencies inside
  175. their app. These are the starting point for an app.
  176. """
  177. roots = set()
  178. for node in self.nodes:
  179. if (not any(key[0] == node[0] for key in self.node_map[node].parents)
  180. and (not app or app == node[0])):
  181. roots.add(node)
  182. return sorted(roots)
  183. def leaf_nodes(self, app=None):
  184. """
  185. Returns all leaf nodes - that is, nodes with no dependents in their app.
  186. These are the "most current" version of an app's schema.
  187. Having more than one per app is technically an error, but one that
  188. gets handled further up, in the interactive command - it's usually the
  189. result of a VCS merge and needs some user input.
  190. """
  191. leaves = set()
  192. for node in self.nodes:
  193. if (not any(key[0] == node[0] for key in self.node_map[node].children)
  194. and (not app or app == node[0])):
  195. leaves.add(node)
  196. return sorted(leaves)
  197. def ensure_not_cyclic(self, start, get_children):
  198. # Algo from GvR:
  199. # http://neopythonic.blogspot.co.uk/2009/01/detecting-cycles-in-directed-graph.html
  200. todo = set(self.nodes)
  201. while todo:
  202. node = todo.pop()
  203. stack = [node]
  204. while stack:
  205. top = stack[-1]
  206. for node in get_children(top):
  207. if node in stack:
  208. cycle = stack[stack.index(node):]
  209. raise CircularDependencyError(", ".join("%s.%s" % n for n in cycle))
  210. if node in todo:
  211. stack.append(node)
  212. todo.remove(node)
  213. break
  214. else:
  215. node = stack.pop()
  216. def __str__(self):
  217. return 'Graph: %s nodes, %s edges' % self._nodes_and_edges()
  218. def __repr__(self):
  219. nodes, edges = self._nodes_and_edges()
  220. return '<%s: nodes=%s, edges=%s>' % (self.__class__.__name__, nodes, edges)
  221. def _nodes_and_edges(self):
  222. return len(self.nodes), sum(len(node.parents) for node in self.node_map.values())
  223. def make_state(self, nodes=None, at_end=True, real_apps=None):
  224. """
  225. Given a migration node or nodes, returns a complete ProjectState for it.
  226. If at_end is False, returns the state before the migration has run.
  227. If nodes is not provided, returns the overall most current project state.
  228. """
  229. if nodes is None:
  230. nodes = list(self.leaf_nodes())
  231. if len(nodes) == 0:
  232. return ProjectState()
  233. if not isinstance(nodes[0], tuple):
  234. nodes = [nodes]
  235. plan = []
  236. for node in nodes:
  237. for migration in self.forwards_plan(node):
  238. if migration not in plan:
  239. if not at_end and migration in nodes:
  240. continue
  241. plan.append(migration)
  242. project_state = ProjectState(real_apps=real_apps)
  243. for node in plan:
  244. project_state = self.nodes[node].mutate_state(project_state, preserve=False)
  245. return project_state
  246. def __contains__(self, node):
  247. return node in self.nodes