mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +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:
@@ -9,33 +9,40 @@ class MigrationTestBase(TransactionTestCase):
|
||||
|
||||
available_apps = ["migrations"]
|
||||
|
||||
def get_table_description(self, table):
|
||||
with connection.cursor() as cursor:
|
||||
return connection.introspection.get_table_description(cursor, table)
|
||||
|
||||
def assertTableExists(self, table):
|
||||
self.assertIn(table, connection.introspection.get_table_list(connection.cursor()))
|
||||
with connection.cursor() as cursor:
|
||||
self.assertIn(table, connection.introspection.get_table_list(cursor))
|
||||
|
||||
def assertTableNotExists(self, table):
|
||||
self.assertNotIn(table, connection.introspection.get_table_list(connection.cursor()))
|
||||
with connection.cursor() as cursor:
|
||||
self.assertNotIn(table, connection.introspection.get_table_list(cursor))
|
||||
|
||||
def assertColumnExists(self, table, column):
|
||||
self.assertIn(column, [c.name for c in connection.introspection.get_table_description(connection.cursor(), table)])
|
||||
self.assertIn(column, [c.name for c in self.get_table_description(table)])
|
||||
|
||||
def assertColumnNotExists(self, table, column):
|
||||
self.assertNotIn(column, [c.name for c in connection.introspection.get_table_description(connection.cursor(), table)])
|
||||
self.assertNotIn(column, [c.name for c in self.get_table_description(table)])
|
||||
|
||||
def assertColumnNull(self, table, column):
|
||||
self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], True)
|
||||
self.assertEqual([c.null_ok for c in self.get_table_description(table) if c.name == column][0], True)
|
||||
|
||||
def assertColumnNotNull(self, table, column):
|
||||
self.assertEqual([c.null_ok for c in connection.introspection.get_table_description(connection.cursor(), table) if c.name == column][0], False)
|
||||
self.assertEqual([c.null_ok for c in self.get_table_description(table) if c.name == column][0], False)
|
||||
|
||||
def assertIndexExists(self, table, columns, value=True):
|
||||
self.assertEqual(
|
||||
value,
|
||||
any(
|
||||
c["index"]
|
||||
for c in connection.introspection.get_constraints(connection.cursor(), table).values()
|
||||
if c['columns'] == list(columns)
|
||||
),
|
||||
)
|
||||
with connection.cursor() as cursor:
|
||||
self.assertEqual(
|
||||
value,
|
||||
any(
|
||||
c["index"]
|
||||
for c in connection.introspection.get_constraints(cursor, table).values()
|
||||
if c['columns'] == list(columns)
|
||||
),
|
||||
)
|
||||
|
||||
def assertIndexNotExists(self, table, columns):
|
||||
return self.assertIndexExists(table, columns, False)
|
||||
|
||||
Reference in New Issue
Block a user