Skip to content

Commit

Permalink
Fixed #22975: Don't call rename SQL if not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgodwin committed Jul 21, 2014
1 parent 2984b30 commit dcb4ed5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django/db/backends/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ def alter_db_table(self, model, old_db_table, new_db_table):
"""
Renames the table a model points to.
"""
if old_db_table == new_db_table:
return
self.execute(self.sql_rename_table % {
"old_table": self.quote_name(old_db_table),
"new_table": self.quote_name(new_db_table),
Expand Down
20 changes: 20 additions & 0 deletions tests/migrations/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,26 @@ def test_alter_model_table(self):
self.assertTableExists("test_almota_pony")
self.assertTableNotExists("test_almota_pony_2")

def test_alter_model_table_noop(self):
"""
Tests the AlterModelTable operation if the table name is not changed.
"""
project_state = self.set_up_test_model("test_almota")
# Test the state alteration
operation = migrations.AlterModelTable("Pony", "test_almota_pony")
new_state = project_state.clone()
operation.state_forwards("test_almota", new_state)
self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
# Test the database alteration
self.assertTableExists("test_almota_pony")
with connection.schema_editor() as editor:
operation.database_forwards("test_almota", editor, project_state, new_state)
self.assertTableExists("test_almota_pony")
# And test reversal
with connection.schema_editor() as editor:
operation.database_backwards("test_almota", editor, new_state, project_state)
self.assertTableExists("test_almota_pony")

def test_alter_field(self):
"""
Tests the AlterField operation.
Expand Down

0 comments on commit dcb4ed5

Please sign in to comment.