Fixed #29245 -- Made autodetector treat field renames + db_column addition as RenameField.

This commit is contained in:
Simon Charette 2018-03-28 00:58:12 -04:00 committed by Tim Graham
parent ba5f24ea64
commit 2156565b5b
2 changed files with 70 additions and 2 deletions

View File

@ -797,12 +797,19 @@ class MigrationAutodetector:
field_dec = self.deep_deconstruct(field)
for rem_app_label, rem_model_name, rem_field_name in sorted(self.old_field_keys - self.new_field_keys):
if rem_app_label == app_label and rem_model_name == model_name:
old_field_dec = self.deep_deconstruct(old_model_state.get_field_by_name(rem_field_name))
old_field = old_model_state.get_field_by_name(rem_field_name)
old_field_dec = self.deep_deconstruct(old_field)
if field.remote_field and field.remote_field.model and 'to' in old_field_dec[2]:
old_rel_to = old_field_dec[2]['to']
if old_rel_to in self.renamed_models_rel:
old_field_dec[2]['to'] = self.renamed_models_rel[old_rel_to]
if old_field_dec == field_dec:
old_field.set_attributes_from_name(rem_field_name)
old_db_column = old_field.get_attname_column()[1]
if (old_field_dec == field_dec or (
# Was the field renamed and db_column equal to the
# old field's column added?
old_field_dec[0:2] == field_dec[0:2] and
dict(old_field_dec[2], db_column=old_db_column) == field_dec[2])):
if self.questioner.ask_rename(model_name, rem_field_name, field_name, field):
self.add_operation(
app_label,

View File

@ -921,6 +921,67 @@ class AutodetectorTests(TestCase):
changes, 'app', 0, 1, model_name='bar', old_name='second', new_name='second_renamed',
)
def test_rename_field_preserved_db_column(self):
"""
RenameField is used if a field is renamed and db_column equal to the
old field's column is added.
"""
before = [
ModelState('app', 'Foo', [
('id', models.AutoField(primary_key=True)),
('field', models.IntegerField()),
]),
]
after = [
ModelState('app', 'Foo', [
('id', models.AutoField(primary_key=True)),
('renamed_field', models.IntegerField(db_column='field')),
]),
]
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
self.assertNumberMigrations(changes, 'app', 1)
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
self.assertOperationAttributes(
changes, 'app', 0, 0, model_name='foo', old_name='field', new_name='renamed_field',
)
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='foo', name='renamed_field')
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
'renamed_field', 'django.db.models.IntegerField', [], {'db_column': 'field'},
))
def test_rename_related_field_preserved_db_column(self):
before = [
ModelState('app', 'Foo', [
('id', models.AutoField(primary_key=True)),
]),
ModelState('app', 'Bar', [
('id', models.AutoField(primary_key=True)),
('foo', models.ForeignKey('app.Foo', models.CASCADE)),
]),
]
after = [
ModelState('app', 'Foo', [
('id', models.AutoField(primary_key=True)),
]),
ModelState('app', 'Bar', [
('id', models.AutoField(primary_key=True)),
('renamed_foo', models.ForeignKey('app.Foo', models.CASCADE, db_column='foo_id')),
]),
]
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
self.assertNumberMigrations(changes, 'app', 1)
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
self.assertOperationAttributes(
changes, 'app', 0, 0, model_name='bar', old_name='foo', new_name='renamed_foo',
)
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='bar', name='renamed_foo')
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
'renamed_foo',
'django.db.models.ForeignKey',
[],
{'to': 'app.Foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'},
))
def test_rename_model(self):
"""Tests autodetection of renamed models."""
changes = self.get_changes(