summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2018-03-28 00:58:12 -0400
committerTim Graham <timograham@gmail.com>2018-03-28 12:43:43 -0400
commit2156565b5bac2e6f503ad4841e2b6ed44e4de656 (patch)
tree66c5b0e6a2003d9a75409b30c227e85dc6499924 /tests
parentba5f24ea6491780bb245ed6f0e5f015f4defffe9 (diff)
Fixed #29245 -- Made autodetector treat field renames + db_column addition as RenameField.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_autodetector.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index e262a8ee52..fd1bc383b9 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -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(