snake-orm 0.1.0b1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- snake_orm-0.1.0b1.dist-info/METADATA +366 -0
- snake_orm-0.1.0b1.dist-info/RECORD +198 -0
- snake_orm-0.1.0b1.dist-info/WHEEL +4 -0
- snake_orm-0.1.0b1.dist-info/entry_points.txt +2 -0
- snake_orm-0.1.0b1.dist-info/licenses/LICENSE +21 -0
- snakeorm/__init__.py +214 -0
- snakeorm/advisor.py +109 -0
- snakeorm/cli/__init__.py +3 -0
- snakeorm/cli/app.py +992 -0
- snakeorm/cli/discovery.py +179 -0
- snakeorm/cli/hooks.py +86 -0
- snakeorm/compiler/__init__.py +3 -0
- snakeorm/compiler/compiler.py +330 -0
- snakeorm/connection.py +228 -0
- snakeorm/contrib/__init__.py +17 -0
- snakeorm/contrib/asgi.py +192 -0
- snakeorm/contrib/config.py +264 -0
- snakeorm/contrib/deliver.py +256 -0
- snakeorm/contrib/django.py +245 -0
- snakeorm/contrib/sidecar.py +44 -0
- snakeorm/contrib/wsgi.py +176 -0
- snakeorm/core/__init__.py +1 -0
- snakeorm/core/config.py +173 -0
- snakeorm/core/converters.py +172 -0
- snakeorm/core/exceptions.py +175 -0
- snakeorm/core/placement.py +15 -0
- snakeorm/core/sentinels.py +37 -0
- snakeorm/core/signals.py +114 -0
- snakeorm/debug/__init__.py +42 -0
- snakeorm/debug/assets/css/panel.css +616 -0
- snakeorm/debug/assets/css/snake_orm_app.css +293 -0
- snakeorm/debug/assets/img/icon.svg +5 -0
- snakeorm/debug/assets/js/history.js +425 -0
- snakeorm/debug/assets/js/language.js +283 -0
- snakeorm/debug/assets/js/panel.js +241 -0
- snakeorm/debug/assets/js/snake_orm_app.js +82 -0
- snakeorm/debug/assets/pages/cli.html +28 -0
- snakeorm/debug/assets/pages/config.html +34 -0
- snakeorm/debug/assets/pages/database_first.html +29 -0
- snakeorm/debug/assets/pages/help.html +21 -0
- snakeorm/debug/assets/pages/history.html +27 -0
- snakeorm/debug/capture.py +234 -0
- snakeorm/debug/channel.py +131 -0
- snakeorm/debug/collector.py +144 -0
- snakeorm/debug/config.py +149 -0
- snakeorm/debug/html.py +578 -0
- snakeorm/debug/matrix.py +60 -0
- snakeorm/debug/origin.py +42 -0
- snakeorm/debug/otel/__init__.py +68 -0
- snakeorm/debug/otel/context.py +43 -0
- snakeorm/debug/otel/exporter.py +277 -0
- snakeorm/debug/otel/payload.py +97 -0
- snakeorm/debug/otel/report.py +29 -0
- snakeorm/debug/otel/spans.py +393 -0
- snakeorm/debug/otel/summary.py +59 -0
- snakeorm/debug/record.py +54 -0
- snakeorm/debug/report.py +311 -0
- snakeorm/debug/testing.py +23 -0
- snakeorm/decorators/__init__.py +13 -0
- snakeorm/decorators/abstract.py +26 -0
- snakeorm/decorators/db_first.py +104 -0
- snakeorm/decorators/function.py +28 -0
- snakeorm/decorators/model.py +268 -0
- snakeorm/decorators/polymorphic.py +140 -0
- snakeorm/decorators/result.py +167 -0
- snakeorm/decorators/row.py +90 -0
- snakeorm/decorators/trigger.py +47 -0
- snakeorm/decorators/view.py +149 -0
- snakeorm/dialects/__init__.py +6 -0
- snakeorm/dialects/base.py +373 -0
- snakeorm/dialects/capabilities.py +571 -0
- snakeorm/dialects/literals.py +46 -0
- snakeorm/dialects/matrix.py +384 -0
- snakeorm/dialects/mysql.py +519 -0
- snakeorm/dialects/postgres.py +504 -0
- snakeorm/dialects/sqlite.py +417 -0
- snakeorm/drivers/__init__.py +18 -0
- snakeorm/drivers/asyncbase.py +73 -0
- snakeorm/drivers/asyncdecorators.py +244 -0
- snakeorm/drivers/asyncpool.py +228 -0
- snakeorm/drivers/asyncpsycopg.py +124 -0
- snakeorm/drivers/asyncpymysql.py +50 -0
- snakeorm/drivers/asyncsqlite.py +30 -0
- snakeorm/drivers/base.py +76 -0
- snakeorm/drivers/failures.py +181 -0
- snakeorm/drivers/logging.py +167 -0
- snakeorm/drivers/pool.py +352 -0
- snakeorm/drivers/psycopg.py +188 -0
- snakeorm/drivers/pymysql.py +176 -0
- snakeorm/drivers/savepoints.py +46 -0
- snakeorm/drivers/sqlite.py +170 -0
- snakeorm/drivers/threaded.py +143 -0
- snakeorm/drivers/timeout.py +87 -0
- snakeorm/dto/__init__.py +55 -0
- snakeorm/dto/read.py +231 -0
- snakeorm/dto/region.py +326 -0
- snakeorm/dto/resolve.py +354 -0
- snakeorm/dto/spec.py +201 -0
- snakeorm/expressions/__init__.py +88 -0
- snakeorm/expressions/conditional.py +178 -0
- snakeorm/expressions/expression.py +810 -0
- snakeorm/expressions/functions.py +71 -0
- snakeorm/expressions/keys.py +299 -0
- snakeorm/expressions/paths.py +43 -0
- snakeorm/expressions/scalar.py +293 -0
- snakeorm/expressions/window.py +248 -0
- snakeorm/fields/__init__.py +68 -0
- snakeorm/fields/check.py +79 -0
- snakeorm/fields/column.py +316 -0
- snakeorm/fields/enum.py +64 -0
- snakeorm/fields/index.py +46 -0
- snakeorm/fields/relationship.py +738 -0
- snakeorm/fields/typed.py +736 -0
- snakeorm/helpers/__init__.py +1 -0
- snakeorm/helpers/annotations.py +21 -0
- snakeorm/helpers/inheritance.py +41 -0
- snakeorm/helpers/pyliteral.py +52 -0
- snakeorm/helpers/pytype.py +50 -0
- snakeorm/introspection/__init__.py +13 -0
- snakeorm/introspection/base.py +30 -0
- snakeorm/introspection/drift.py +78 -0
- snakeorm/introspection/models.py +633 -0
- snakeorm/introspection/mysql.py +367 -0
- snakeorm/introspection/postgres.py +397 -0
- snakeorm/introspection/sqlite.py +217 -0
- snakeorm/introspection/unsupported.py +94 -0
- snakeorm/linker/__init__.py +3 -0
- snakeorm/linker/linker.py +521 -0
- snakeorm/metadata/__init__.py +41 -0
- snakeorm/metadata/check.py +37 -0
- snakeorm/metadata/column.py +201 -0
- snakeorm/metadata/enum_storage.py +33 -0
- snakeorm/metadata/fk_action.py +19 -0
- snakeorm/metadata/foreign_key.py +26 -0
- snakeorm/metadata/index.py +43 -0
- snakeorm/metadata/index_method.py +19 -0
- snakeorm/metadata/int_size.py +20 -0
- snakeorm/metadata/json_storage.py +21 -0
- snakeorm/metadata/polymorphic.py +30 -0
- snakeorm/metadata/primary_key.py +23 -0
- snakeorm/metadata/relationship.py +60 -0
- snakeorm/metadata/relationship_kind.py +43 -0
- snakeorm/metadata/routine.py +26 -0
- snakeorm/metadata/server_default.py +20 -0
- snakeorm/metadata/table.py +97 -0
- snakeorm/metadata/table_kind.py +23 -0
- snakeorm/metadata/trigger.py +62 -0
- snakeorm/metadata/type_params.py +348 -0
- snakeorm/migration/__init__.py +88 -0
- snakeorm/migration/asyncrunner.py +168 -0
- snakeorm/migration/autodetect.py +96 -0
- snakeorm/migration/ddl.py +1172 -0
- snakeorm/migration/diff.py +701 -0
- snakeorm/migration/loader.py +77 -0
- snakeorm/migration/operations.py +952 -0
- snakeorm/migration/planner.py +78 -0
- snakeorm/migration/realize.py +420 -0
- snakeorm/migration/renames.py +153 -0
- snakeorm/migration/render.py +1095 -0
- snakeorm/migration/runner.py +314 -0
- snakeorm/migration/squash.py +60 -0
- snakeorm/migration/state.py +107 -0
- snakeorm/model.py +162 -0
- snakeorm/py.typed +0 -0
- snakeorm/query/__init__.py +9 -0
- snakeorm/query/compound.py +288 -0
- snakeorm/query/join_kind.py +19 -0
- snakeorm/query/joined.py +143 -0
- snakeorm/query/query.py +1001 -0
- snakeorm/query/recursive.py +244 -0
- snakeorm/registry/__init__.py +5 -0
- snakeorm/registry/by_module.py +163 -0
- snakeorm/registry/registry.py +166 -0
- snakeorm/session/__init__.py +8 -0
- snakeorm/session/asyncsession.py +638 -0
- snakeorm/session/coercion.py +306 -0
- snakeorm/session/factory.py +29 -0
- snakeorm/session/guards.py +269 -0
- snakeorm/session/isolation.py +23 -0
- snakeorm/session/mapper.py +213 -0
- snakeorm/session/planning.py +815 -0
- snakeorm/session/retry.py +96 -0
- snakeorm/session/session.py +871 -0
- snakeorm/session/shared.py +355 -0
- snakeorm/sql/__init__.py +18 -0
- snakeorm/sql/adapt.py +104 -0
- snakeorm/sql/aggregate.py +232 -0
- snakeorm/sql/condition.py +359 -0
- snakeorm/sql/delete.py +46 -0
- snakeorm/sql/insert.py +149 -0
- snakeorm/sql/joins.py +158 -0
- snakeorm/sql/pk_subquery.py +54 -0
- snakeorm/sql/refs.py +19 -0
- snakeorm/sql/resolver.py +28 -0
- snakeorm/sql/select.py +175 -0
- snakeorm/sql/update.py +82 -0
- snakeorm/sql/value.py +552 -0
- snakeorm/times.py +276 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Retrying a UNIT OF WORK in the face of a transient concurrency conflict."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import TypeVar
|
|
7
|
+
|
|
8
|
+
from snakeorm.session.session import SnakeSession
|
|
9
|
+
|
|
10
|
+
T = TypeVar("T")
|
|
11
|
+
|
|
12
|
+
# SQLSTATE class 40 ("transaction rollback"): conflicts the engine resolves by aborting the
|
|
13
|
+
# transaction, and which get resolved by repeating it (40001 serialisation, 40P01 deadlock). It is
|
|
14
|
+
# standard SQL, not Postgres jargon.
|
|
15
|
+
_TRANSIENT_SQLSTATE_CLASS = "40"
|
|
16
|
+
|
|
17
|
+
# Where each DBAPI puts the SQLSTATE. `pgcode` is psycopg2's name and `sqlstate` is psycopg3's, and
|
|
18
|
+
# consulting only the first is what made this function answer False for psycopg3 — which is this
|
|
19
|
+
# project's OWN async driver, so the retry never worked on the asynchronous path.
|
|
20
|
+
_SQLSTATE_ATTRIBUTES = ("sqlstate", "pgcode")
|
|
21
|
+
|
|
22
|
+
# Engines that do not report a SQLSTATE at all, and what they report instead. Declared with the
|
|
23
|
+
# reason rather than sniffed, so adding an engine is answering a question and not remembering one.
|
|
24
|
+
_MYSQL_TRANSIENT_ERRNOS = frozenset(
|
|
25
|
+
{
|
|
26
|
+
1213, # ER_LOCK_DEADLOCK: the server picked this transaction as the deadlock victim
|
|
27
|
+
1205, # ER_LOCK_WAIT_TIMEOUT: the lock did not come free in time; it may next time
|
|
28
|
+
}
|
|
29
|
+
)
|
|
30
|
+
"""MySQL reports its own errno in `args[0]`; pymysql exposes no SQLSTATE attribute."""
|
|
31
|
+
|
|
32
|
+
_SQLITE_TRANSIENT_MESSAGES = ("database is locked", "database table is locked")
|
|
33
|
+
"""SQLite has neither SQLSTATE nor a stable errno on `OperationalError`: the message IS the code.
|
|
34
|
+
|
|
35
|
+
Matching on a message is the thing this module's docstring says not to do, and it is done here on
|
|
36
|
+
purpose and only here, because SQLite offers nothing else. It is narrow —two exact phrases from the
|
|
37
|
+
C library, not a substring hunt— and it is the difference between a busy database being retried and
|
|
38
|
+
being raised at the caller.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def is_transient(error: BaseException) -> bool:
|
|
43
|
+
"""Whether the error is a concurrency conflict that makes sense to retry.
|
|
44
|
+
|
|
45
|
+
The SQLSTATE is what gets looked at where there is one, not the message nor the driver's class.
|
|
46
|
+
Syntax, constraint or network errors are NOT transient: retrying them repeats the failure (and a
|
|
47
|
+
constraint could duplicate side effects).
|
|
48
|
+
|
|
49
|
+
It reads every spelling the shipped drivers use, because it used to read exactly one. The tests
|
|
50
|
+
could not see that: they built a double carrying the attribute the code read, so the fixture was
|
|
51
|
+
shaped like the implementation and agreed with it by construction.
|
|
52
|
+
"""
|
|
53
|
+
for attribute in _SQLSTATE_ATTRIBUTES:
|
|
54
|
+
code = getattr(error, attribute, None)
|
|
55
|
+
if isinstance(code, str) and code.startswith(_TRANSIENT_SQLSTATE_CLASS):
|
|
56
|
+
return True
|
|
57
|
+
if error.args and isinstance(error.args[0], int):
|
|
58
|
+
return error.args[0] in _MYSQL_TRANSIENT_ERRNOS
|
|
59
|
+
return any(str(error) == message for message in _SQLITE_TRANSIENT_MESSAGES)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def with_retry(
|
|
63
|
+
session: SnakeSession,
|
|
64
|
+
work: Callable[[SnakeSession], T],
|
|
65
|
+
*,
|
|
66
|
+
attempts: int = 3,
|
|
67
|
+
) -> T:
|
|
68
|
+
"""Runs `work` and REPEATS it if the engine aborted the transaction over a transient conflict.
|
|
69
|
+
|
|
70
|
+
seat = with_retry(session, lambda s: reserve_seat(s, course_id))
|
|
71
|
+
|
|
72
|
+
It lives in the session, not in the driver: an abort renders the WHOLE transaction useless
|
|
73
|
+
("current transaction is aborted"), so the entire unit of work has to be redone along with its
|
|
74
|
+
`rollback` — not the statement. It goes hand in hand with
|
|
75
|
+
`set_isolation(REPEATABLE_READ | SERIALIZABLE)`, which abort on conflict. `work` must be
|
|
76
|
+
IDEMPOTENT with respect to external side effects (do not send the email inside it).
|
|
77
|
+
"""
|
|
78
|
+
if attempts < 1:
|
|
79
|
+
raise ValueError(f"attempts has to be at least 1; got {attempts}.")
|
|
80
|
+
|
|
81
|
+
last: BaseException | None = None
|
|
82
|
+
for attempt in range(1, attempts + 1):
|
|
83
|
+
try:
|
|
84
|
+
result = work(session)
|
|
85
|
+
session.commit()
|
|
86
|
+
except Exception as error:
|
|
87
|
+
session.rollback() # leaves the connection usable for the next attempt
|
|
88
|
+
if not is_transient(error):
|
|
89
|
+
raise # a real error does not get better by being repeated
|
|
90
|
+
last = error
|
|
91
|
+
continue
|
|
92
|
+
else:
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
assert last is not None # the loop is only left by exhausting transient attempts
|
|
96
|
+
raise last
|