Repository navigation
Commit e78d743
fix(driver-sql): a SQLite array insert past one statement's bounds goes as several INSERTs in one transaction (#38309)
Fixes #38236
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用 SQLite 的应用(本地开发、自托管)一次写入超过五百行时,以前整批报错,现在全部写入;其它数据库无变化。
- 属于数据层的稳定性修复,替用 SQLite 做批量导入、种子数据和批量创建的人解决问题。
- 出错时整批回滚、一行不留,与以前一次写入失败的表现相同;改动只在数据库驱动里,可直接撤回。
- 无
## What changes
`SqlDriver#bulkCreate` sent every batch as ONE INSERT. On SQLite, knex
compiles a multi-row INSERT as `insert … select … union all select …`,
and SQLite refuses one statement past either of two bounds. Measured on
current `main` (9f5562f) with the better-sqlite3 build this package
ships (SQLite 3.53.4; `pragma compile_options` lists
`MAX_COMPOUND_SELECT=500 MAX_VARIABLE_NUMBER=32766`), binary search per
width:
| columns | most rows one INSERT took | bound values | refusal at one
row more |
|:---|:---|:---|:---|
| 1 | 500 | 500 | `too many terms in compound SELECT` |
| 5 | 500 | 2500 | `too many terms in compound SELECT` |
| 20 | 500 | 10000 | `too many terms in compound SELECT` |
| 70 | 468 | 32760 | `too many SQL variables` |
So the bound is `rows <= 500 AND rows x columns <= 32766`; the row bound
binds first up to 65 columns, the variable bound past that. sql.js (the
`driver-sqlite-wasm` subclass, SQLite 3.49.1) compiles with the same two
values and refuses row 501 the same way.
**Who can send a larger array today (non-test):**
- REST `POST /data/:object/createMany`:
`packages/rest/src/batch-routes.ts:693` caps it at `batch.maxBatchSize`,
which `packages/spec/src/api/rest-server.zod.ts:599` allows up to 1000
(default 200); `:700` calls `createManyData`,
`packages/metadata-protocol/src/protocol-batch-data.ts:815` calls
`engine.insert(object, records)`, and
`packages/objectql/src/engine-insert.ts:1553` calls
`driver.bulkCreate(object, liveRows)` with the whole array. So an
operator who raises the cap past 500 makes `createMany` of 501 to 1000
rows throw on SQLite.
- Any in-process `engine.insert(object, rows[])` (plugins, hooks,
flows): unbounded; `engine-insert.ts:1553` does not split.
- Already bounded below 500: the import runner
(`packages/core/src/utils/import-runner.ts:804`, 200), `bulkWrite`
(`packages/core/src/utils/bulk-write.ts:99`, default 200, a caller may
pass more), the seed loader
(`packages/metadata-protocol/src/seed-loader.ts:4369`, 200).
**The fix**
(`packages/drivers/driver-sql/src/sql-driver-bulk-operations.ts`): on
SQLite, a batch past either bound goes as several INSERTs, in row order,
inside ONE transaction; under a caller's `options.transaction`, a
savepoint of it (the runner idiom `bulkUpdate` already uses). The
guarantees the `bulkCreate` comment states hold for the split batch:
- all-or-nothing: a failing statement rolls back the statements before
it;
- the auto-number reservation is taken once, for the whole batch, by the
existing loop, BEFORE the transaction opens, and dealt in row order; the
transaction holds only the INSERTs, so it never waits on a reservation's
own connection (the SQLite pool-of-one deadlock the comment names);
- a failed batch burns its reserved numbers exactly as a failed single
INSERT does, and the collision retry re-issues the whole batch, as
before.
A batch within both bounds is sent exactly as before: one INSERT, no
transaction. Other dialects are not split. The comment block in
`bulkCreate` gains a section saying all of this. No refusal is added, so
nothing a caller may send changes: `Metadata-protocol: none` holds.
"On SQLite" is asked of knex's client (`compilesSqliteInsert`:
`knex.client.dialect === 'sqlite3'`), because knex's SQLite compiler is
what emits the bounded `union all` form. Measured: `better-sqlite3`
reports `sqlite3`; `pg`, `mysql2`, `mssql`, `oracledb` report
`postgresql`, `mysql`, `mssql`, `oracle`; the sql.js client of
`driver-sqlite-wasm` is `class Client_WasmSqlite extends Client_SQLite3`
and does not rename the dialect, and Turso's local mode is spelled
`better-sqlite3`. So the split reaches the same three drivers `isSqlite`
names, without a host member: `sql-driver.ts` is a facade at its line
ceiling (`check:facade-delegation-only`, 10071 of 10071), and the first
cut of this branch, which added `get isSqlite()` to the facet host
there, went red on that gate by one line. This diff does not touch
`sql-driver.ts`.
## Tests
New
`packages/drivers/driver-sql/src/sql-driver-38236-sqlite-bulk-insert-bound.test.ts`
(13 tests), against better-sqlite3 in memory:
- a 1200-row x 6-column batch inserts every row, in order, as 3 INSERTs
that sit between ONE `BEGIN` and ONE `COMMIT`;
- a 450-row x 80-column batch (under 500 rows, past 32766 values) splits
and lands;
- auto-numbers across the 1200 rows are `T-00001` to `T-01200`,
contiguous and in row order, stored and answered, counter at 1200;
- a 1200-row batch whose row 1150 (third statement) repeats an id leaves
0 rows stored; the counter moves by 1200, exactly as the 3-row
single-statement CONTROL moves it by 3; the next batch continues at
`T-01204` with no duplicate;
- under a caller's transaction: the caller's rollback removes all 1200
rows; a failed split batch is undone and the caller's own earlier write
survives its commit;
- CONTROL: a 500-row batch is ONE INSERT, preceded by the reservations'
own `COMMIT` and followed by nothing (no transaction opened around it);
501 rows is two INSERTs;
- three unit pins on `sqliteInsertStatements` (one array kept as is
within both bounds; every statement within both bounds for 1200x6,
450x80, 100x2000, 501x1; a column only one row carries counts for every
row);
- two pins on `compilesSqliteInsert`: true for `better-sqlite3`, false
for `pg`, `mysql2`, `mssql`, `oracledb` (other dialects are never
split); true for a client class extending knex's SQLite client (the
sql.js driver's shape).
<details><summary>Pins red without the fix
(scripts/ablation-replace.mjs)</summary>
Mutation, on head a51d057: `const statements =
compilesSqliteInsert(this.knex) ? sqliteInsertStatements(formattedRows)
: [formattedRows];` replaced by `const statements = [formattedRows];`
(the pre-fix single statement). The suite imports the driver from
`src/`, so no build sits between the mutation and the run.
```
ablation-replace: ok mutation landed: anchor 1 -> 0, blob 3131297 -> 82f60ca228e2
x inserts every row of a 1200-row batch, in order, as statements within both bounds
x splits a wide batch by the parameter bound even under 500 rows
x deals the batch's auto-numbers contiguously and in row order across statements
x leaves nothing stored when the LAST statement fails, and burns the reservation once, as one statement does
x runs inside the caller's transaction: the caller's rollback removes every statement's rows
x undoes a failed split batch inside the caller's transaction and keeps the caller's own writes
ok CONTROL: a batch at the bound is still ONE statement with no transaction opened
x one row past the bound is two statements
ok (3 sqliteInsertStatements pins, 2 compilesSqliteInsert pins)
Tests 7 failed | 6 passed (13)
ablation-replace: ok restored: blob == HEAD (3131297) and `git diff HEAD` is empty
```
Red reasons in the log: `too many terms in compound SELECT` (4), `too
many SQL variables` (1, the wide batch); the two caller-transaction pins
fail on the same refusal and then time out in teardown on the
transaction left open.
</details>
<details><summary>Package suite and typecheck</summary>
`pnpm --filter @objectstack/driver-sql typecheck && pnpm --filter
@objectstack/driver-sql test` under the verify lock: `VERDICT
command-exit 0`; `Test Files 272 passed | 14 skipped (286)`, `Tests 4582
passed | 265 skipped (4847)`, on head a51d057. The package `typecheck`
(`tsc --noEmit`) lists the new test file (`--listFiles`: 1 hit). Live
PostgreSQL and MySQL were not run locally (no URLs set); they are
untouched by this diff, which splits only on SQLite.
</details>
<details><summary>Derived gate families, run locally on head
a51d057</summary>
`node scripts/pm/dispatch-gates.mjs --commands` derived 65 commands from
the tree at a51d057; each was run with its exit code captured before
any pipe; `--ran` reconciles `65 derived, 65 run, 0 UNRUN`.
- exit 0: 62 of 65, among them `check:durability-log-level`,
`check:tenant-chokepoint`, `check:driver-conformance`,
`check:published-files`, `check:nul-bytes`, `check:changeset-no-major`,
`check:empty-changeset`, `check:adr-0087-registration`.
- NOT MEASURED locally, exit 3 (PREREQUISITE NOT MET, so nothing was
measured); CI runs each on a full checkout and build:
- `node scripts/check-plugin-teardown-shape.mjs --self-test`: its
fixture is pinned to a commit this shallow clone lacks
- `pnpm check:dual-build-cjs-loads`: it loads the built entry points of
every package, and only the driver-sql closure is built here
- `pnpm check:lean-entry-closure`: it loads built entry points
(`@objectstack/objectql` is not built here)
- exit 1: none.
- Outside the derivation, because the final diff no longer touches
`sql-driver.ts`: `pnpm check:facade-delegation-only` exit 0 on a51d057
(`sql-driver.ts: 10071 lines, ceiling 10071 (0 left; ruling
6897467376)`). It is the gate that refused this branch's first cut by
one line.
</details>
## Acceptance notes
- File surface: exactly the claim's: `sql-driver-bulk-operations.ts`,
one co-located test file, one changeset. `sql-driver.ts` and
`sql-driver-schema-sync.ts` are not touched.
- The two bounds are SQLite's compile-time defaults, written as
constants (`SQLITE_MAX_INSERT_ROWS`, `SQLITE_MAX_INSERT_VARIABLES`), not
read at runtime: better-sqlite3 does not expose `sqlite3_limit`, and
both builds this repository ships (better-sqlite3, sql.js) compile with
these values. A build compiled LOWER would still refuse; none ships
here.
- The column count is the union of the rows' keys (knex binds every
column of the union for every row); an overcount only makes a statement
smaller.
## Verification
Files to read first:
`packages/drivers/driver-sql/src/sql-driver-bulk-operations.ts` (the
`try` block in `bulkCreate`: the one-statement branch must send the same
single INSERT, through the same RETURNING or read-back, as before), then
the new test file.
Two-minute check: `pnpm --filter @objectstack/driver-sql exec vitest run
src/sql-driver-38236-sqlite-bulk-insert-bound.test.ts` shows `Tests 13
passed (13)`.
---
_Generated by [Claude
Code](https://claude.ai/code/session_017xRBcVXco8T2suSgnRk52Y)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent a7e60c7 commit e78d743
3 files changed
Lines changed: 368 additions & 8 deletions
File tree
- .changeset
- packages/drivers/driver-sql/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 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 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 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 | + | |
| 129 | + | |
| 130 | + | |
| 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 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 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 | + | |
| 200 | + | |
| 201 | + | |
| 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 | + | |
0 commit comments