mirror of
https://github.com/django/django.git
synced 2025-08-03 18:38:50 +00:00
Ensure cursors are closed when no longer needed.
This commit touchs various parts of the code base and test framework. Any found usage of opening a cursor for the sake of initializing a connection has been replaced with 'ensure_connection()'.
This commit is contained in:
parent
0837eacc4e
commit
3ffeb93186
31 changed files with 657 additions and 615 deletions
|
@ -19,15 +19,15 @@ class OperationTests(MigrationTestBase):
|
|||
Creates a test model state and database table.
|
||||
"""
|
||||
# Delete the tables if they already exist
|
||||
cursor = connection.cursor()
|
||||
try:
|
||||
cursor.execute("DROP TABLE %s_pony" % app_label)
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
cursor.execute("DROP TABLE %s_stable" % app_label)
|
||||
except:
|
||||
pass
|
||||
with connection.cursor() as cursor:
|
||||
try:
|
||||
cursor.execute("DROP TABLE %s_pony" % app_label)
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
cursor.execute("DROP TABLE %s_stable" % app_label)
|
||||
except:
|
||||
pass
|
||||
# Make the "current" state
|
||||
operations = [migrations.CreateModel(
|
||||
"Pony",
|
||||
|
@ -348,21 +348,21 @@ class OperationTests(MigrationTestBase):
|
|||
operation.state_forwards("test_alflpkfk", new_state)
|
||||
self.assertIsInstance(project_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.AutoField)
|
||||
self.assertIsInstance(new_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.FloatField)
|
||||
|
||||
def assertIdTypeEqualsFkType(self):
|
||||
with connection.cursor() as cursor:
|
||||
id_type = [c.type_code for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_pony") if c.name == "id"][0]
|
||||
fk_type = [c.type_code for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider") if c.name == "pony_id"][0]
|
||||
self.assertEqual(id_type, fk_type)
|
||||
assertIdTypeEqualsFkType()
|
||||
# Test the database alteration
|
||||
id_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_pony") if c.name == "id"][0]
|
||||
fk_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_rider") if c.name == "pony_id"][0]
|
||||
self.assertEqual(id_type, fk_type)
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards("test_alflpkfk", editor, project_state, new_state)
|
||||
id_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_pony") if c.name == "id"][0]
|
||||
fk_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_rider") if c.name == "pony_id"][0]
|
||||
self.assertEqual(id_type, fk_type)
|
||||
assertIdTypeEqualsFkType()
|
||||
# And test reversal
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
|
||||
id_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_pony") if c.name == "id"][0]
|
||||
fk_type = [c.type_code for c in connection.introspection.get_table_description(connection.cursor(), "test_alflpkfk_rider") if c.name == "pony_id"][0]
|
||||
self.assertEqual(id_type, fk_type)
|
||||
assertIdTypeEqualsFkType()
|
||||
|
||||
def test_rename_field(self):
|
||||
"""
|
||||
|
@ -400,24 +400,24 @@ class OperationTests(MigrationTestBase):
|
|||
self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0)
|
||||
self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
|
||||
# Make sure we can insert duplicate rows
|
||||
cursor = connection.cursor()
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
# Test the database alteration
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards("test_alunto", editor, project_state, new_state)
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
with self.assertRaises(IntegrityError):
|
||||
with atomic():
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
# And test reversal
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_backwards("test_alunto", editor, new_state, project_state)
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
# Test the database alteration
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_forwards("test_alunto", editor, project_state, new_state)
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
with self.assertRaises(IntegrityError):
|
||||
with atomic():
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
# And test reversal
|
||||
with connection.schema_editor() as editor:
|
||||
operation.database_backwards("test_alunto", editor, new_state, project_state)
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)")
|
||||
cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)")
|
||||
cursor.execute("DELETE FROM test_alunto_pony")
|
||||
# Test flat unique_together
|
||||
operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
|
||||
operation.state_forwards("test_alunto", new_state)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue