Repository navigation
Commit ff72d1c
committed
fix(prisma): Record SQL queries in the call which made them
The context for Prisma's query events was kept in a single module-level
variable, overwritten by every model call, so with concurrent calls all
the queries were recorded in whichever call started last.
Each request, and each interactive transaction, now runs in a context of
its own, like an instrumented async function, which replaces
Recording.reserve(). Prisma 7 runs queries in JavaScript through driver
adapters and emits query events in that context, so they are recorded
where they belong. Batched requests (findUnique calls made at the same
time, a $transaction array) run in the context of the first one, so
their queries go to the closest context enclosing the batch.
The Rust engine of earlier versions emits query events out of any
context, with nothing telling which request made them. A query then
goes to the client's request in progress or, if there are several, to
the closest context enclosing them all. This is a best guess: the events
can arrive after their request is done, once another has started.
Sequential code is reliable in practice.
Raw queries get a context too, so their SQL is now recorded where they
were made instead of at the top level. The return event is emitted right
away and filled in later, as for other async functions; with
async_tracking_timeout set to 0, the SQL now follows the call instead of
being nested in it.
Query logging is now turned on through the public log option. Setting
the engine config after construction did nothing on Prisma 7, so no
SQL was recorded from query events there. When the adapter's driver is
one we hook (pg, better-sqlite3), that hook records the queries with
more detail, including failed ones, which Prisma doesn't report. The
adapter passed to the client is wrapped so that its calls, and those of
the connections and transactions it makes, run in a context naming the
client; once a driver hook records a query in such a call, the client's
query events are ignored. This also covers the driverAdapters preview
feature of earlier versions. (Such a hook places batched queries in the
batch's first call.)
test/prisma7 covers Prisma 7 with a libsql adapter (query events) and
a better-sqlite3 one (driver hook). Prisma 7 needs Node 20.19+, so the
tests are skipped below that and its preinstall check is disabled so
that installing still works on Node 18. better-sqlite3 is held at 11.x
for the adapter, as 12 doesn't support Node 18.
Assisted-by: Claude:claude-opus-5-51 parent 3c72fad commit ff72d1c
14 files changed
Lines changed: 4291 additions & 209 deletions
File tree
- src
- hooks
- test
- __snapshots__
- prisma7
- prisma
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
101 | 106 | | |
| 107 | + | |
102 | 108 | | |
103 | 109 | | |
104 | 110 | | |
| |||
122 | 128 | | |
123 | 129 | | |
124 | 130 | | |
| 131 | + | |
125 | 132 | | |
126 | 133 | | |
127 | 134 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
112 | 115 | | |
| 116 | + | |
113 | 117 | | |
114 | 118 | | |
115 | 119 | | |
| |||
376 | 380 | | |
377 | 381 | | |
378 | 382 | | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | 383 | | |
389 | 384 | | |
390 | 385 | | |
| |||
Large diffs are not rendered by default.
0 commit comments