Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Prompt about renames rather than doing them automatically

  • Loading branch information...
commit cca40703dfdc6171bebe9be50a82ef61a0749cb0 1 parent 92a10f5
@andrewgodwin andrewgodwin authored
View
31 django/db/migrations/autodetector.py
@@ -74,18 +74,19 @@ def changes(self):
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 @@ def ask_not_null_addition(self, field_name, model_name):
# 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 @@ def ask_not_null_addition(self, field_name, model_name):
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__))
View
2  tests/migrations/test_autodetector.py
@@ -155,7 +155,7 @@ def test_rename_field(self):
# 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)
Please sign in to comment.
Something went wrong with that request. Please try again.