Loading django/contrib/gis/db/backends/postgis/creation.py +7 −7 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ class PostGISCreation(DatabaseCreation): @cached_property def template_postgis(self): template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis') cursor = self.connection.cursor() with self.connection.cursor() as cursor: cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,)) if cursor.fetchone(): return template_postgis Loading Loading @@ -88,7 +88,7 @@ class PostGISCreation(DatabaseCreation): # Connect to the test database in order to create the postgis extension self.connection.close() self.connection.settings_dict["NAME"] = test_database_name cursor = self.connection.cursor() with self.connection.cursor() as cursor: cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis") cursor.connection.commit() Loading django/contrib/gis/db/backends/spatialite/creation.py +2 −3 Original line number Diff line number Diff line Loading @@ -55,9 +55,8 @@ class SpatiaLiteCreation(DatabaseCreation): call_command('createcachetable', database=self.connection.alias) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. self.connection.cursor() # Ensure a connection for the side effect of initializing the test database. self.connection.ensure_connection() return test_database_name Loading django/contrib/sites/management.py +3 −3 Original line number Diff line number Diff line Loading @@ -33,7 +33,7 @@ def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB if sequence_sql: if verbosity >= 2: print("Resetting sequence") cursor = connections[db].cursor() with connections[db].cursor() as cursor: for command in sequence_sql: cursor.execute(command) Loading django/core/cache/backends/db.py +65 −64 Original line number Diff line number Diff line Loading @@ -59,8 +59,8 @@ class DatabaseCache(BaseDatabaseCache): self.validate_key(key) db = router.db_for_read(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("SELECT cache_key, value, expires FROM %s " "WHERE cache_key = %%s" % table, [key]) row = cursor.fetchone() Loading @@ -75,7 +75,7 @@ class DatabaseCache(BaseDatabaseCache): expires = typecast_timestamp(str(expires)) if expires < now: db = router.db_for_write(self.cache_model_class) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("DELETE FROM %s " "WHERE cache_key = %%s" % table, [key]) return default Loading @@ -96,8 +96,8 @@ class DatabaseCache(BaseDatabaseCache): timeout = self.get_backend_timeout(timeout) db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("SELECT COUNT(*) FROM %s" % table) num = cursor.fetchone()[0] now = timezone.now() Loading Loading @@ -152,8 +152,8 @@ class DatabaseCache(BaseDatabaseCache): db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key]) def has_key(self, key, version=None): Loading @@ -162,13 +162,14 @@ class DatabaseCache(BaseDatabaseCache): db = router.db_for_read(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() if settings.USE_TZ: now = datetime.utcnow() else: now = datetime.now() now = now.replace(microsecond=0) with connections[db].cursor() as cursor: cursor.execute("SELECT cache_key FROM %s " "WHERE cache_key = %%s and expires > %%s" % table, [key, connections[db].ops.value_to_db_datetime(now)]) Loading Loading @@ -197,7 +198,7 @@ class DatabaseCache(BaseDatabaseCache): def clear(self): db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute('DELETE FROM %s' % table) Loading django/core/management/commands/createcachetable.py +9 −9 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ class Command(BaseCommand): full_statement.append(' %s%s' % (line, ',' if i < len(table_output) - 1 else '')) full_statement.append(');') with transaction.commit_on_success_unless_managed(): curs = connection.cursor() with connection.cursor() as curs: try: curs.execute("\n".join(full_statement)) except DatabaseError as e: Loading Loading
django/contrib/gis/db/backends/postgis/creation.py +7 −7 Original line number Diff line number Diff line Loading @@ -11,7 +11,7 @@ class PostGISCreation(DatabaseCreation): @cached_property def template_postgis(self): template_postgis = getattr(settings, 'POSTGIS_TEMPLATE', 'template_postgis') cursor = self.connection.cursor() with self.connection.cursor() as cursor: cursor.execute('SELECT 1 FROM pg_database WHERE datname = %s LIMIT 1;', (template_postgis,)) if cursor.fetchone(): return template_postgis Loading Loading @@ -88,7 +88,7 @@ class PostGISCreation(DatabaseCreation): # Connect to the test database in order to create the postgis extension self.connection.close() self.connection.settings_dict["NAME"] = test_database_name cursor = self.connection.cursor() with self.connection.cursor() as cursor: cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis") cursor.connection.commit() Loading
django/contrib/gis/db/backends/spatialite/creation.py +2 −3 Original line number Diff line number Diff line Loading @@ -55,9 +55,8 @@ class SpatiaLiteCreation(DatabaseCreation): call_command('createcachetable', database=self.connection.alias) # Get a cursor (even though we don't need one yet). This has # the side effect of initializing the test database. self.connection.cursor() # Ensure a connection for the side effect of initializing the test database. self.connection.ensure_connection() return test_database_name Loading
django/contrib/sites/management.py +3 −3 Original line number Diff line number Diff line Loading @@ -33,7 +33,7 @@ def create_default_site(app_config, verbosity=2, interactive=True, db=DEFAULT_DB if sequence_sql: if verbosity >= 2: print("Resetting sequence") cursor = connections[db].cursor() with connections[db].cursor() as cursor: for command in sequence_sql: cursor.execute(command) Loading
django/core/cache/backends/db.py +65 −64 Original line number Diff line number Diff line Loading @@ -59,8 +59,8 @@ class DatabaseCache(BaseDatabaseCache): self.validate_key(key) db = router.db_for_read(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("SELECT cache_key, value, expires FROM %s " "WHERE cache_key = %%s" % table, [key]) row = cursor.fetchone() Loading @@ -75,7 +75,7 @@ class DatabaseCache(BaseDatabaseCache): expires = typecast_timestamp(str(expires)) if expires < now: db = router.db_for_write(self.cache_model_class) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("DELETE FROM %s " "WHERE cache_key = %%s" % table, [key]) return default Loading @@ -96,8 +96,8 @@ class DatabaseCache(BaseDatabaseCache): timeout = self.get_backend_timeout(timeout) db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("SELECT COUNT(*) FROM %s" % table) num = cursor.fetchone()[0] now = timezone.now() Loading Loading @@ -152,8 +152,8 @@ class DatabaseCache(BaseDatabaseCache): db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % table, [key]) def has_key(self, key, version=None): Loading @@ -162,13 +162,14 @@ class DatabaseCache(BaseDatabaseCache): db = router.db_for_read(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() if settings.USE_TZ: now = datetime.utcnow() else: now = datetime.now() now = now.replace(microsecond=0) with connections[db].cursor() as cursor: cursor.execute("SELECT cache_key FROM %s " "WHERE cache_key = %%s and expires > %%s" % table, [key, connections[db].ops.value_to_db_datetime(now)]) Loading Loading @@ -197,7 +198,7 @@ class DatabaseCache(BaseDatabaseCache): def clear(self): db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) cursor = connections[db].cursor() with connections[db].cursor() as cursor: cursor.execute('DELETE FROM %s' % table) Loading
django/core/management/commands/createcachetable.py +9 −9 Original line number Diff line number Diff line Loading @@ -72,7 +72,7 @@ class Command(BaseCommand): full_statement.append(' %s%s' % (line, ',' if i < len(table_output) - 1 else '')) full_statement.append(');') with transaction.commit_on_success_unless_managed(): curs = connection.cursor() with connection.cursor() as curs: try: curs.execute("\n".join(full_statement)) except DatabaseError as e: Loading