|
1 | 1 | from dataclasses import dataclass |
2 | | -from typing import TYPE_CHECKING, Any, List, TypeVar |
| 2 | +from typing import TYPE_CHECKING, Any, Iterable, TypeVar |
3 | 3 |
|
4 | 4 | from django.apps import apps |
5 | 5 | from django.core.checks import ERROR, CheckMessage |
6 | 6 | from django.core.exceptions import ValidationError |
| 7 | +from django.db import connections, router |
7 | 8 | from typing_extensions import ParamSpec |
8 | 9 |
|
9 | 10 | from django_tasks.backends.base import BaseTaskBackend |
@@ -81,16 +82,26 @@ async def aget_result(self, result_id: str) -> TaskResult: |
81 | 82 | except (DBTaskResult.DoesNotExist, ValidationError) as e: |
82 | 83 | raise ResultDoesNotExist(result_id) from e |
83 | 84 |
|
84 | | - def check(self, **kwargs: Any) -> List[CheckMessage]: |
85 | | - if not apps.is_installed("django_tasks.backends.database"): |
86 | | - backend_name = self.__class__.__name__ |
| 85 | + def check(self, **kwargs: Any) -> Iterable[CheckMessage]: |
| 86 | + from .models import DBTaskResult |
87 | 87 |
|
88 | | - return [ |
89 | | - CheckMessage( |
90 | | - ERROR, |
91 | | - f"{backend_name} configured as django_tasks backend, but database app not installed", |
92 | | - "Insert 'django_tasks.backends.database' in INSTALLED_APPS", |
93 | | - ) |
94 | | - ] |
| 88 | + backend_name = self.__class__.__name__ |
95 | 89 |
|
96 | | - return [] |
| 90 | + if not apps.is_installed("django_tasks.backends.database"): |
| 91 | + yield CheckMessage( |
| 92 | + ERROR, |
| 93 | + f"{backend_name} configured as django_tasks backend, but database app not installed", |
| 94 | + "Insert 'django_tasks.backends.database' in INSTALLED_APPS", |
| 95 | + ) |
| 96 | + |
| 97 | + db_connection = connections[router.db_for_read(DBTaskResult)] |
| 98 | + if ( |
| 99 | + db_connection.vendor == "sqlite" |
| 100 | + and hasattr(db_connection, "transaction_mode") |
| 101 | + and db_connection.transaction_mode != "EXCLUSIVE" |
| 102 | + ): |
| 103 | + yield CheckMessage( |
| 104 | + ERROR, |
| 105 | + f"{backend_name} is using SQLite non-exclusive transactions", |
| 106 | + f"Set settings.DATABASES[{db_connection.alias!r}]['OPTIONS']['transaction_mode'] to 'EXCLUSIVE'", |
| 107 | + ) |
0 commit comments