1
from __future__ import unicode_literals
3
from django.utils.datastructures import OrderedSet
4
from django.db.migrations.state import ProjectState
7
class MigrationGraph(object):
9
Represents the digraph of all migrations in a project.
11
Each migration is a node, and each dependency is an edge. There are
12
no implicit dependencies between numbered migrations - the numbering is
13
merely a convention to aid file listing. Every new numbered migration
14
has a declared dependency to the previous number, meaning that VCS
15
branch merges can be detected and resolved.
17
Migrations files can be marked as replacing another set of migrations -
18
this is to support the "squash" feature. The graph handler isn't responsible
19
for these; instead, the code to load them in here should examine the
20
migration files and if the replaced migrations are all either unapplied
21
or not present, it should ignore the replaced ones, load in just the
22
replacing migration, and repoint any dependencies that pointed to the
23
replaced migrations to point to the replacing one.
25
A node should be a tuple: (app_path, migration_name). The tree special-cases
26
things within an app - namely, root nodes and leaf nodes ignore dependencies
32
self.dependencies = {}
35
def add_node(self, node, implementation):
36
self.nodes[node] = implementation
38
def add_dependency(self, migration, child, parent):
39
if child not in self.nodes:
40
raise KeyError("Migration %s dependencies references nonexistent child node %r" % (migration, child))
41
if parent not in self.nodes:
42
raise KeyError("Migration %s dependencies references nonexistent parent node %r" % (migration, parent))
43
self.dependencies.setdefault(child, set()).add(parent)
44
self.dependents.setdefault(parent, set()).add(child)
46
def forwards_plan(self, node):
48
Given a node, returns a list of which previous nodes (dependencies)
49
must be applied, ending with the node itself.
50
This is the list you would follow if applying the migrations to
53
if node not in self.nodes:
54
raise ValueError("Node %r not a valid node" % (node, ))
55
return self.dfs(node, lambda x: self.dependencies.get(x, set()))
57
def backwards_plan(self, node):
59
Given a node, returns a list of which dependent nodes (dependencies)
60
must be unapplied, ending with the node itself.
61
This is the list you would follow if removing the migrations from
64
if node not in self.nodes:
65
raise ValueError("Node %r not a valid node" % (node, ))
66
return self.dfs(node, lambda x: self.dependents.get(x, set()))
68
def root_nodes(self, app=None):
70
Returns all root nodes - that is, nodes with no dependencies inside
71
their app. These are the starting point for an app.
74
for node in self.nodes:
75
if not any(key[0] == node[0] for key in self.dependencies.get(node, set())) and (not app or app == node[0]):
79
def leaf_nodes(self, app=None):
81
Returns all leaf nodes - that is, nodes with no dependents in their app.
82
These are the "most current" version of an app's schema.
83
Having more than one per app is technically an error, but one that
84
gets handled further up, in the interactive command - it's usually the
85
result of a VCS merge and needs some user input.
88
for node in self.nodes:
89
if not any(key[0] == node[0] for key in self.dependents.get(node, set())) and (not app or app == node[0]):
93
def dfs(self, start, get_children):
95
Dynamic programming based depth first search, for finding dependencies.
99
def _dfs(start, get_children, path):
100
# If we already computed this, use that (dynamic programming)
101
if (start, get_children) in cache:
102
return cache[(start, get_children)]
103
# If we've traversed here before, that's a circular dep
105
raise CircularDependencyError(path[path.index(start):] + [start])
106
# Build our own results list, starting with us
108
results.append(start)
109
# We need to add to results all the migrations this one depends on
110
children = sorted(get_children(start))
113
results = _dfs(n, get_children, path) + results
115
# Use OrderedSet to ensure only one instance of each result
116
results = list(OrderedSet(results))
118
cache[(start, get_children)] = results
121
return _dfs(start, get_children, [])
124
return "Graph: %s nodes, %s edges" % (len(self.nodes), sum(len(x) for x in self.dependencies.values()))
126
def make_state(self, nodes=None, at_end=True, real_apps=None):
128
Given a migration node or nodes, returns a complete ProjectState for it.
129
If at_end is False, returns the state before the migration has run.
130
If nodes is not provided, returns the overall most current project state.
133
nodes = list(self.leaf_nodes())
135
return ProjectState()
136
if not isinstance(nodes[0], tuple):
140
for migration in self.forwards_plan(node):
141
if migration not in plan:
142
if not at_end and migration in nodes:
144
plan.append(migration)
145
project_state = ProjectState(real_apps=real_apps)
147
project_state = self.nodes[node].mutate_state(project_state)
150
def __contains__(self, node):
151
return node in self.nodes
154
class CircularDependencyError(Exception):
156
Raised when there's an impossible-to-resolve circular dependency.