django_subatomic.db ¶
transaction ¶
transaction(func: None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None]
transaction[**P, R](func: Callable[P, R], *, using: str | None = None) -> Callable[P, R]
transaction[**P, R](func: Callable[P, R] | None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None] | Callable[P, R]
Create a database transaction.
Can be used as a decorator or a context manager.
Nested calls are not allowed because SQL does not support nested transactions.
Consider this like Django's atomic(durable=True), but with added after-commit callback support in tests.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if we call this from inside another existing transaction. |
Source code in django_subatomic/db.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
transaction_if_not_already ¶
transaction_if_not_already(func: None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None]
transaction_if_not_already[**P, R](func: Callable[P, R], *, using: str | None = None) -> Callable[P, R]
transaction_if_not_already[**P, R](func: Callable[P, R] | None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None] | Callable[P, R]
Create a transaction if one isn't already open.
Can be used as a decorator or a context manager.
Use of this hints at code which lacks control over the state it's called in.
Note
This has a bit of a clunky name. This is a deliberate attempt to discourage its use. It acts as a code-smell to highlight that places which use it may need further work to achieve full control over how transactions are managed.
Suggested alternatives
-
In functions which should not control transactions, use
transaction_required. This ensures they are handled by the caller. -
In functions which can unambiguously control transactions, use
transaction.
Source code in django_subatomic/db.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
savepoint ¶
savepoint(*, using: str | None = None) -> Generator[None]
Create a database savepoint.
Can be used as a context manager, but not as a decorator.
Must be called inside an active transaction.
Recommended usage
- You should only create a savepoint if you may roll back to it before
continuing with your transaction. If your intention is to ensure that
your code is committed atomically, consider using
transaction_requiredinstead. - We believe savepoint rollback should be handled where the savepoint is created. That locality is not possible with a decorator, so this function deliberately does not work as one.
Raises:
| Type | Description |
|---|---|
_MissingRequiredTransaction
|
if we are not in a transaction |
Source code in django_subatomic/db.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
transaction_required ¶
transaction_required(func: None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None]
transaction_required[**P, R](func: Callable[P, R], *, using: str | None = None) -> Callable[P, R]
transaction_required[**P, R](func: Callable[P, R] | None = None, *, using: str | None = None) -> contextlib._GeneratorContextManager[None] | Callable[P, R]
Make sure that code is always executed in a transaction.
Can be used as a decorator or a context manager.
We ignore test-suite transactions when checking for a transaction because we don't want to run the risk of allowing code to pass tests but fail in production.
Raises:
| Type | Description |
|---|---|
_MissingRequiredTransaction
|
if we are not in a transaction. |
Source code in django_subatomic/db.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
durable ¶
durable[**P, R](func: Callable[P, R]) -> Callable[P, R]
Enforce durability with this decorator.
Can be used as a decorator, but not as a context manager.
"Durability" means that the function's work cannot be rolled back after it completes, and is not to be confused with "atomicity" (which is about ensuring that the function either completes all its work or none of it).
We enforce this by ensuring that the function is not called within a transaction, and that no transaction is left open when the function completes.
Raises:
| Type | Description |
|---|---|
_UnexpectedOpenTransaction
|
if a transaction is already open when this is called. |
_UnexpectedDanglingTransaction
|
if a transaction remains open after the decorated function exits. Before raising this exeption, we roll back and end the transaction. |
Source code in django_subatomic/db.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
run_after_commit ¶
run_after_commit(callback: Callable[[], object], *, using: str | None = None) -> None
Register a callback to be called after the current transaction is committed.
(Transactions created by the test suite are deliberately ignored.)
If the current transaction is rolled back, the callback will not be called.
By default, an error will be raised if there is no transaction open.
While you are transitioning your codebase to stricter transaction handling,
you may disable this with settings.SUBATOMIC_AFTER_COMMIT_NEEDS_TRANSACTION.
Note
Django's on_commit has a robust parameter, which allows a callback
to fail silently. Kraken has a convention to "not allow code to fail
silently" so this behaviour is not available from this function.
Source code in django_subatomic/db.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 | |
in_transaction ¶
in_transaction(*, using: str | None = None) -> bool
Return True if the database connection has a transaction active.
A transaction is active if the connection is no longer in autocommit mode.
So that code doesn't need to handle how testcase transactions work, testcase transactions are not considered a transaction.
Source code in django_subatomic/db.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
dbs_with_open_transactions ¶
dbs_with_open_transactions() -> frozenset[str]
Get the names of databases with open transactions.
Source code in django_subatomic/db.py
578 579 580 581 582 583 584 585 586 587 588 589 | |