From cca40703dfdc6171bebe9be50a82ef61a0749cb0 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Fri, 21 Jun 2013 15:32:15 +0100 Subject: [PATCH] Prompt about renames rather than doing them automatically --- django/db/migrations/autodetector.py | 31 +++++++++++++++++---------- tests/migrations/test_autodetector.py | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index b915a69293..107141d1cf 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -74,18 +74,19 @@ class MigrationAutodetector(object): found_rename = False for removed_field_name in (old_field_names - new_field_names): if old_model_state.get_field_by_name(removed_field_name).deconstruct()[1:] == field_dec: - self.add_to_migration( - app_label, - operations.RenameField( - model_name = model_name, - old_name = removed_field_name, - new_name = field_name, + if self.questioner.ask_rename(model_name, removed_field_name, field_name, field): + self.add_to_migration( + app_label, + operations.RenameField( + model_name = model_name, + old_name = removed_field_name, + new_name = field_name, + ) ) - ) - old_field_names.remove(removed_field_name) - new_field_names.remove(field_name) - found_rename = True - break + old_field_names.remove(removed_field_name) + new_field_names.remove(field_name) + found_rename = True + break if found_rename: continue # You can't just add NOT NULL fields with no default @@ -257,6 +258,10 @@ class MigrationQuestioner(object): # None means quit return None + def ask_rename(self, model_name, old_name, new_name, field_instance): + "Was this field really renamed?" + return self.defaults.get("ask_rename", False) + class InteractiveMigrationQuestioner(MigrationQuestioner): @@ -323,3 +328,7 @@ class InteractiveMigrationQuestioner(MigrationQuestioner): print("Invalid input: %s" % e) else: break + + def ask_rename(self, model_name, old_name, new_name, field_instance): + "Was this field really renamed?" + return self._boolean_input("Did you rename %s.%s to %s.%s (a %s)?" % (model_name, old_name, model_name, new_name, field_instance.__class__.__name__)) diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index d9031faca7..7a2f4715fa 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -155,7 +155,7 @@ class AutodetectorTests(TestCase): # Make state before = self.make_project_state([self.author_name]) after = self.make_project_state([self.author_name_renamed]) - autodetector = MigrationAutodetector(before, after) + autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_rename": True})) changes = autodetector.changes() # Right number of migrations? self.assertEqual(len(changes['testapp']), 1)