xtr-orm 6.8.0__tar.gz → 7.0.0__tar.gz
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.
- xtr_orm-6.8.0/README.md → xtr_orm-7.0.0/PKG-INFO +81 -5
- xtr_orm-6.8.0/PKG-INFO → xtr_orm-7.0.0/README.md +52 -37
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/pyproject.toml +21 -23
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/pyproject.toml.orig +23 -23
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/.agents/skills/xtr-orm/SKILL.md +12 -2
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/.agents/skills/xtr-orm/references/messenger.md +2 -0
- xtr_orm-7.0.0/src/xtr_orm/bundle/_profiler.py +81 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/bundle/connection_config.py +10 -2
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/bundle/orm_bundle.py +68 -27
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/run_sql_command.py +5 -2
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/connection_registry.py +6 -8
- xtr_orm-7.0.0/src/xtr_orm/data_collector/__init__.py +16 -0
- xtr_orm-7.0.0/src/xtr_orm/data_collector/_masking.py +48 -0
- xtr_orm-7.0.0/src/xtr_orm/data_collector/orm_data_collector.py +70 -0
- xtr_orm-7.0.0/src/xtr_orm/data_collector/query_listener.py +179 -0
- xtr_orm-7.0.0/src/xtr_orm/data_collector/recorded_query.py +50 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/messenger/__init__.py +2 -1
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/messenger/close_connection_middleware.py +20 -5
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/messenger/open_transaction_logger_middleware.py +47 -12
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/messenger/transaction_middleware.py +64 -9
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/_environment.py +2 -2
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/_revisions.py +1 -1
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/migrator.py +2 -3
- xtr_orm-7.0.0/src/xtr_orm/stickiness_participant.py +52 -0
- xtr_orm-7.0.0/src/xtr_orm/templates/collector/orm.html.j2 +225 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/LICENSE +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/.agents/skills/xtr-orm/references/commands.md +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/.agents/skills/xtr-orm/references/connections.md +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/.agents/skills/xtr-orm/references/migrations.md +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/bundle/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/bundle/orm_config.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/bundle/orm_mappings_pass.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/_reporting.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/connection_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/database_create_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/database_drop_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_current_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_diff_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_dump_schema_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_execute_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_generate_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_latest_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_list_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_migrate_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_rollup_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_status_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_up_to_date_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/command/migrations_version_command.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/database/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/database/database_manager.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/invalid_argument_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/migration_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/missing_database_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/orm_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/session_unavailable_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/unknown_connection_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/exception/unsupported_database_error.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/mapping_driver_interface.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/messenger_transport.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/_history.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/_schema.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/available_migration.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/direction.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/executed_migration.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/execution_result.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/migration_plan.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/migration_status.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/migrations_config.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/migrations/template/script.py.mako +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/py.typed +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/recipe/__init__.py +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/recipe/files/config/orm.py.tmpl +0 -0
- {xtr_orm-6.8.0 → xtr_orm-7.0.0}/src/xtr_orm/recipe/manifest.toml +0 -0
|
@@ -1,10 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: xtr-orm
|
|
3
|
+
Version: 7.0.0
|
|
4
|
+
Summary: Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection.
|
|
5
|
+
Keywords: orm,database,migrations,sqlalchemy,alembic,asyncio
|
|
6
|
+
Author: Xterr
|
|
7
|
+
Author-email: Xterr <me@xterr.dev>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Framework :: AsyncIO
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Topic :: Database
|
|
15
|
+
Classifier: Typing :: Typed
|
|
16
|
+
Requires-Dist: advanced-alchemy>=1.11
|
|
17
|
+
Requires-Dist: alembic>=1.20
|
|
18
|
+
Requires-Dist: sqlalchemy[asyncio]>=2.0.52
|
|
19
|
+
Requires-Dist: xtr-clock>=7.0,<8
|
|
20
|
+
Requires-Dist: xtr-logging-contracts>=7.0,<8
|
|
21
|
+
Requires-Dist: asyncmy>=0.2.12 ; extra == 'mysql'
|
|
22
|
+
Requires-Dist: asyncpg>=0.31 ; extra == 'postgres'
|
|
23
|
+
Requires-Dist: aiosqlite>=0.20 ; extra == 'sqlite'
|
|
24
|
+
Requires-Python: >=3.14.4
|
|
25
|
+
Provides-Extra: mysql
|
|
26
|
+
Provides-Extra: postgres
|
|
27
|
+
Provides-Extra: sqlite
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
1
30
|
<div align="center">
|
|
2
31
|
|
|
3
32
|
# xtr-orm
|
|
4
33
|
|
|
5
34
|
**Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection.**
|
|
6
35
|
|
|
7
|
-
<img alt="python 3.
|
|
36
|
+
<img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
|
|
8
37
|
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
9
38
|
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
10
39
|
|
|
@@ -50,6 +79,12 @@ uv add "xtr-messenger[orm]" # beside it: the orm:// tr
|
|
|
50
79
|
|---|---|---|
|
|
51
80
|
| `postgres` / `mysql` / `sqlite` | `asyncpg` / `asyncmy` / `aiosqlite` | The async driver of your database |
|
|
52
81
|
|
|
82
|
+
SQLite is for development and tests, not production. Its Python driver runs in a legacy
|
|
83
|
+
transaction mode by default: it begins no transaction for a read or a `SAVEPOINT`, so an outer
|
|
84
|
+
rollback does not undo a savepoint that was already released. The driver's own setting gives
|
|
85
|
+
it real transactions — pass it through the engine options:
|
|
86
|
+
`engine_options={"connect_args": {"autocommit": False}}`.
|
|
87
|
+
|
|
53
88
|
No xtr package is an extra, and the container is not a dependency: the `OrmBundle` — engines,
|
|
54
89
|
sessions, migrators from one configuration — is used when
|
|
55
90
|
[xtr-dependency-injection](../xtr-dependency-injection) is installed beside the package, and a
|
|
@@ -162,6 +197,21 @@ before the version and history tables are written, and gives up the transaction
|
|
|
162
197
|
revision would roll back; set `transaction_per_migration=True` with it, and keep it for the
|
|
163
198
|
revisions that need it (`CREATE INDEX CONCURRENTLY`).
|
|
164
199
|
|
|
200
|
+
## Profiler panel
|
|
201
|
+
|
|
202
|
+
When the [http kernel](../xtr-http-kernel)'s profiler is on — the web profiler turns it on in
|
|
203
|
+
development — the bundle registers an `orm` panel recording every statement a profiled request
|
|
204
|
+
runs, on every connection and on each connection's replicas too: whether it came from a session,
|
|
205
|
+
a connection or a migration. For each statement the panel shows the connection, the SQL, its
|
|
206
|
+
parameters (a value named like a credential — a password, a secret, a token — as `******`),
|
|
207
|
+
whether it ran once per row, how long the driver took, the rows it reports affected and the
|
|
208
|
+
error, if it failed; and, for the request, how many statements ran, their total time, how many
|
|
209
|
+
failed and which ran more than once — the usual sign of a query issued per row.
|
|
210
|
+
|
|
211
|
+
With the profiler off, nothing is registered and no engine is listened to: it costs nothing. An
|
|
212
|
+
unprofiled request on a profiling application costs one context lookup per statement and keeps
|
|
213
|
+
nothing.
|
|
214
|
+
|
|
165
215
|
## Use in an application
|
|
166
216
|
|
|
167
217
|
Everything adding this package to an application on
|
|
@@ -308,6 +358,11 @@ Flags read `1`/`true`/`yes`/`on` and `0`/`false`/`no`/`off`; numbers are numbers
|
|
|
308
358
|
takes `debug`. A value that does not read fails the service that needed the URL, naming the
|
|
309
359
|
option. Only the connection's own URL is read this way, not its replicas'.
|
|
310
360
|
|
|
361
|
+
Outside debug (`kernel.debug` false), `hide_parameters` defaults to on: an error raised by a
|
|
362
|
+
failed statement leaves its bound values out of its message and the logs — an email, a token.
|
|
363
|
+
Set it in the engine options or the URL (`?hide_parameters=false`) to see them. A connection's
|
|
364
|
+
`repr` hides the password of every URL and every value of `connect_args`.
|
|
365
|
+
|
|
311
366
|
### Read replicas
|
|
312
367
|
|
|
313
368
|
With `replicas`, a session reads from a replica picked at random, and goes to the primary —
|
|
@@ -315,6 +370,8 @@ With `replicas`, a session reads from a replica picked at random, and goes to th
|
|
|
315
370
|
sees what it wrote. It stays there for the rest of its unit of work, unless `keep_replica`
|
|
316
371
|
sends it back to the replicas once it commits. Each unit of work starts on the replicas,
|
|
317
372
|
whatever the one before wrote. Migrations and the database commands always use the primary.
|
|
373
|
+
The bundle's `StickinessParticipant` keeps that per unit: a unit's writes stick only that unit to
|
|
374
|
+
the primary, and a child task's writes do not stick its parent, whose context it only copied.
|
|
318
375
|
|
|
319
376
|
A block can be sent elsewhere with advanced-alchemy's `primary_context()` and
|
|
320
377
|
`replica_context()`, from `advanced_alchemy.routing`.
|
|
@@ -404,7 +461,7 @@ commands that change the database ask first; `-n` answers yes.
|
|
|
404
461
|
|---|---|
|
|
405
462
|
| `orm:database:create [--if-not-exists]` | Creates the database, through the server's maintenance database |
|
|
406
463
|
| `orm:database:drop --force [--if-exists]` | Drops it; refused without `--force` |
|
|
407
|
-
| `orm:run-sql SQL [--force-fetch]` | Runs a statement, committed; shows its rows or how many it changed — when the driver can tell |
|
|
464
|
+
| `orm:run-sql SQL [--force-fetch]` | Runs a statement as written, committed — a `:word` is never read as a parameter; shows its rows or how many it changed — when the driver can tell |
|
|
408
465
|
| `orm:migrations:current` | The revisions the database is at — `base` for none |
|
|
409
466
|
| `orm:migrations:latest` | The revisions no other follows |
|
|
410
467
|
| `orm:migrations:list` | Every revision with its status, when it ran and how long it took |
|
|
@@ -471,7 +528,7 @@ MessageBusConfig(
|
|
|
471
528
|
|---|---|---|---|
|
|
472
529
|
| `orm_transaction` | `TransactionMiddleware` | Runs every handler of a message in one transaction, committed once they all succeeded and rolled back otherwise; a failure drops the other handlers' handled stamps, so a retry runs them all again | `connection_name`, the default connection when left out |
|
|
473
530
|
| `orm_close_connection` | `CloseConnectionMiddleware` | Closes the connections once a worker handled a message it consumed, so an idle worker holds none — once the last, when it handles several at once | `connection_names`, every connection in use — its engine built — when left out |
|
|
474
|
-
| `orm_open_transaction_logger` | `OpenTransactionLoggerMiddleware` | Logs an error — to the application's logger when the logging bundle is active — when a handler began a transaction — `begin()`, `begin_nested()` — and left it open, in a message that was handled | `connection_names`, every connection in use — its engine built — when left out |
|
|
531
|
+
| `orm_open_transaction_logger` | `OpenTransactionLoggerMiddleware` | Logs an error — to the application's logger when the logging bundle is active — when a handler began a transaction — `begin()`, `begin_nested()` — and left it open, in a message that was handled; the record names the connections, the message's `message_type` and its transport `message_id`, never its body | `connection_names`, every connection in use — its engine built — when left out |
|
|
475
532
|
|
|
476
533
|
- **One session per message.** The messenger bundle makes every message a unit of work, and a
|
|
477
534
|
scoped service — the session, a repository built on it — is the unit's: every handler of
|
|
@@ -479,6 +536,15 @@ MessageBusConfig(
|
|
|
479
536
|
dispatched while another is handled joins its unit, and its transaction — in a savepoint of
|
|
480
537
|
its own, so when it fails only its handlers' work is rolled back, and the handler that
|
|
481
538
|
dispatched it decides whether the rest goes on.
|
|
539
|
+
- **The caller's transaction stays the caller's.** A message dispatched by code that already
|
|
540
|
+
has a transaction open on the session — a route that wrote, then dispatched over `sync://`
|
|
541
|
+
— runs in a savepoint of it: a failing message leaves the caller's work in place, and a
|
|
542
|
+
succeeding one commits nothing, so the caller still commits (or rolls back) its own work. A
|
|
543
|
+
message a worker received owns its transaction, whatever an earlier middleware read.
|
|
544
|
+
- **Dispatch one message at a time per unit.** Messages handled at once by tasks sharing the
|
|
545
|
+
unit's session (`asyncio.gather` of dispatches) interleave on its one transaction, and one
|
|
546
|
+
message's rollback can undo another's work; give concurrent work units of its own
|
|
547
|
+
(`unit_of_work(join=False)`).
|
|
482
548
|
- **Handlers do not commit** under `orm_transaction`; a handler that does ends the
|
|
483
549
|
transaction early, and one using `async with session.begin()` is refused, the transaction
|
|
484
550
|
being open already.
|
|
@@ -488,6 +554,10 @@ MessageBusConfig(
|
|
|
488
554
|
- **Only messages a worker consumed are closed after** — one carrying a `ReceivedStamp` on
|
|
489
555
|
arrival. A message handled during its dispatch, `sync://` included, closes nothing. A worker
|
|
490
556
|
handling several at once closes once the last of them is done, keeping its pool meanwhile.
|
|
557
|
+
The unit's session on each connection is closed first, so a message that read and never
|
|
558
|
+
committed gives its connection back before the pool is dropped. A worker consuming an
|
|
559
|
+
`orm://` transport on the same connection reconnects for every poll after that: name only the
|
|
560
|
+
other connections in `connection_names` there.
|
|
491
561
|
- **Follow-ups wait for the commit when stamped.** A message a handler dispatches to a
|
|
492
562
|
transport is sent at once — before the commit, and whether or not it comes. Dispatch it
|
|
493
563
|
with `DispatchAfterCurrentBusStamp` and it goes out only once the message being handled
|
|
@@ -568,8 +638,9 @@ Every error derives from `OrmError`.
|
|
|
568
638
|
## Known limitations
|
|
569
639
|
|
|
570
640
|
- **Migrations take no lock.** Two processes migrating one database at once — two deploys
|
|
571
|
-
starting together —
|
|
572
|
-
|
|
641
|
+
starting together — race: on PostgreSQL the loser fails on recording a revision already
|
|
642
|
+
recorded and rolls its transaction back; elsewhere a revision may run twice. Run
|
|
643
|
+
`orm:migrations:migrate` from one place, before the application starts.
|
|
573
644
|
- **Applied times follow the database.** `executed_at` has a time zone on PostgreSQL and is
|
|
574
645
|
naive elsewhere: UTC on SQLite, the session's time zone on MySQL.
|
|
575
646
|
- **advanced-alchemy's model registry is process-wide.** Its `metadata_registry` maps a
|
|
@@ -580,6 +651,11 @@ Every error derives from `OrmError`.
|
|
|
580
651
|
table records every one.
|
|
581
652
|
- **Async only**: engines and sessions are async; there is no sync counterpart.
|
|
582
653
|
|
|
654
|
+
## Upgrading
|
|
655
|
+
|
|
656
|
+
What changes for an application moving to 7.0, and how to keep the old behaviour where that is
|
|
657
|
+
still possible: [Upgrading to 7.0](../../UPGRADING.md#xtr-orm).
|
|
658
|
+
|
|
583
659
|
## Development
|
|
584
660
|
|
|
585
661
|
Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
|
|
@@ -1,42 +1,10 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: xtr-orm
|
|
3
|
-
Version: 6.8.0
|
|
4
|
-
Summary: Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection.
|
|
5
|
-
Keywords: orm,database,migrations,sqlalchemy,alembic,asyncio
|
|
6
|
-
Author: Xterr
|
|
7
|
-
Author-email: Xterr <me@xterr.dev>
|
|
8
|
-
License-Expression: MIT
|
|
9
|
-
License-File: LICENSE
|
|
10
|
-
Classifier: Development Status :: 3 - Alpha
|
|
11
|
-
Classifier: Framework :: AsyncIO
|
|
12
|
-
Classifier: Intended Audience :: Developers
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
-
Classifier: Topic :: Database
|
|
18
|
-
Classifier: Typing :: Typed
|
|
19
|
-
Requires-Dist: advanced-alchemy>=1.11
|
|
20
|
-
Requires-Dist: alembic>=1.20
|
|
21
|
-
Requires-Dist: sqlalchemy[asyncio]>=2.0.41
|
|
22
|
-
Requires-Dist: typing-extensions>=4.4
|
|
23
|
-
Requires-Dist: xtr-logging-contracts>=6.0,<7
|
|
24
|
-
Requires-Dist: asyncmy>=0.2.9 ; extra == 'mysql'
|
|
25
|
-
Requires-Dist: asyncpg>=0.29 ; extra == 'postgres'
|
|
26
|
-
Requires-Dist: aiosqlite>=0.20 ; extra == 'sqlite'
|
|
27
|
-
Requires-Python: >=3.11
|
|
28
|
-
Provides-Extra: mysql
|
|
29
|
-
Provides-Extra: postgres
|
|
30
|
-
Provides-Extra: sqlite
|
|
31
|
-
Description-Content-Type: text/markdown
|
|
32
|
-
|
|
33
1
|
<div align="center">
|
|
34
2
|
|
|
35
3
|
# xtr-orm
|
|
36
4
|
|
|
37
5
|
**Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection.**
|
|
38
6
|
|
|
39
|
-
<img alt="python 3.
|
|
7
|
+
<img alt="python 3.14+" src="https://img.shields.io/badge/python-%E2%89%A5%203.14-3776AB?logo=python&logoColor=white">
|
|
40
8
|
<img alt="typed" src="https://img.shields.io/badge/typed-ty%20%2B%20basedpyright-1f6feb">
|
|
41
9
|
<img alt="license MIT" src="https://img.shields.io/badge/license-MIT-blue">
|
|
42
10
|
|
|
@@ -82,6 +50,12 @@ uv add "xtr-messenger[orm]" # beside it: the orm:// tr
|
|
|
82
50
|
|---|---|---|
|
|
83
51
|
| `postgres` / `mysql` / `sqlite` | `asyncpg` / `asyncmy` / `aiosqlite` | The async driver of your database |
|
|
84
52
|
|
|
53
|
+
SQLite is for development and tests, not production. Its Python driver runs in a legacy
|
|
54
|
+
transaction mode by default: it begins no transaction for a read or a `SAVEPOINT`, so an outer
|
|
55
|
+
rollback does not undo a savepoint that was already released. The driver's own setting gives
|
|
56
|
+
it real transactions — pass it through the engine options:
|
|
57
|
+
`engine_options={"connect_args": {"autocommit": False}}`.
|
|
58
|
+
|
|
85
59
|
No xtr package is an extra, and the container is not a dependency: the `OrmBundle` — engines,
|
|
86
60
|
sessions, migrators from one configuration — is used when
|
|
87
61
|
[xtr-dependency-injection](../xtr-dependency-injection) is installed beside the package, and a
|
|
@@ -194,6 +168,21 @@ before the version and history tables are written, and gives up the transaction
|
|
|
194
168
|
revision would roll back; set `transaction_per_migration=True` with it, and keep it for the
|
|
195
169
|
revisions that need it (`CREATE INDEX CONCURRENTLY`).
|
|
196
170
|
|
|
171
|
+
## Profiler panel
|
|
172
|
+
|
|
173
|
+
When the [http kernel](../xtr-http-kernel)'s profiler is on — the web profiler turns it on in
|
|
174
|
+
development — the bundle registers an `orm` panel recording every statement a profiled request
|
|
175
|
+
runs, on every connection and on each connection's replicas too: whether it came from a session,
|
|
176
|
+
a connection or a migration. For each statement the panel shows the connection, the SQL, its
|
|
177
|
+
parameters (a value named like a credential — a password, a secret, a token — as `******`),
|
|
178
|
+
whether it ran once per row, how long the driver took, the rows it reports affected and the
|
|
179
|
+
error, if it failed; and, for the request, how many statements ran, their total time, how many
|
|
180
|
+
failed and which ran more than once — the usual sign of a query issued per row.
|
|
181
|
+
|
|
182
|
+
With the profiler off, nothing is registered and no engine is listened to: it costs nothing. An
|
|
183
|
+
unprofiled request on a profiling application costs one context lookup per statement and keeps
|
|
184
|
+
nothing.
|
|
185
|
+
|
|
197
186
|
## Use in an application
|
|
198
187
|
|
|
199
188
|
Everything adding this package to an application on
|
|
@@ -340,6 +329,11 @@ Flags read `1`/`true`/`yes`/`on` and `0`/`false`/`no`/`off`; numbers are numbers
|
|
|
340
329
|
takes `debug`. A value that does not read fails the service that needed the URL, naming the
|
|
341
330
|
option. Only the connection's own URL is read this way, not its replicas'.
|
|
342
331
|
|
|
332
|
+
Outside debug (`kernel.debug` false), `hide_parameters` defaults to on: an error raised by a
|
|
333
|
+
failed statement leaves its bound values out of its message and the logs — an email, a token.
|
|
334
|
+
Set it in the engine options or the URL (`?hide_parameters=false`) to see them. A connection's
|
|
335
|
+
`repr` hides the password of every URL and every value of `connect_args`.
|
|
336
|
+
|
|
343
337
|
### Read replicas
|
|
344
338
|
|
|
345
339
|
With `replicas`, a session reads from a replica picked at random, and goes to the primary —
|
|
@@ -347,6 +341,8 @@ With `replicas`, a session reads from a replica picked at random, and goes to th
|
|
|
347
341
|
sees what it wrote. It stays there for the rest of its unit of work, unless `keep_replica`
|
|
348
342
|
sends it back to the replicas once it commits. Each unit of work starts on the replicas,
|
|
349
343
|
whatever the one before wrote. Migrations and the database commands always use the primary.
|
|
344
|
+
The bundle's `StickinessParticipant` keeps that per unit: a unit's writes stick only that unit to
|
|
345
|
+
the primary, and a child task's writes do not stick its parent, whose context it only copied.
|
|
350
346
|
|
|
351
347
|
A block can be sent elsewhere with advanced-alchemy's `primary_context()` and
|
|
352
348
|
`replica_context()`, from `advanced_alchemy.routing`.
|
|
@@ -436,7 +432,7 @@ commands that change the database ask first; `-n` answers yes.
|
|
|
436
432
|
|---|---|
|
|
437
433
|
| `orm:database:create [--if-not-exists]` | Creates the database, through the server's maintenance database |
|
|
438
434
|
| `orm:database:drop --force [--if-exists]` | Drops it; refused without `--force` |
|
|
439
|
-
| `orm:run-sql SQL [--force-fetch]` | Runs a statement, committed; shows its rows or how many it changed — when the driver can tell |
|
|
435
|
+
| `orm:run-sql SQL [--force-fetch]` | Runs a statement as written, committed — a `:word` is never read as a parameter; shows its rows or how many it changed — when the driver can tell |
|
|
440
436
|
| `orm:migrations:current` | The revisions the database is at — `base` for none |
|
|
441
437
|
| `orm:migrations:latest` | The revisions no other follows |
|
|
442
438
|
| `orm:migrations:list` | Every revision with its status, when it ran and how long it took |
|
|
@@ -503,7 +499,7 @@ MessageBusConfig(
|
|
|
503
499
|
|---|---|---|---|
|
|
504
500
|
| `orm_transaction` | `TransactionMiddleware` | Runs every handler of a message in one transaction, committed once they all succeeded and rolled back otherwise; a failure drops the other handlers' handled stamps, so a retry runs them all again | `connection_name`, the default connection when left out |
|
|
505
501
|
| `orm_close_connection` | `CloseConnectionMiddleware` | Closes the connections once a worker handled a message it consumed, so an idle worker holds none — once the last, when it handles several at once | `connection_names`, every connection in use — its engine built — when left out |
|
|
506
|
-
| `orm_open_transaction_logger` | `OpenTransactionLoggerMiddleware` | Logs an error — to the application's logger when the logging bundle is active — when a handler began a transaction — `begin()`, `begin_nested()` — and left it open, in a message that was handled | `connection_names`, every connection in use — its engine built — when left out |
|
|
502
|
+
| `orm_open_transaction_logger` | `OpenTransactionLoggerMiddleware` | Logs an error — to the application's logger when the logging bundle is active — when a handler began a transaction — `begin()`, `begin_nested()` — and left it open, in a message that was handled; the record names the connections, the message's `message_type` and its transport `message_id`, never its body | `connection_names`, every connection in use — its engine built — when left out |
|
|
507
503
|
|
|
508
504
|
- **One session per message.** The messenger bundle makes every message a unit of work, and a
|
|
509
505
|
scoped service — the session, a repository built on it — is the unit's: every handler of
|
|
@@ -511,6 +507,15 @@ MessageBusConfig(
|
|
|
511
507
|
dispatched while another is handled joins its unit, and its transaction — in a savepoint of
|
|
512
508
|
its own, so when it fails only its handlers' work is rolled back, and the handler that
|
|
513
509
|
dispatched it decides whether the rest goes on.
|
|
510
|
+
- **The caller's transaction stays the caller's.** A message dispatched by code that already
|
|
511
|
+
has a transaction open on the session — a route that wrote, then dispatched over `sync://`
|
|
512
|
+
— runs in a savepoint of it: a failing message leaves the caller's work in place, and a
|
|
513
|
+
succeeding one commits nothing, so the caller still commits (or rolls back) its own work. A
|
|
514
|
+
message a worker received owns its transaction, whatever an earlier middleware read.
|
|
515
|
+
- **Dispatch one message at a time per unit.** Messages handled at once by tasks sharing the
|
|
516
|
+
unit's session (`asyncio.gather` of dispatches) interleave on its one transaction, and one
|
|
517
|
+
message's rollback can undo another's work; give concurrent work units of its own
|
|
518
|
+
(`unit_of_work(join=False)`).
|
|
514
519
|
- **Handlers do not commit** under `orm_transaction`; a handler that does ends the
|
|
515
520
|
transaction early, and one using `async with session.begin()` is refused, the transaction
|
|
516
521
|
being open already.
|
|
@@ -520,6 +525,10 @@ MessageBusConfig(
|
|
|
520
525
|
- **Only messages a worker consumed are closed after** — one carrying a `ReceivedStamp` on
|
|
521
526
|
arrival. A message handled during its dispatch, `sync://` included, closes nothing. A worker
|
|
522
527
|
handling several at once closes once the last of them is done, keeping its pool meanwhile.
|
|
528
|
+
The unit's session on each connection is closed first, so a message that read and never
|
|
529
|
+
committed gives its connection back before the pool is dropped. A worker consuming an
|
|
530
|
+
`orm://` transport on the same connection reconnects for every poll after that: name only the
|
|
531
|
+
other connections in `connection_names` there.
|
|
523
532
|
- **Follow-ups wait for the commit when stamped.** A message a handler dispatches to a
|
|
524
533
|
transport is sent at once — before the commit, and whether or not it comes. Dispatch it
|
|
525
534
|
with `DispatchAfterCurrentBusStamp` and it goes out only once the message being handled
|
|
@@ -600,8 +609,9 @@ Every error derives from `OrmError`.
|
|
|
600
609
|
## Known limitations
|
|
601
610
|
|
|
602
611
|
- **Migrations take no lock.** Two processes migrating one database at once — two deploys
|
|
603
|
-
starting together —
|
|
604
|
-
|
|
612
|
+
starting together — race: on PostgreSQL the loser fails on recording a revision already
|
|
613
|
+
recorded and rolls its transaction back; elsewhere a revision may run twice. Run
|
|
614
|
+
`orm:migrations:migrate` from one place, before the application starts.
|
|
605
615
|
- **Applied times follow the database.** `executed_at` has a time zone on PostgreSQL and is
|
|
606
616
|
naive elsewhere: UTC on SQLite, the session's time zone on MySQL.
|
|
607
617
|
- **advanced-alchemy's model registry is process-wide.** Its `metadata_registry` maps a
|
|
@@ -612,6 +622,11 @@ Every error derives from `OrmError`.
|
|
|
612
622
|
table records every one.
|
|
613
623
|
- **Async only**: engines and sessions are async; there is no sync counterpart.
|
|
614
624
|
|
|
625
|
+
## Upgrading
|
|
626
|
+
|
|
627
|
+
What changes for an application moving to 7.0, and how to keep the old behaviour where that is
|
|
628
|
+
still possible: [Upgrading to 7.0](../../UPGRADING.md#xtr-orm).
|
|
629
|
+
|
|
615
630
|
## Development
|
|
616
631
|
|
|
617
632
|
Developed in the [python-xtr](https://github.com/xterr/python-xtr) monorepo, under
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "xtr-orm"
|
|
3
|
-
version = "
|
|
3
|
+
version = "7.0.0"
|
|
4
4
|
description = "Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection."
|
|
5
5
|
readme = "README.md"
|
|
6
|
-
requires-python = ">=3.
|
|
6
|
+
requires-python = ">=3.14.4"
|
|
7
7
|
license = "MIT"
|
|
8
8
|
license-files = ["LICENSE"]
|
|
9
9
|
keywords = [
|
|
@@ -18,9 +18,6 @@ classifiers = [
|
|
|
18
18
|
"Development Status :: 3 - Alpha",
|
|
19
19
|
"Framework :: AsyncIO",
|
|
20
20
|
"Intended Audience :: Developers",
|
|
21
|
-
"Programming Language :: Python :: 3.11",
|
|
22
|
-
"Programming Language :: Python :: 3.12",
|
|
23
|
-
"Programming Language :: Python :: 3.13",
|
|
24
21
|
"Programming Language :: Python :: 3.14",
|
|
25
22
|
"Topic :: Database",
|
|
26
23
|
"Typing :: Typed",
|
|
@@ -28,9 +25,9 @@ classifiers = [
|
|
|
28
25
|
dependencies = [
|
|
29
26
|
"advanced-alchemy>=1.11",
|
|
30
27
|
"alembic>=1.20",
|
|
31
|
-
"sqlalchemy[asyncio]>=2.0.
|
|
32
|
-
"
|
|
33
|
-
"xtr-logging-contracts>=
|
|
28
|
+
"sqlalchemy[asyncio]>=2.0.52",
|
|
29
|
+
"xtr-clock>=7.0,<8",
|
|
30
|
+
"xtr-logging-contracts>=7.0,<8",
|
|
34
31
|
]
|
|
35
32
|
|
|
36
33
|
[[project.authors]]
|
|
@@ -38,9 +35,9 @@ name = "Xterr"
|
|
|
38
35
|
email = "me@xterr.dev"
|
|
39
36
|
|
|
40
37
|
[project.optional-dependencies]
|
|
41
|
-
postgres = ["asyncpg>=0.
|
|
38
|
+
postgres = ["asyncpg>=0.31"]
|
|
42
39
|
sqlite = ["aiosqlite>=0.20"]
|
|
43
|
-
mysql = ["asyncmy>=0.2.
|
|
40
|
+
mysql = ["asyncmy>=0.2.12"]
|
|
44
41
|
|
|
45
42
|
[project.entry-points."xtr_dependency_injection.bundles"]
|
|
46
43
|
orm = "xtr_orm.bundle:OrmBundle"
|
|
@@ -50,19 +47,20 @@ orm = "xtr_orm.recipe"
|
|
|
50
47
|
|
|
51
48
|
[dependency-groups]
|
|
52
49
|
dev = [
|
|
53
|
-
"basedpyright>=1.
|
|
54
|
-
"ruff>=0.
|
|
55
|
-
"pytest>=8",
|
|
56
|
-
"pytest-cov>=
|
|
57
|
-
"anyio>=4.
|
|
50
|
+
"basedpyright>=1.31.7",
|
|
51
|
+
"ruff>=0.14.7",
|
|
52
|
+
"pytest>=8.4",
|
|
53
|
+
"pytest-cov>=7.1",
|
|
54
|
+
"anyio>=4.10",
|
|
58
55
|
"ty>=0.0.83",
|
|
56
|
+
"xtr-http-kernel>=7.0,<8",
|
|
59
57
|
"aiosqlite>=0.20",
|
|
60
|
-
"xtr-console>=
|
|
61
|
-
"xtr-dependency-injection>=
|
|
62
|
-
"xtr-logging>=
|
|
63
|
-
"xtr-messenger>=
|
|
64
|
-
"xtr-orm-messenger>=
|
|
65
|
-
"xtr-service-contracts>=
|
|
58
|
+
"xtr-console>=7.0,<8",
|
|
59
|
+
"xtr-dependency-injection>=7.0,<8",
|
|
60
|
+
"xtr-logging>=7.0,<8",
|
|
61
|
+
"xtr-messenger>=7.0,<8",
|
|
62
|
+
"xtr-orm-messenger>=7.0,<8",
|
|
63
|
+
"xtr-service-contracts>=7.0,<8",
|
|
66
64
|
]
|
|
67
65
|
|
|
68
66
|
[build-system]
|
|
@@ -71,7 +69,7 @@ build-backend = "uv_build"
|
|
|
71
69
|
|
|
72
70
|
[tool.basedpyright]
|
|
73
71
|
typeCheckingMode = "all"
|
|
74
|
-
pythonVersion = "3.
|
|
72
|
+
pythonVersion = "3.14"
|
|
75
73
|
pythonPlatform = "All"
|
|
76
74
|
include = [
|
|
77
75
|
"src",
|
|
@@ -97,7 +95,7 @@ extraPaths = ["."]
|
|
|
97
95
|
reportPrivateUsage = "none"
|
|
98
96
|
|
|
99
97
|
[tool.ruff]
|
|
100
|
-
target-version = "
|
|
98
|
+
target-version = "py314"
|
|
101
99
|
line-length = 100
|
|
102
100
|
src = [
|
|
103
101
|
"src",
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "xtr-orm"
|
|
3
|
-
version = "
|
|
3
|
+
version = "7.0.0"
|
|
4
4
|
description = "Databases for async applications: engines, sessions and versioned schema migrations, configured once per connection."
|
|
5
5
|
readme = "README.md"
|
|
6
|
-
requires-python = ">=3.
|
|
6
|
+
requires-python = ">=3.14.4"
|
|
7
7
|
license = "MIT"
|
|
8
8
|
license-files = ["LICENSE"]
|
|
9
9
|
authors = [
|
|
@@ -14,9 +14,6 @@ classifiers = [
|
|
|
14
14
|
"Development Status :: 3 - Alpha",
|
|
15
15
|
"Framework :: AsyncIO",
|
|
16
16
|
"Intended Audience :: Developers",
|
|
17
|
-
"Programming Language :: Python :: 3.11",
|
|
18
|
-
"Programming Language :: Python :: 3.12",
|
|
19
|
-
"Programming Language :: Python :: 3.13",
|
|
20
17
|
"Programming Language :: Python :: 3.14",
|
|
21
18
|
"Topic :: Database",
|
|
22
19
|
"Typing :: Typed",
|
|
@@ -24,20 +21,21 @@ classifiers = [
|
|
|
24
21
|
dependencies = [
|
|
25
22
|
"advanced-alchemy>=1.11",
|
|
26
23
|
"alembic>=1.20",
|
|
27
|
-
"sqlalchemy[asyncio]>=2.0.
|
|
28
|
-
|
|
29
|
-
"xtr-
|
|
24
|
+
"sqlalchemy[asyncio]>=2.0.52",
|
|
25
|
+
# The profiler panel times each statement on its monotonic clock.
|
|
26
|
+
"xtr-clock>=7.0,<8",
|
|
27
|
+
"xtr-logging-contracts>=7.0,<8",
|
|
30
28
|
]
|
|
31
29
|
|
|
32
30
|
[project.optional-dependencies]
|
|
33
31
|
postgres = [
|
|
34
|
-
"asyncpg>=0.
|
|
32
|
+
"asyncpg>=0.31",
|
|
35
33
|
]
|
|
36
34
|
sqlite = [
|
|
37
35
|
"aiosqlite>=0.20",
|
|
38
36
|
]
|
|
39
37
|
mysql = [
|
|
40
|
-
"asyncmy>=0.2.
|
|
38
|
+
"asyncmy>=0.2.12",
|
|
41
39
|
]
|
|
42
40
|
|
|
43
41
|
# Advertises the bundle so debug:bundles can name it when installed but not listed.
|
|
@@ -51,20 +49,22 @@ orm = "xtr_orm.recipe"
|
|
|
51
49
|
|
|
52
50
|
[dependency-groups]
|
|
53
51
|
dev = [
|
|
54
|
-
"basedpyright>=1.
|
|
55
|
-
"ruff>=0.
|
|
56
|
-
"pytest>=8",
|
|
57
|
-
"pytest-cov>=
|
|
58
|
-
"anyio>=4.
|
|
52
|
+
"basedpyright>=1.31.7",
|
|
53
|
+
"ruff>=0.14.7",
|
|
54
|
+
"pytest>=8.4",
|
|
55
|
+
"pytest-cov>=7.1",
|
|
56
|
+
"anyio>=4.10",
|
|
59
57
|
"ty>=0.0.83",
|
|
58
|
+
# The profiler panel's collector, tested against the http kernel's profiler.
|
|
59
|
+
"xtr-http-kernel>=7.0,<8",
|
|
60
60
|
# Every test runs on SQLite files under the test's own directory: no server.
|
|
61
61
|
"aiosqlite>=0.20",
|
|
62
|
-
"xtr-console>=
|
|
63
|
-
"xtr-dependency-injection>=
|
|
64
|
-
"xtr-logging>=
|
|
65
|
-
"xtr-messenger>=
|
|
66
|
-
"xtr-orm-messenger>=
|
|
67
|
-
"xtr-service-contracts>=
|
|
62
|
+
"xtr-console>=7.0,<8",
|
|
63
|
+
"xtr-dependency-injection>=7.0,<8",
|
|
64
|
+
"xtr-logging>=7.0,<8",
|
|
65
|
+
"xtr-messenger>=7.0,<8",
|
|
66
|
+
"xtr-orm-messenger>=7.0,<8",
|
|
67
|
+
"xtr-service-contracts>=7.0,<8",
|
|
68
68
|
]
|
|
69
69
|
|
|
70
70
|
[build-system]
|
|
@@ -76,7 +76,7 @@ build-backend = "uv_build"
|
|
|
76
76
|
# ─────────────────────────────────────────────────────────────────
|
|
77
77
|
[tool.basedpyright]
|
|
78
78
|
typeCheckingMode = "all"
|
|
79
|
-
pythonVersion = "3.
|
|
79
|
+
pythonVersion = "3.14"
|
|
80
80
|
pythonPlatform = "All"
|
|
81
81
|
include = ["src", "tests"]
|
|
82
82
|
exclude = ["**/__pycache__", "**/.venv", "**/build", "**/dist", ".tmp"]
|
|
@@ -102,7 +102,7 @@ reportPrivateUsage = "none"
|
|
|
102
102
|
# ruff — select = ["ALL"], minimal justified ignores
|
|
103
103
|
# ─────────────────────────────────────────────────────────────────
|
|
104
104
|
[tool.ruff]
|
|
105
|
-
target-version = "
|
|
105
|
+
target-version = "py314"
|
|
106
106
|
line-length = 100
|
|
107
107
|
src = ["src", "tests"]
|
|
108
108
|
# The revision template is rendered into application code, not run here.
|
|
@@ -20,6 +20,8 @@ connection's name. Everything is async; there is no sync counterpart.
|
|
|
20
20
|
- Write a revision from your models: `orm:migrations:diff --message "add invoices"`. Apply:
|
|
21
21
|
`orm:migrations:migrate`. Read it: `orm:migrations:status`.
|
|
22
22
|
- Without a container: `Migrator(engine, MigrationsConfig(directory="migrations"), Base.metadata)`.
|
|
23
|
+
- With the http kernel's profiler on (the web profiler turns it on in dev), every statement of a
|
|
24
|
+
profiled request shows in the `orm` panel — nothing to configure; with it off, nothing is added.
|
|
23
25
|
- In an application, with xtr-dependency-injection installed beside the package (it is not a
|
|
24
26
|
dependency): activate `OrmBundle` and inject what it registers. Only `xtr_orm.bundle` needs the
|
|
25
27
|
container.
|
|
@@ -28,6 +30,10 @@ connection's name. Everything is async; there is no sync counterpart.
|
|
|
28
30
|
- One transaction per message: list `orm_transaction` in the bus's `middleware`; handlers never
|
|
29
31
|
commit.
|
|
30
32
|
- On SQLite, set `MigrationsConfig(render_as_batch=True)` or table alterations cannot be written.
|
|
33
|
+
- SQLite is for development and tests only. For real transactions there (savepoints undone by an
|
|
34
|
+
outer rollback), pass `engine_options={"connect_args": {"autocommit": False}}`.
|
|
35
|
+
- Outside debug `hide_parameters` is on: SQL errors leave bound values out. `?hide_parameters=false`
|
|
36
|
+
shows them.
|
|
31
37
|
|
|
32
38
|
## Inject a session
|
|
33
39
|
|
|
@@ -128,10 +134,9 @@ keeps an `OrmMappingsPass`, registers it in `build`, and hands it the driver it
|
|
|
128
134
|
migrations use, so `orm:migrations:diff` writes them like the application's own models.
|
|
129
135
|
|
|
130
136
|
```python
|
|
131
|
-
from typing import final
|
|
137
|
+
from typing import final, override
|
|
132
138
|
|
|
133
139
|
from sqlalchemy import Column, Integer, MetaData, Table
|
|
134
|
-
from typing_extensions import override
|
|
135
140
|
from xtr_dependency_injection import (
|
|
136
141
|
Bundle,
|
|
137
142
|
ContainerBuilder,
|
|
@@ -308,6 +313,11 @@ A handler asks for `Injected[AsyncSession]` like any service and adds rows; it n
|
|
|
308
313
|
|
|
309
314
|
- Every handler of a message shares one session per connection. A message dispatched while
|
|
310
315
|
another is handled joins its unit of work, in a savepoint of its own.
|
|
316
|
+
- A message dispatched by code holding an open transaction (a route that wrote, then dispatched)
|
|
317
|
+
runs in a savepoint of it and commits nothing of the caller's: the caller still commits its
|
|
318
|
+
own work. A message a worker received owns its transaction.
|
|
319
|
+
- Never `asyncio.gather` dispatches that share one unit's session; give concurrent work its own
|
|
320
|
+
`unit_of_work(container, join=False)`.
|
|
311
321
|
- A follow-up dispatched to a transport goes out at once unless stamped:
|
|
312
322
|
`await bus.dispatch(SendReceipt(order.id), DispatchAfterCurrentBusStamp())` sends it only after
|
|
313
323
|
the commit, and never after a rollback.
|
|
@@ -28,6 +28,8 @@ MessageBusConfig(
|
|
|
28
28
|
```
|
|
29
29
|
|
|
30
30
|
- `orm_close_connection` outermost, so it closes after the transaction ended.
|
|
31
|
+
It closes the unit's session on each connection before dropping its pool; when the worker
|
|
32
|
+
consumes `orm://` on one of them, list only the other connections in `connection_names`.
|
|
31
33
|
- `orm_open_transaction_logger` inside `orm_transaction`, so it looks before the commit closes
|
|
32
34
|
everything.
|
|
33
35
|
- A second `orm_transaction` with another `connection_name` puts that connection in a transaction
|