summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIuri de Silvio <iurisilvio@gmail.com>2020-07-24 18:08:40 -0300
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-08-13 13:14:58 +0200
commit7f4c9222dfe2f28ff8a7ffc56c28ccbadf19cf6f (patch)
tree878f533d578acfe752047fc7da1c9a0265a6f727 /tests
parent20799cc0a6d98816b9ef0577e24691bd26b80d7d (diff)
Fixed #31825 -- Made RenameField operation a noop for fields with db_column.
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_operations.py42
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 588186fae4..6abbd34ab8 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -1632,6 +1632,48 @@ class OperationTests(OperationTestBase):
self.assertEqual(definition[1], [])
self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"})
+ def test_rename_field_with_db_column(self):
+ project_state = self.apply_operations('test_rfwdbc', ProjectState(), operations=[
+ migrations.CreateModel('Pony', fields=[
+ ('id', models.AutoField(primary_key=True)),
+ ('field', models.IntegerField(db_column='db_field')),
+ ('fk_field', models.ForeignKey(
+ 'Pony',
+ models.CASCADE,
+ db_column='db_fk_field',
+ )),
+ ]),
+ ])
+ new_state = project_state.clone()
+ operation = migrations.RenameField('Pony', 'field', 'renamed_field')
+ operation.state_forwards('test_rfwdbc', new_state)
+ self.assertIn('renamed_field', new_state.models['test_rfwdbc', 'pony'].fields)
+ self.assertNotIn('field', new_state.models['test_rfwdbc', 'pony'].fields)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_field')
+ with connection.schema_editor() as editor:
+ with self.assertNumQueries(0):
+ operation.database_forwards('test_rfwdbc', editor, project_state, new_state)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_field')
+ with connection.schema_editor() as editor:
+ with self.assertNumQueries(0):
+ operation.database_backwards('test_rfwdbc', editor, new_state, project_state)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_field')
+
+ new_state = project_state.clone()
+ operation = migrations.RenameField('Pony', 'fk_field', 'renamed_fk_field')
+ operation.state_forwards('test_rfwdbc', new_state)
+ self.assertIn('renamed_fk_field', new_state.models['test_rfwdbc', 'pony'].fields)
+ self.assertNotIn('fk_field', new_state.models['test_rfwdbc', 'pony'].fields)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
+ with connection.schema_editor() as editor:
+ with self.assertNumQueries(0):
+ operation.database_forwards('test_rfwdbc', editor, project_state, new_state)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
+ with connection.schema_editor() as editor:
+ with self.assertNumQueries(0):
+ operation.database_backwards('test_rfwdbc', editor, new_state, project_state)
+ self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
+
def test_rename_missing_field(self):
state = ProjectState()
state.add_model(ModelState('app', 'model', []))