Skip to content

Commit 645ede5

Browse files
refactor(driver-sql): move the transaction doors out of sql-driver.ts into sql-driver-transactions.ts (#25521 cut 3) (#25655)
Part of #25521 Clause-②: no Metadata-protocol: none Ruling-ref: 6270097840 Ruling-ref: 6348891160 Cut 3 of the `sql-driver.ts` split (#24586 ruling A, recorded in `6270097840`). The claim is `6384369018`. Six `SqlDriver` method bodies move unedited into a new module, `packages/drivers/driver-sql/src/sql-driver-transactions.ts`: the public `beginTransaction`, `commit` and `rollback`, the deprecated aliases `commitTransaction` and `rollbackTransaction`, and the protected SQLite single-connection guard `assertBareKnexSafe`. `releaseTransaction` stays in the class whole. It is move-only, and the card stays open for the later cuts. Branch `claude/issue-25521-sql-driver-cut-3`, cut from `origin/main` at `451bf61d0` (cut 2 merged as `b694e56e9` before it). **Serial with the file's in-flight PRs.** At claim the PM read no open PR touching `sql-driver.ts`, `driver-turso/src` or `driver-sqlite-wasm/src`. Re-measured at my start (18 open PRs) and again just before this PR was opened (16 open; #25632 had merged and #25619 had left the list), each PR's file list read in full. No PR touches `packages/drivers/driver-sql/**`, `packages/drivers/driver-turso/**` or `packages/drivers/driver-sqlite-wasm/**`, except the Version Packages PR #25640, which touches only those packages' `CHANGELOG.md` and `package.json` (its 183 files read page by page). `origin/main` moved one commit past the base meanwhile (#25632, now `49653f918`). It touches only `packages/spec` and its tests, none of the three driver packages and no gate script this diff derives, so `main` is not merged in. ## Seam table (fresh, at `451bf61d0`) `sql-driver.ts` is 21,651 lines. 30-day lines are `git blame --line-porcelain` lines whose committer time is on or after 2026-09-24T12:16:02Z, 30 days before the base commit. The clone's history floor is 2026-09-15, and `scripts/pm/git-history.mjs count` proves 69 commits on the file in that window (20 in the last 7 days). In all, 3,882 lines (17.9%) are from the last 30 days. The class `SqlDriver` spans lines 5487-21576 with 347 members: 251 methods (49 public), 32 statics. No public method has an inferred return type; the one inferred public member is the getter `supports`, in the class head. Sections are the class's own banner comments, cut by contiguous line range. "Public" counts public methods, plus public accessors where there are any: | Lines | Section | Lines | 30-day lines | Members (public) | |:--|:--|--:|--:|:--| | 1-5486 | Module preamble: imports, types, top-level helpers (199 statements) | 5,486 | 1,416 | - | | 5487-7078 | Class head: fields, statics, constructor, capability getters (`supports`) | 1,592 | 290 | 88 (1 + 2 accessors) | | 7079-7510 | Lifecycle | 432 | 155 | 15 (3) | | 7511-10189 | CRUD (IDataDriver core) | 2,679 | 691 | 45 (7) | | 10190-10801 | Bulk and batch operations | 612 | 140 | 9 (6) | | 10802-10898 | Raw execution (cut 2's three stubs and the `runtimeCreatedIndexes` field) | 97 | 28 | 4 (1) | | **10899-10976** | **Transactions (this cut, less `releaseTransaction`)** | **78** | **0** | **7 (5)** | | 10977-11508 | Aggregation | 532 | 61 | 5 (1) | | 11509-11796 | Distinct | 288 | 20 | 3 (1) | | 11797-11912 | Window functions | 116 | 4 | 1 (1) | | 11913-12011 | Query plan analysis | 99 | 1 | 2 (2) | | 12012-15766 | Schema sync (syncSchema / init) | 3,755 | 373 | 62 (14 + 3 accessors) | | 15767-15902 | Schema introspection (`listPhysicalColumns`, `introspectSchema`) | 136 | 65 | 2 (2) | | 15903-21576 | Internal helpers, including cut 1's five introspection stubs | 5,674 | 638 | 104 (5) | | 21577-21651 | After the class: `UniqueConstraintMember`, `singleColumnUniqueColumns` | 75 | 0 | - | The limits per area are cut 1's (PR #25614), re-read on this tree: - **`getBuilder` builders.** Aggregation, Distinct, Window functions, Query plan analysis, CRUD and Bulk build through `getBuilder`, so a cut of any of them must first carry the new module into `check:tenant-chokepoint`'s `SCAN_FILES`, or the gate goes silently narrower. None of them is cut here. - **Protected statics.** The hash-shadow family and the row-size explainers (Internal helpers) read protected statics, so those bodies stay. - **`this.FIELD.delete/insert/update(`.** Transactions has one, `releaseTransaction` (`this.openTransactions.delete(key)`), and Schema sync has three. Those bodies stay. The six bodies moved here have none: `beginTransaction` calls `.add(` on `openTransactions`, and the other five read no field method at all. - `insertOnlyUpsertColumns` stays in `sql-driver.ts`, because #8740's restart line names that symbol in that file. This cut does not touch it. **Proposed cut 4: Schema introspection** (15767-15902, 136 lines, 65 from the last 30 days): the public `listPhysicalColumns` and `introspectSchema`, both with annotated return types. Neither builds through `getBuilder` (the word appears only in a docblock), neither reads a static, and neither has a `this.FIELD.delete/insert/update(` site. It is warmer than this cut, and its dev should price three things first: the two module-level helpers the bodies call (`suggestFieldTypeForSqlType`, `postgresTableNameCollisionError`), which may need cut 2's leading-`export` shape; `TursoDriver`'s overrides of both members (the `'remote'` and `'refused'` rows of its door table); and whether `check:tenant-chokepoint`'s raw-fragment read covers the `knex.raw(` calls in those bodies, since a move out of `SCAN_FILES` would narrow it. Every cold area left after that builds through `getBuilder`. ## Why this area for cut 3 Cut 2 proposed it, and it re-measures clean: 78 lines, none from the last 30 days. All five public members (`beginTransaction`, `commit`, `rollback`, `commitTransaction`, `rollbackTransaction`) have annotated return types. The section reads no static and no `getBuilder`. Its one `this.FIELD.delete(` site is `releaseTransaction`, which stays. Between them, the six moved bodies read `knex`, `openTransactions`, `activeTransactions`, `isSqlite`, `isProductionEnv`, `releaseTransaction`, `commit` and `rollback`, and nothing else. They read no module-level declaration of `sql-driver.ts`, so unlike cut 2 no `export` was added. **Overrides and spies, measured before the move.** - `TursoDriver` overrides `beginTransaction`, `commit` and `rollback`, and each override calls `super` after its remote-mode refusal. Its door table lists `commitTransaction` and `rollbackTransaction` as `'inherited'`, because they call `this.commit` / `this.rollback`, which refuse. So both aliases must keep reaching the subclass's `commit` / `rollback`, and they do: they reach them through late-bound closures. - `SqliteWasmDriver` overrides the getter `isSqlite`, which `assertBareKnexSafe` reads. The host reads it through a getter that reads the driver, so the override is the one the guard sees. - `vi.spyOn(driver, 'beginTransaction')` in `service-storage` and `plugin-auth` tests spies on the class method, which is still there. - A scratch probe against the built `dist` checked every moved member's late binding at base and at head: the counter and the open set after `beginTransaction`, an instance spy on `isProductionEnv` (reached by `assertBareKnexSafe`), on `releaseTransaction` (reached by `commit`), on `commit` and `rollback` (reached by the aliases), a subclass override of `isSqlite`, a subclass `commit` that refuses (reached by `commitTransaction`), and a rejecting `commit` that still releases. Its 14 readings are identical at base and head. The probe is not committed. ## The shape (#23050 shape B, as cuts 1 and 2 apply it) - The class keeps each method with its name, signature, docblock and position, because tsup emits members with their JSDoc into the `.d.ts`. Each body is now one host literal plus `fn.call(host, ...)`, marked `[#25521 cut 3]`. The five `async` methods drop `async` and return the moved function's promise. `assertBareKnexSafe` was never `async`. - `TransactionHost` is new. It types every member as the class member itself (an indexed access on `SqlDriver`). Each moved function types its `this` as the `Pick` of the host over exactly the members its body reads. The generator measures those reads from the AST (`this.NAME` property accesses) and asserts them, so the string `this.knex.` inside the guard's error text is not counted as a read. Each stub annotates its literal as the `ThisParameterType` of the moved function. There is no cast, and no member becomes public. - `knex`, `openTransactions`, `activeTransactions` and the getter `isSqlite` go as getters, so each read happens when the body makes it. `commit`, `rollback`, `releaseTransaction` and `isProductionEnv` go as late-bound closures. - **One field is written.** `beginTransaction` runs `this.activeTransactions++`, so its literal carries a setter beside the getter, which writes the field on the driver. The generator measures writes (`++`, `--` and assignment on `this.NAME`) and asserts them, and the interface marks `activeTransactions` as the one non-`readonly` field. This is the accessor pair `auth-manager.ts` uses for `jwtSigningFailureReported` (#25224 cut 6). Both fields stay on the class: `releaseTransaction`, `ensureSequencesTable`'s guard call and the tests read them. - `this.knex.transaction()` keeps its receiver: the getter hands back the class's knex, and `.transaction` is called on it. - The move was produced by a script from the source text, not retyped: each method's parameter list, return type and body lines, re-indented two columns. The guard's error message is five single-line template literals joined by `+`, so no line continues a template literal. ## The published `.d.ts` does not change Under the series bar (ruling `6348891160`, letter A), measured on base `451bf61d0` and on head `59d63a8d1`. Both were built against the same dependency tree: `turbo run build --filter=@objectstack/driver-turso... --filter=@objectstack/driver-sqlite-wasm...` at base, 9/9 tasks (driver-sql a cache miss). Then `pnpm --filter @objectstack/driver-sql build` at head. - This package emits `dist/index.d.ts` and `dist/index.d.mts` (there is no `.d.cts`). The two are identical to each other at both ends. - Raw sha256 of both files, base and head, all four the same: `93da45168e82136b7613944525e47402d5dc6f9f8daa9efa4bdceeaa11b52a89` (cuts 1 and 2's hash, unchanged). The raw diff is 0 lines. - Normalised with the ruling's one-liner verbatim (double-quoted unions), base = head = `93da45168e82136b7613944525e47402d5dc6f9f8daa9efa4bdceeaa11b52a89`. This file has 0 double-quoted unions, so that normaliser rewrites nothing. - Normalised with the same one-liner, with the quote character swapped to a single quote (31 single-quoted unions match), base = head = `118e992ad8aad590044eb31509dca65e6b321e3410ce846e1e5f5dd34b956e95`. With a raw diff of 0 lines, the proof rests on raw identity, and neither normaliser is relied on. - Positive control on a copy of the head `dist/index.d.ts`: line 4282, `protected assertBareKnexSafe(op: string): void;`, made public. That gives raw and double-quote-normalised `f492eb1c19c6832b1e13aee2f02596041b34c0252971720a0472a266c44eb7cc` and single-quote-normalised `8337de653b61ff24a59c29934ce1783051ba7c056523aa2e700107e9c69c6216`, each different from the real hash. - Export tables: the ESM and CJS bundles each export the same 56 names at base and head, loaded at runtime (`import()` and `require` of the built bundles). The sorted list hashes to `d61a9dd3a02661602b1bb2dbe18152ac43986148f36b2746f40c0884bd8ffdec` on all four. - The head build carries the move: `dist/index.mjs` and `dist/index.js` each contain 14 host `.call(` sites (8 at base, plus these 6). No changeset: `skip-changeset`, per ruling `6348891160` item 4. ## Proof of move-only - **Bodies, by AST token** (recursive `getChildren` leaves). Each moved function's parameters (less the added `this`), return type and body have the same token sequence as the base method, and `async` matches on all six. 218 tokens compared, 0 differ. - **Lines**: 38 lines compared, from each method's name to its closing brace. Each one is the base line with two leading spaces removed (the first line, less the added `this` parameter, is byte-identical). - **Negative control**: changing one character inside a template literal of `assertBareKnexSafe` in a copy of the module makes the check report 1 token and 1 line different, and it exits `PROOF FAILED`, exit 1. - **Class side**: 347 members at base and head. The 341 members that did not move are byte-identical to base, leading trivia included. Each of the 6 stubs keeps its docblock and signature byte-identical, less `async` on the five that had it. - **Top level of `sql-driver.ts`**: all 201 base statements other than the class are byte-identical, in order. The one new statement is the import of the new module. - **Size**: +177 / -31 over 2 files, far under the 3,000-line manual-merge line. `sql-driver.ts` goes from 21,651 to 21,678 lines, and `sql-driver-transactions.ts` has 119. See the acceptance notes on why the file grows. ## Readers of `sql-driver.ts` by path, checked against the moved text None needed an edit. Each was read against the moved region (10899-10976) and run at both ends where it is a gate or test: - **ADR anchors.** `scripts/adr-anchors/packages__drivers__driver-sql__src__sql-driver.ts.json` carries ADR-0053 (`formatOutput`, `presentReadValue`) and ADR-0120 D4. Neither paragraph describes a moved body, so per card ask 3 the new module gets no entry of its own. The moved bodies cite no ADR. `check:adr-anchors`: 85 anchored files at base and head. Decision-letter citations on the anchored surface: 1,849 at base and 1,849 at head. - **Symbol anchors.** ADR-0119 anchors `packages/drivers/driver-sql/src/sql-driver.ts#beginTransaction`. The stub keeps that declaration in `sql-driver.ts`, so the anchor still resolves. No other anchor names a moved member. `check:adr-symbol-anchors`: 2,384 anchors resolve at both ends. - **`check:tenant-chokepoint`** reads `sql-driver.ts` for `getBuilder` bindings and for `IS NULL` arms in raw fragments. The moved text has neither, so the gate still covers the same population: 24 `getBuilder()` bindings across 3 files at both ends. - **Logger receivers.** The moved text reads no logger. `check:logger-receiver-detach`: 0 detaches at both ends (3,681 files walked at head, one more than base: the new module). - **Other source scans**, none of whose patterns occurs in the moved text: `logger-receiver-detach.test.ts` ③, `sql-driver-compile-refusal-seam.test.ts`, the five `packages/cli` `generate-*.pin.test.ts` files, `check-keyed-text-bounds` (`varcharColumnChars`), `check-object-def-param-keys` (the `initObjects` parameter keys). - **Path lists and comment citations** that name the file (`turbo.json`, `scripts/cross-package-test-inputs.mjs`, `scripts/pm/check-half-states.mjs`, `scripts/check-changeset-no-major.mjs`, `packages/spec/liveness/*.json` evidence, prose citations in `objectql`, `spec`, `driver-turso`). None names a moved member. - **`driver-turso`** and **`driver-sqlite-wasm`**: their readers of moved members reach the stubs unchanged. Both suites ran at both ends (below). - `check:driver-conformance` at base and head: 55 covered, 0 in the DEBT ledger, 0 exempt, both times. ## Verification - `tsc --noEmit` (driver-sql) exit 0. `eslint --no-inline-config` on both source files exit 0. - `packages/drivers/driver-sql` suite: base 3,999 tests (3,787 passed, 0 failed, 212 skipped; 247 files), head the same. Compared per test name and status from vitest's JSON reporter: no test lost, none changed status, none added. - `packages/drivers/driver-turso` suite, run against the rebuilt head `dist`: base 2,526 tests (2,492 passed, 0 failed, 34 skipped; 95 files), head the same. Per test name and status there is no difference (2,513 distinct names; 13 names repeat). - `packages/drivers/driver-sqlite-wasm` suite, against the rebuilt head `dist`: base 704 tests (704 passed; 39 files), head the same, per test name no difference. - `dispatch-gates --commands` on this diff, at head `59d63a8d1`: 55 derived commands, each run with its exit code. `--ran` reconciles 55 derived, 54 run, 1 NOT MEASURED, 0 unrun. The NOT MEASURED one is `check:dual-build-cjs-loads` (exit 3, PREREQUISITE NOT MET: it needs every package's `dist`), which CI runs. `check:lean-entry-closure` ran after `objectql`'s closure was built. - No reverse verification or ablation: the diff changes no tested behaviour. ## Acceptance notes - **The file grows by 27 lines.** The six bodies are short (the two deprecated aliases are one line each), and each stub's host literal is longer than the body it replaced. The cut moves the transaction logic into its own module rather than shrinking the file. Every remaining cut is larger than this one. - `assertBareKnexSafe` now builds its host literal before the production early return. Its docblock calls the guard zero-overhead on the hot path. Its one caller is `ensureSequencesTable`'s lazy fallback (a write that reaches the sequences table before `initObjects` created it), and only when the runner is the bare `knex`, so this is not a hot path today. The docblock is unchanged, because this cut is move-only. - `TursoDriver`'s three overrides still call `super`, which now returns the moved function's promise instead of an `async` method's promise. A caller awaits the same settlement. - ADR-0119 still says driver-sql implements `beginTransaction` at `sql-driver.ts#beginTransaction`. That declaration is still there, and the body is one module over. No `docs/adr/**` edit is made here. --- _Generated by [Claude Code](https://claude.ai/code/session_01JVTd3PprStGa26aez5omLB)_ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5aafc82 commit 645ede5

2 files changed

Lines changed: 177 additions & 31 deletions

File tree

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#25521 cut 3] The transaction doors of `SqlDriver`: `beginTransaction`,
5+
* `commit`, `rollback`, their deprecated aliases `commitTransaction` and
6+
* `rollbackTransaction`, and the SQLite single-connection guard
7+
* `assertBareKnexSafe`. They moved out of `sql-driver.ts` whole.
8+
*
9+
* A MOVE, not a redesign. Each function below is the body of the `SqlDriver`
10+
* method of the same name as it stood, re-indented one level and otherwise
11+
* unedited: the same open-transaction count, the same release on every close,
12+
* the same guard.
13+
*
14+
* `releaseTransaction` did NOT move. Its body deletes from the
15+
* `openTransactions` set (`this.openTransactions.delete(`), and the series
16+
* keeps a body with such a site in the class. `commit` and `rollback` reach it
17+
* through the host, like any other member.
18+
*
19+
* ## How the moved bodies reach the class
20+
*
21+
* The shape `sql-driver-introspection.ts` (cut 1) and
22+
* `sql-driver-raw-execution.ts` (cut 2) apply, after #23050. The class keeps
23+
* every method, because the published `dist/*.d.ts` lists each one's name,
24+
* docblock and position. `TursoDriver` overrides `beginTransaction`, `commit`
25+
* and `rollback`, and each override calls `super`. Each body is now one
26+
* literal of exactly the {@link TransactionHost} members that method reads,
27+
* plus a `.call`. The five `async` methods are no longer `async`: each returns
28+
* the moved function's own promise, so a caller awaits the same promise the
29+
* body settles, with no extra wrapper.
30+
*
31+
* `commit`, `rollback`, `releaseTransaction` and `isProductionEnv` go as
32+
* closures that call them on the driver at call time. So a subclass override
33+
* is still the one a body reaches: the deprecated `commitTransaction` reaches
34+
* `TursoDriver`'s refusing `commit`, as it did when it called `this.commit`.
35+
* A test spy on the instance is reached the same way.
36+
*
37+
* The fields `knex`, `openTransactions` and `activeTransactions`, and the
38+
* getter `isSqlite`, go as GETTERS, so each read happens when the body makes
39+
* it, as it did on the class (`SqliteWasmDriver` overrides `isSqlite`).
40+
* `beginTransaction` also WRITES `activeTransactions` (`++`), so its literal
41+
* carries a setter beside the getter, which writes the field on the driver.
42+
* Both fields stay on the class: `releaseTransaction` and the tests read them.
43+
*
44+
* The bodies read no module-level declaration of `sql-driver.ts`, so nothing
45+
* gained an `export` there. `index.ts` re-exports nothing from this module, so
46+
* the published surface does not move.
47+
*/
48+
49+
import type { Knex } from 'knex';
50+
import type { SqlDriver } from './sql-driver.js';
51+
52+
/**
53+
* The `SqlDriver` members the functions below read — every one of them, and
54+
* no other; each function's `this` is the `Pick` of the members IT reads. Each
55+
* is typed AS the class member (an indexed access on the class), so a change
56+
* to a member's signature on the class is a change here too, and naming a
57+
* member the class does not have is a compile error. See the module header
58+
* for which members are getters, and for the one that is also written.
59+
*/
60+
export interface TransactionHost {
61+
activeTransactions: SqlDriver['activeTransactions'];
62+
commit: SqlDriver['commit'];
63+
isProductionEnv: SqlDriver['isProductionEnv'];
64+
readonly isSqlite: SqlDriver['isSqlite'];
65+
readonly knex: SqlDriver['knex'];
66+
readonly openTransactions: SqlDriver['openTransactions'];
67+
releaseTransaction: SqlDriver['releaseTransaction'];
68+
rollback: SqlDriver['rollback'];
69+
}
70+
71+
/** The body of `SqlDriver#beginTransaction`; the method's docblock there is its contract. */
72+
export async function beginTransaction(this: Pick<TransactionHost, 'activeTransactions' | 'knex' | 'openTransactions'>): Promise<Knex.Transaction> {
73+
const trx = await this.knex.transaction();
74+
this.openTransactions.add(trx as unknown as object);
75+
this.activeTransactions++;
76+
return trx;
77+
}
78+
79+
/** The body of `SqlDriver#assertBareKnexSafe`; the method's docblock there is its contract. */
80+
export function assertBareKnexSafe(this: Pick<TransactionHost, 'activeTransactions' | 'isProductionEnv' | 'isSqlite'>, op: string): void {
81+
if (this.isProductionEnv()) return;
82+
if (!this.isSqlite) return;
83+
if (this.activeTransactions === 0) return;
84+
throw new Error(
85+
`[driver-sql] refusing to run '${op}' on a fresh pooled connection while a ` +
86+
`transaction is open: SQLite's pool has a single connection, so acquiring a ` +
87+
`second one would dead-lock (surfacing later as "Knex: Timeout acquiring a ` +
88+
`connection"). Bind this operation to the active transaction instead of using ` +
89+
`this.knex.`,
90+
);
91+
}
92+
93+
/** The body of `SqlDriver#commit`; the method's docblock there is its contract. */
94+
export async function commit(this: Pick<TransactionHost, 'releaseTransaction'>, transaction: unknown): Promise<void> {
95+
try {
96+
await (transaction as Knex.Transaction).commit();
97+
} finally {
98+
this.releaseTransaction(transaction);
99+
}
100+
}
101+
102+
/** The body of `SqlDriver#rollback`; the method's docblock there is its contract. */
103+
export async function rollback(this: Pick<TransactionHost, 'releaseTransaction'>, transaction: unknown): Promise<void> {
104+
try {
105+
await (transaction as Knex.Transaction).rollback();
106+
} finally {
107+
this.releaseTransaction(transaction);
108+
}
109+
}
110+
111+
/** The body of `SqlDriver#commitTransaction`; the method's docblock there is its contract. */
112+
export async function commitTransaction(this: Pick<TransactionHost, 'commit'>, trx: Knex.Transaction): Promise<void> {
113+
await this.commit(trx);
114+
}
115+
116+
/** The body of `SqlDriver#rollbackTransaction`; the method's docblock there is its contract. */
117+
export async function rollbackTransaction(this: Pick<TransactionHost, 'rollback'>, trx: Knex.Transaction): Promise<void> {
118+
await this.rollback(trx);
119+
}

‎packages/drivers/driver-sql/src/sql-driver.ts‎

Lines changed: 58 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ import {
175175
// index DDL `execute` ran); the methods of the same names below hand each body
176176
// the members it reads (`RawExecutionHost`).
177177
import { execute, noteRuntimeIndexDdl, rawStatementFault } from './sql-driver-raw-execution.js';
178+
// [#25521 cut 3] The transaction doors (`beginTransaction`, `commit`, `rollback`,
179+
// their deprecated aliases, and the SQLite single-connection guard
180+
// `assertBareKnexSafe`); the methods of the same names below hand each body the
181+
// members it reads (`TransactionHost`).
182+
import {
183+
assertBareKnexSafe,
184+
beginTransaction,
185+
commit,
186+
commitTransaction,
187+
rollback,
188+
rollbackTransaction,
189+
} from './sql-driver-transactions.js';
178190
import { makeDatabaseDirectory, probeAbsentSqliteDatabaseFile, SqliteDatabaseFileError } from './sqlite-database-file.js';
179191
import { jsonMembershipPredicate } from '@objectstack/core';
180192
import knex, { Knex } from 'knex';
@@ -10900,11 +10912,19 @@ export class SqlDriver implements IDataDriver {
1090010912
// Transactions
1090110913
// ===================================
1090210914

10903-
async beginTransaction(): Promise<Knex.Transaction> {
10904-
const trx = await this.knex.transaction();
10905-
this.openTransactions.add(trx as unknown as object);
10906-
this.activeTransactions++;
10907-
return trx;
10915+
beginTransaction(): Promise<Knex.Transaction> {
10916+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10917+
const readActiveTransactions = () => this.activeTransactions;
10918+
const writeActiveTransactions = (v: SqlDriver['activeTransactions']) => { this.activeTransactions = v; };
10919+
const readKnex = () => this.knex;
10920+
const readOpenTransactions = () => this.openTransactions;
10921+
const host: ThisParameterType<typeof beginTransaction> = {
10922+
get activeTransactions() { return readActiveTransactions(); },
10923+
set activeTransactions(v) { writeActiveTransactions(v); },
10924+
get knex() { return readKnex(); },
10925+
get openTransactions() { return readOpenTransactions(); },
10926+
};
10927+
return beginTransaction.call(host);
1090810928
}
1090910929

1091010930
/** Idempotently drop a transaction from the open-count (safe on double close). */
@@ -10934,44 +10954,51 @@ export class SqlDriver implements IDataDriver {
1093410954
* `this.knex`.
1093510955
*/
1093610956
protected assertBareKnexSafe(op: string): void {
10937-
if (this.isProductionEnv()) return;
10938-
if (!this.isSqlite) return;
10939-
if (this.activeTransactions === 0) return;
10940-
throw new Error(
10941-
`[driver-sql] refusing to run '${op}' on a fresh pooled connection while a ` +
10942-
`transaction is open: SQLite's pool has a single connection, so acquiring a ` +
10943-
`second one would dead-lock (surfacing later as "Knex: Timeout acquiring a ` +
10944-
`connection"). Bind this operation to the active transaction instead of using ` +
10945-
`this.knex.`,
10946-
);
10957+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10958+
const readActiveTransactions = () => this.activeTransactions;
10959+
const readIsSqlite = () => this.isSqlite;
10960+
const host: ThisParameterType<typeof assertBareKnexSafe> = {
10961+
get activeTransactions() { return readActiveTransactions(); },
10962+
isProductionEnv: (...a) => this.isProductionEnv(...a),
10963+
get isSqlite() { return readIsSqlite(); },
10964+
};
10965+
return assertBareKnexSafe.call(host, op);
1094710966
}
1094810967

1094910968
/** IDataDriver standard */
10950-
async commit(transaction: unknown): Promise<void> {
10951-
try {
10952-
await (transaction as Knex.Transaction).commit();
10953-
} finally {
10954-
this.releaseTransaction(transaction);
10955-
}
10969+
commit(transaction: unknown): Promise<void> {
10970+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10971+
const host: ThisParameterType<typeof commit> = {
10972+
releaseTransaction: (...a) => this.releaseTransaction(...a),
10973+
};
10974+
return commit.call(host, transaction);
1095610975
}
1095710976

1095810977
/** IDataDriver standard */
10959-
async rollback(transaction: unknown): Promise<void> {
10960-
try {
10961-
await (transaction as Knex.Transaction).rollback();
10962-
} finally {
10963-
this.releaseTransaction(transaction);
10964-
}
10978+
rollback(transaction: unknown): Promise<void> {
10979+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10980+
const host: ThisParameterType<typeof rollback> = {
10981+
releaseTransaction: (...a) => this.releaseTransaction(...a),
10982+
};
10983+
return rollback.call(host, transaction);
1096510984
}
1096610985

1096710986
/** @deprecated Use commit() instead */
10968-
async commitTransaction(trx: Knex.Transaction): Promise<void> {
10969-
await this.commit(trx);
10987+
commitTransaction(trx: Knex.Transaction): Promise<void> {
10988+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10989+
const host: ThisParameterType<typeof commitTransaction> = {
10990+
commit: (...a) => this.commit(...a),
10991+
};
10992+
return commitTransaction.call(host, trx);
1097010993
}
1097110994

1097210995
/** @deprecated Use rollback() instead */
10973-
async rollbackTransaction(trx: Knex.Transaction): Promise<void> {
10974-
await this.rollback(trx);
10996+
rollbackTransaction(trx: Knex.Transaction): Promise<void> {
10997+
// [#25521 cut 3] The body moved to `sql-driver-transactions.ts` (see `TransactionHost`).
10998+
const host: ThisParameterType<typeof rollbackTransaction> = {
10999+
rollback: (...a) => this.rollback(...a),
11000+
};
11001+
return rollbackTransaction.call(host, trx);
1097511002
}
1097611003

1097711004
// ===================================

0 commit comments

Comments
 (0)