diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py index b05f37a8bb..9f0690b483 100644 --- a/django/core/management/commands/makemigrations.py +++ b/django/core/management/commands/makemigrations.py @@ -49,10 +49,7 @@ class Command(BaseCommand): 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: diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index 2771bcbc68..334c26d973 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -25,7 +25,19 @@ class MigrationAutodetector(object): 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 @@ class MigrationAutodetector(object): 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 @@ class MigrationAutodetector(object): 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 diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 9b7fbd5e8a..de3b156ba6 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -44,9 +44,9 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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 @@ class AutodetectorTests(TestCase): 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?