8000 move `supports_async` to `DatabaseFeatures` class. · django/django@126f15a · GitHub
[go: up one dir, main page]

Skip to content

Commit 126f15a

Browse files
committed
move supports_async to DatabaseFeatures class.
1 parent 23dbad2 commit 126f15a

File tree

11 files changed

+28
-27
lines changed

11 files changed

+28
-27
lines changed

django/db/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __init__(self, using=DEFAULT_DB_ALIAS):
5151

5252
async def __aenter__(self):
5353
conn = connections.create_connection(self.using)
54-
if conn.supports_async is False:
54+
if conn.features.supports_async is False:
5555
raise NotSupportedError(
5656
"The database backend does not support asynchronous execution."
5757
)

django/db/backends/base/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class BaseDatabaseWrapper:
3939
ops = None
4040
vendor = "unknown"
4141
display_name = "unknown"
42-
supports_async = False
4342

4443
SchemaEditorClass = None
4544
# Classes instantiated in __init__().

django/db/backends/base/features.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ class BaseDatabaseFeatures:
348348
# Does the backend support JSONObject() database function?
349349
has_json_object_function = True
350350

351+
# Asynchronous database operations
352+
supports_async = False
353+
351354
# Does the backend support column collations?
352355
supports_collation_on_charfield = True
353356
supports_collation_on_textfield = True

django/db/backends/postgresql/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
9393
vendor = "postgresql"
9494
display_name = "PostgreSQL"
9595
_pg_version = None
96-
supports_async = is_psycopg3
9796

9897
# This dictionary maps Field objects to their associated PostgreSQL column
9998
# types, as strings. Column-type strings can contain format strings; they'll

django/db/backends/postgresql/features.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5353
END;
5454
$$ LANGUAGE plpgsql;"""
5555
requires_casted_case_in_updates = True
56+
supports_async = is_psycopg3
5657
supports_over_clause = True
5758
supports_frame_exclusion = True
5859
only_supports_unbounded_with_preceding_and_following = True

django/db/backends/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def debug_transaction(connection, sql):
319319
{
320320
"sql": "%s" % sql,
321321
"time": "%.3f" % duration,
322-
"async": connection.supports_async,
322+
"async": connection.features.supports_async,
323323
}
324324
)
325325
logger.debug(
@@ -328,12 +328,12 @@ def debug_transaction(connection, sql):
328328
sql,
329329
None,
330330
connection.alias,
331-
connection.supports_async,
331+
connection.features.supports_async,
332332
extra={
333333
"duration": duration,
334334
"sql": sql,
335335
"alias": connection.alias,
336-
"async": connection.supports_async,
336+
"async": connection.features.supports_async,
337337
},
338338
)
339339

tests/async/test_async_connections.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import unittest
2-
31
from asgiref.sync import sync_to_async
42

5-
from django.db import connection, new_connection
6-
from django.test import TransactionTestCase
3+
from django.db import new_connection
4+
from django.test import TransactionTestCase, skipUnlessDBFeature
75

86
from .models import SimpleModel
97

108

11-
@unittest.skipUnless(connection.supports_async is True, "Async DB test")
9+
@skipUnlessDBFeature("supports_async")
1210
class AsyncSyncCominglingTest(TransactionTestCase):
1311

1412
available_apps = ["async"]

tests/async/test_async_cursor.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import unittest
1+
from django.db import new_connection
2+
from django.test import SimpleTestCase, skipUnlessDBFeature
23

3-
from django.db import connection, new_connection
4-
from django.test import SimpleTestCase
54

6-
7-
@unittest.skipUnless(connection.supports_async is True, "Async DB test")
5+
@skipUnlessDBFeature("supports_async")
86
class AsyncCursorTests(SimpleTestCase):
7+
databases = {"default", "other"}
8+
99
async def test_aexecute(self):
1010
async with new_connection() as conn:
1111
async with conn.acursor() as cursor:

tests/backends/base/test_base_async.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import unittest
1+
from django.db import new_connection
2+
from django.test import SimpleTestCase, skipUnlessDBFeature
23

3-
from django.db import connection, new_connection
4-
from django.test import SimpleTestCase
54

6-
7-
@unittest.skipUnless(connection.supports_async is True, "Async DB test")
5+
@skipUnlessDBFeature("supports_async")
86
class AsyncDatabaseWrapperTests(SimpleTestCase):
7+
databases = {"default", "other"}
8+
99
async def test_async_cursor(self):
1010
async with new_connection() as conn:
1111
async with conn.acursor() as cursor:

tests/db_utils/tests.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ConnectionHandler,
2121
load_backend,
2222
)
23-
from django.test import SimpleTestCase, TestCase
23+
from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature
2424
from django.utils.connection import ConnectionDoesNotExist
2525

2626

@@ -108,6 +108,8 @@ def test_load_backend_invalid_name(self):
108108

109109

110110
class AsyncConnectionTests(SimpleTestCase):
111+
databases = {"default", "other"}
112+
111113
def run_pool(self, coro, count=2):
112114
def fn():
113115
asyncio.run(coro())
@@ -146,7 +148,7 @@ async def coro():
146148

147149
self.run_pool(coro)
148150

149-
@unittest.skipUnless(connection.supports_async is True, "Async DB test")
151+
@skipUnlessDBFeature("supports_async")
150152
def test_new_connection_threading(self):
151153
async def coro():
152154
assert async_connections.empty is True
@@ -156,7 +158,7 @@ async def coro():
156158

157159
self.run_pool(coro)
158160

159-
@unittest.skipUnless(connection.supports_async is True, "Async DB test")
161+
@skipUnlessDBFeature("supports_async")
160162
async def test_new_connection(self):
161163
with self.assertRaises(ConnectionDoesNotExist):
162164
async_connections.get_connection(DEFAULT_DB_ALIAS)
@@ -177,7 +179,7 @@ async def test_new_connection(self):
177179
with self.assertRaises(ConnectionDoesNotExist):
178180
async_connections.get_connection(DEFAULT_DB_ALIAS)
179181

180-
@unittest.skipUnless(connection.supports_async is False, "Sync DB test")
182+
@skipUnlessDBFeature("supports_async")
181183
async def test_new_connection_on_sync(self):
182184
with self.assertRaises(NotSupportedError):
183185
async with new_connection():

tests/transactions/tests.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,7 @@ class DurableTests(DurableTestsBase, TestCase):
580580
pass
581581

582582

583-
@skipUnlessDBFeature("uses_savepoints")
584-
@skipUnless(connection.supports_async is True, "Async DB test")
583+
@skipUnlessDBFeature("uses_savepoints", "supports_async")
585584
class AsyncTransactionTestCase(TransactionTestCase):
586585
available_apps = ["transactions"]
587586

0 commit comments

Comments
 (0)
0