Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change autodetector changes API to be just one method

  • Loading branch information...
commit 2e7f45a3721a8afbc61a4320ae9ab3908d1e81b4 1 parent b6a957f
@andrewgodwin andrewgodwin authored
View
5 django/core/management/commands/makemigrations.py
@@ -49,10 +49,7 @@ def handle(self, *app_labels, **options):
ProjectState.from_app_cache(cache),
InteractiveMigrationQuestioner(specified_apps=app_labels),
)
- changes = autodetector.changes()
- changes = autodetector.arrange_for_graph(changes, loader.graph)
- if app_labels:
- changes = autodetector.trim_to_apps(changes, app_labels)
+ changes = autodetector.changes(graph=loader.graph, trim_to_apps=app_labels or None)
# No changes? Tell them.
if not changes:
View
18 django/db/migrations/autodetector.py
@@ -25,7 +25,19 @@ def __init__(self, from_state, to_state, questioner=None):
self.to_state = to_state
self.questioner = questioner or MigrationQuestioner()
- def changes(self):
+ def changes(self, graph, trim_to_apps=None):
+ """
+ Main entry point to produce a list of appliable changes.
+ Takes a graph to base names on and an optional set of apps
+ to try and restrict to (restriction is not guaranteed)
+ """
+ changes = self._detect_changes()
+ changes = self._arrange_for_graph(changes, graph)
+ if trim_to_apps:
+ changes = self._trim_to_apps(changes, trim_to_apps)
+ return changes
+
+ def _detect_changes(self):
"""
Returns a dict of migration plans which will achieve the
change from from_state to to_state. The dict has app labels
@@ -229,7 +241,7 @@ def add_dependency(self, app_label, other_app_label):
dependency = (other_app_label, "__first__")
self.migrations[app_label][-1].dependencies.append(dependency)
- def arrange_for_graph(self, changes, graph):
+ def _arrange_for_graph(self, changes, graph):
"""
Takes in a result from changes() and a MigrationGraph,
and fixes the names and dependencies of the changes so they
@@ -273,7 +285,7 @@ def arrange_for_graph(self, changes, graph):
migration.dependencies = [name_map.get(d, d) for d in migration.dependencies]
return changes
- def trim_to_apps(self, changes, app_labels):
+ def _trim_to_apps(self, changes, app_labels):
"""
Takes changes from arrange_for_graph and set of app labels and
returns a modified set of changes which trims out as many migrations
View
30 tests/migrations/test_autodetector.py
@@ -44,9 +44,9 @@ def test_arrange_for_graph(self):
before = self.make_project_state([])
after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Run through arrange_for_graph
- changes = autodetector.arrange_for_graph(changes, graph)
+ changes = autodetector._arrange_for_graph(changes, graph)
# Make sure there's a new name, deps match, etc.
self.assertEqual(changes["testapp"][0].name, "0003_author")
self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
@@ -59,12 +59,12 @@ def test_trim_apps(self):
before = self.make_project_state([])
after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable, self.third_thing])
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_initial": True}))
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Run through arrange_for_graph
graph = MigrationGraph()
- changes = autodetector.arrange_for_graph(changes, graph)
+ changes = autodetector._arrange_for_graph(changes, graph)
changes["testapp"][0].dependencies.append(("otherapp", "0001_initial"))
- changes = autodetector.trim_to_apps(changes, set(["testapp"]))
+ changes = autodetector._trim_to_apps(changes, set(["testapp"]))
# Make sure there's the right set of migrations
self.assertEqual(changes["testapp"][0].name, "0001_initial")
self.assertEqual(changes["otherapp"][0].name, "0001_initial")
@@ -76,7 +76,7 @@ def test_new_model(self):
before = self.make_project_state([])
after = self.make_project_state([self.author_empty])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -93,7 +93,7 @@ def test_old_model(self):
before = self.make_project_state([self.author_empty])
after = self.make_project_state([])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -110,7 +110,7 @@ def test_add_field(self):
before = self.make_project_state([self.author_empty])
after = self.make_project_state([self.author_name])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -127,7 +127,7 @@ def test_remove_field(self):
before = self.make_project_state([self.author_name])
after = self.make_project_state([self.author_empty])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -144,7 +144,7 @@ def test_alter_field(self):
before = self.make_project_state([self.author_name])
after = self.make_project_state([self.author_name_longer])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -161,7 +161,7 @@ def test_rename_field(self):
before = self.make_project_state([self.author_name])
after = self.make_project_state([self.author_name_renamed])
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_rename": True}))
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
# Right number of actions?
@@ -179,7 +179,7 @@ def test_fk_dependency(self):
before = self.make_project_state([])
after = self.make_project_state([self.author_name, self.book, self.edition])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
self.assertEqual(len(changes['otherapp']), 1)
@@ -212,7 +212,7 @@ def test_circular_fk_dependency(self):
before = self.make_project_state([])
after = self.make_project_state([self.author_with_book, self.book])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['testapp']), 1)
self.assertEqual(len(changes['otherapp']), 2)
@@ -243,7 +243,7 @@ def test_unique_together(self):
before = self.make_project_state([self.author_empty, self.book])
after = self.make_project_state([self.author_empty, self.book_unique])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['otherapp']), 1)
# Right number of actions?
@@ -261,7 +261,7 @@ def test_unique_together_ordering(self):
before = self.make_project_state([self.author_empty, self.book_unique])
after = self.make_project_state([self.author_empty, self.book_unique_2])
autodetector = MigrationAutodetector(before, after)
- changes = autodetector.changes()
+ changes = autodetector._detect_changes()
# Right number of migrations?
self.assertEqual(len(changes['otherapp']), 1)
# Right number of actions?

0 comments on commit 2e7f45a

Please sign in to comment.
Something went wrong with that request. Please try again.