Fixed #33197 -- Made field rename with prior matching db_column change a noop.
Thanks Jacob Walls for the report.
This commit is contained in:
parent
69b0736fad
commit
5896aa8367
|
@ -840,6 +840,20 @@ class MigrationAutodetector:
|
||||||
old_field_dec[0:2] == field_dec[0:2] and
|
old_field_dec[0:2] == field_dec[0:2] and
|
||||||
dict(old_field_dec[2], db_column=old_db_column) == field_dec[2])):
|
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):
|
if self.questioner.ask_rename(model_name, rem_field_name, field_name, field):
|
||||||
|
# A db_column mismatch requires a prior noop
|
||||||
|
# AlterField for the subsequent RenameField to be a
|
||||||
|
# noop on attempts at preserving the old name.
|
||||||
|
if old_field.db_column != field.db_column:
|
||||||
|
altered_field = field.clone()
|
||||||
|
altered_field.name = rem_field_name
|
||||||
|
self.add_operation(
|
||||||
|
app_label,
|
||||||
|
operations.AlterField(
|
||||||
|
model_name=model_name,
|
||||||
|
name=rem_field_name,
|
||||||
|
field=altered_field,
|
||||||
|
),
|
||||||
|
)
|
||||||
self.add_operation(
|
self.add_operation(
|
||||||
app_label,
|
app_label,
|
||||||
operations.RenameField(
|
operations.RenameField(
|
||||||
|
@ -970,7 +984,10 @@ class MigrationAutodetector:
|
||||||
new_field.remote_field.through = old_field.remote_field.through
|
new_field.remote_field.through = old_field.remote_field.through
|
||||||
old_field_dec = self.deep_deconstruct(old_field)
|
old_field_dec = self.deep_deconstruct(old_field)
|
||||||
new_field_dec = self.deep_deconstruct(new_field)
|
new_field_dec = self.deep_deconstruct(new_field)
|
||||||
if old_field_dec != new_field_dec:
|
# If the field was confirmed to be renamed it means that only
|
||||||
|
# db_column was allowed to change which generate_renamed_fields()
|
||||||
|
# already accounts for by adding an AlterField operation.
|
||||||
|
if old_field_dec != new_field_dec and old_field_name == field_name:
|
||||||
both_m2m = old_field.many_to_many and new_field.many_to_many
|
both_m2m = old_field.many_to_many and new_field.many_to_many
|
||||||
neither_m2m = not old_field.many_to_many and not new_field.many_to_many
|
neither_m2m = not old_field.many_to_many and not new_field.many_to_many
|
||||||
if both_m2m or neither_m2m:
|
if both_m2m or neither_m2m:
|
||||||
|
|
|
@ -239,7 +239,11 @@ class AlterField(FieldOperation):
|
||||||
def reduce(self, operation, app_label):
|
def reduce(self, operation, app_label):
|
||||||
if isinstance(operation, RemoveField) and self.is_same_field_operation(operation):
|
if isinstance(operation, RemoveField) and self.is_same_field_operation(operation):
|
||||||
return [operation]
|
return [operation]
|
||||||
elif isinstance(operation, RenameField) and self.is_same_field_operation(operation):
|
elif (
|
||||||
|
isinstance(operation, RenameField) and
|
||||||
|
self.is_same_field_operation(operation) and
|
||||||
|
self.field.db_column is None
|
||||||
|
):
|
||||||
return [
|
return [
|
||||||
operation,
|
operation,
|
||||||
AlterField(
|
AlterField(
|
||||||
|
|
|
@ -1001,14 +1001,17 @@ class AutodetectorTests(TestCase):
|
||||||
]
|
]
|
||||||
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
||||||
self.assertNumberMigrations(changes, 'app', 1)
|
self.assertNumberMigrations(changes, 'app', 1)
|
||||||
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
|
self.assertOperationTypes(changes, 'app', 0, ['AlterField', 'RenameField'])
|
||||||
self.assertOperationAttributes(
|
self.assertOperationAttributes(
|
||||||
changes, 'app', 0, 0, model_name='foo', old_name='field', new_name='renamed_field',
|
changes, 'app', 0, 0, model_name='foo', name='field',
|
||||||
)
|
)
|
||||||
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='foo', name='renamed_field')
|
self.assertEqual(changes['app'][0].operations[0].field.deconstruct(), (
|
||||||
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
|
'field', 'django.db.models.IntegerField', [], {'db_column': 'field'},
|
||||||
'renamed_field', 'django.db.models.IntegerField', [], {'db_column': 'field'},
|
|
||||||
))
|
))
|
||||||
|
self.assertOperationAttributes(
|
||||||
|
changes, 'app', 0, 1, model_name='foo', old_name='field',
|
||||||
|
new_name='renamed_field',
|
||||||
|
)
|
||||||
|
|
||||||
def test_rename_related_field_preserved_db_column(self):
|
def test_rename_related_field_preserved_db_column(self):
|
||||||
before = [
|
before = [
|
||||||
|
@ -1031,17 +1034,20 @@ class AutodetectorTests(TestCase):
|
||||||
]
|
]
|
||||||
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename': True}))
|
||||||
self.assertNumberMigrations(changes, 'app', 1)
|
self.assertNumberMigrations(changes, 'app', 1)
|
||||||
self.assertOperationTypes(changes, 'app', 0, ['RenameField', 'AlterField'])
|
self.assertOperationTypes(changes, 'app', 0, ['AlterField', 'RenameField'])
|
||||||
self.assertOperationAttributes(
|
self.assertOperationAttributes(
|
||||||
changes, 'app', 0, 0, model_name='bar', old_name='foo', new_name='renamed_foo',
|
changes, 'app', 0, 0, model_name='bar', name='foo',
|
||||||
)
|
)
|
||||||
self.assertOperationAttributes(changes, 'app', 0, 1, model_name='bar', name='renamed_foo')
|
self.assertEqual(changes['app'][0].operations[0].field.deconstruct(), (
|
||||||
self.assertEqual(changes['app'][0].operations[-1].field.deconstruct(), (
|
'foo',
|
||||||
'renamed_foo',
|
|
||||||
'django.db.models.ForeignKey',
|
'django.db.models.ForeignKey',
|
||||||
[],
|
[],
|
||||||
{'to': 'app.foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'},
|
{'to': 'app.foo', 'on_delete': models.CASCADE, 'db_column': 'foo_id'},
|
||||||
))
|
))
|
||||||
|
self.assertOperationAttributes(
|
||||||
|
changes, 'app', 0, 1, model_name='bar', old_name='foo',
|
||||||
|
new_name='renamed_foo',
|
||||||
|
)
|
||||||
|
|
||||||
def test_rename_model(self):
|
def test_rename_model(self):
|
||||||
"""Tests autodetection of renamed models."""
|
"""Tests autodetection of renamed models."""
|
||||||
|
|
Loading…
Reference in New Issue