Repository navigation
Commit 5d27d32
fix(service-datasource): in development, a SQLite file that cannot be opened or created fails its connect instead of stepping down to an in-memory engine (#24955)
Fixes #24950
Clause-②: no
## What was wrong
`resolveSqliteDriver`
(`packages/services/service-datasource/src/sqlite-driver-fallback.ts`)
probes a native `better-sqlite3` connection in development and steps
down to wasm SQLite, then to an in-memory engine. It stepped down on ANY
probe failure. Its own module note says the step-down exists for one
cause: the native addon loads lazily, so an ABI or load failure stays
invisible until the first query.
So under `bin/run-dev.js` (`NODE_ENV=development`), a declared SQLite
datasource whose FILE could not be opened was handed to the in-memory
engine and read `connected`. Measured at the public door before this
change (below): `os migrate apply --yes` with a bound `archive`
datasource on a path under a regular file printed "neither native nor
wasm SQLite available — dev falling back to InMemoryDriver", then
`datasource 'archive': connected`, `Applied 0 change(s)`, and exit 0.
## What changed
- In development, a probe failure that is the database FILE failing to
open or be created is re-thrown, as it is in production. The
datasource's connect fails, and `listConnectionStates()` reports it
`failed`. This applies to every SQLite datasource, the primary included.
- The classification reads the thrown value's `code` only, never its
message (`isSqliteFileOpenFailure`, module-private). A file failure is
an operating-system code (`E` plus capitals and digits, no underscore:
`EEXIST`, `ENOTDIR`, `ENOENT`, `EACCES`, …) or `SQLITE_CANTOPEN`.
`SqlDriver.connect()` raises these as its `SqliteDatabaseFileError`.
- A native addon that cannot load still steps down to wasm SQLite, with
the same warning. Its failures carry `ERR_DLOPEN_FAILED`,
`MODULE_NOT_FOUND`, or no code at all, and at the probe's `SELECT 1`
they arrive wrapped as `DATABASE_ERROR`. None of these matches.
Production is unchanged.
- The module note now says which failures step down and why.
- One changeset: `@objectstack/service-datasource` `patch`.
Not touched: `packages/cli/**` (`bin/run-dev.js` keeps
`NODE_ENV=development`), `connect-failure-remedy.ts`,
`datasource-connection-service.test.ts`, `packages/drivers/**`. No new
env var, flag or gate.
## Premises, measured before the change was committed
All readings were taken on this branch's base `feb09db14a` with real
`better-sqlite3` 13.0.3 on Node v22.22.0, running the base copy of
`sqlite-driver-fallback.ts` byte for byte (`git show BASE:path`) against
the real drivers.
- **P1 holds, on codes.** Raw `better-sqlite3`: under a regular file,
`SqliteError` `SQLITE_CANTOPEN`. A directory as the file:
`SQLITE_CANTOPEN`. A missing parent: a code-less `TypeError` ("directory
does not exist"). Through `SqlDriver.connect()`, which makes the
directory first, the probe's thrown value is `SqliteDatabaseFileError`
with code `EEXIST` (under a regular file), `ENOTDIR` (deeper), `ENOENT`
(`/proc/missing/`), `EACCES` (mkdir as an unprivileged user in a 0555
directory) or `SQLITE_CANTOPEN` (a directory, or a new file in a
read-only directory). Addon-load shapes: a real `process.dlopen` of a
non-addon gives `ERR_DLOPEN_FAILED`. A missing `.node` path gives
`MODULE_NOT_FOUND`. `bindings`' "Could not locate the bindings file" has
no code. A missing `better-sqlite3` package gives knex's install hint,
which has no code either. No message text was needed to separate them.
- **P2 holds.** An absent file in an existing directory opens native,
and the file is created. An absent file under two missing directory
levels also opens native: `connect()` makes the directories. Neither
hits the re-throw. The standalone host already refuses a primary whose
directory it cannot make (`createStandaloneStack`, #23737), and no test
or flow in the tree relies on the in-memory rung for an unopenable file.
- **P3 holds for the shapes the ruling names, with one refinement.** On
base, under a regular file, `/proc/missing/` and an EACCES mkdir, wasm
failed on the same path and the in-memory rung answered (`engine:
memory`). For a directory as the file, and for a new file in a read-only
directory, wasm's `connect()` RESOLVED, so the base answer was `engine:
sqlite-wasm` over a file it could not read. For the directory, the first
query then failed with `DATABASE_ERROR` (cause `EISDIR`). The native
re-throw catches both shapes before wasm.
- **Why the re-throw sits at the native rung only.** I first put the
same re-throw on the wasm rung too. The ablation showed that no measured
path reaches it. `connect()` makes the file's directory before the first
statement loads the addon, so even with an addon that cannot load, the
native rung refuses every path the wasm rung would. I removed the
wasm-rung re-throw (`4da3d4bf0c`). The step-down for an addon that
cannot load behaves exactly as before. The wasm rung's catch only moved
its best-effort `disconnect()` into the shared `closeQuietly`.
## Pins (new file
`src/__tests__/sqlite-driver-fallback-file-open-failure.test.ts`)
These use real `better-sqlite3`, real wasm SQLite and real files in a
scratch directory. The one thing faked is an addon that cannot load. The
fake sits where `better-sqlite3` loads the addon, on the knex
better-sqlite3 client's own `acquireRawConnection`, and throws a REAL
load error: Node's `ERR_DLOPEN_FAILED`, or `bindings`' own "not built"
error. The existing `src/sqlite-driver-fallback.test.ts` mocks all three
driver packages and is unchanged; its 11 cases pass.
- development: under a regular file, the probe rejects with
`SqliteDatabaseFileError`: `EEXIST`, and `ENOTDIR` one level deeper. No
warning, never wasm or memory.
- development: a directory as the file rejects with `SQLITE_CANTOPEN`.
- development: an absent file in an existing directory opens native, and
the file is created.
- development: an absent file under missing directories opens native.
- development: `ERR_DLOPEN_FAILED` steps down to wasm SQLite with
`NATIVE_SQLITE_WASM_FALLBACK_WARNING`, and `SELECT 1` answers.
- development: the code-less "not built" error steps down to wasm
SQLite.
- development: an addon that cannot load plus a path under a regular
file still rejects, never memory.
- production: under a regular file, the resolver returns native unprobed
with no warning, and its `connect()` refuses the file (`EEXIST`), as
before.
- `DatasourceConnectionService` +
`createDefaultDatasourceDriverFactory({ dev: true })`: a declared
`archive` (`autoConnect: true`) under a regular file reads
`availability: 'failed'`, its reason names the file, and no driver is
registered.
**Ablation**, one leg for the one behaviour changed. The native re-throw
was replaced through `scripts/ablation-replace.mjs`: anchor 1 → 0,
marker 0 → 1, blob `ec4203705cea` → `fc817e7389d4`. The subject is
imported relatively from `src`, so no dist leg applies. Predicted 4 red
/ 5 green, observed 4 red / 5 green: regular file, directory,
addon+regular file and connection-state turned red; both absent-file
pins, both addon-load pins and production stayed green. The restore was
proved by blob `ec4203705cea` equal to HEAD and an empty `git diff
HEAD`.
## Public door, before and after
Each run is `bin/run-dev.js migrate apply --yes --database-url
file:main.db` in a throwaway project whose `dist/objectstack.json`
declares `archive` (`driver: 'sqlite'`) on a path under a regular file.
| fixture | before (base behaviour planted in `dist/`) | after
(`05e9a8e09a`) |
|---|---|---|
| an object binds to `archive` | "neither native nor wasm SQLite
available — dev falling back to InMemoryDriver", `datasource 'archive':
connected`, `Applied 0 change(s)`, exit 0 | `datasource 'archive':
connect failed — SQLite database file '…/plain/archive.db' cannot be
created: its directory '…/plain' could not be made: EEXIST …` (fail-fast
per ADR-0062 D5), exit 1 |
| `autoConnect: true`, nothing bound | `connected` on the in-memory
engine, exit 0 | WARN `datasource 'archive': connect failed — SQLite
database file …` (degraded, as D5 says), exit 0 |
For the before leg, the native re-throw was mutated away in `src`
through `ablation-replace` (WRAP). Then `service-datasource` was rebuilt
and `ablation-dist-preflight` confirmed the marker in 2 built files.
After the restore, the package was rebuilt again and `--absent`
confirmed the marker gone from all 24 built files, with a clean tree.
**The card's own path, `os migrate audit-mail-bodies` over an archive
datasource: NOT MEASURED.** The archive examination exists only on PR
#24934, which is not merged at this head. The `--apply` boot of that
command builds its datasources through this same factory, so the after
row above is that boot's mechanism.
## Local verification, on head `d353b3979b` (after merging `origin/main`
`ea08ce584c`, which landed #24933)
- `pnpm exec turbo run build
--filter='@objectstack/service-datasource...' --concurrency=2`: 19/19
successful.
- `pnpm --filter @objectstack/service-datasource exec vitest run
--maxWorkers=2`: 50 files, 892 tests passed.
- `pnpm --filter @objectstack/service-datasource typecheck` (`tsc
--noEmit`): exit 0. `--listFiles` includes the new test file.
- Gates: `node scripts/pm/dispatch-gates.mjs --commands --repo
objectstack-ai/objectstack` (no paths) derived 66 commands at
`d353b3979b`. All 66 ran one at a time, each exit captured before any
pipe: 66 × exit 0. `--ran` printed: 66 derived, 66 run, 0 NOT-MEASURED,
0 UNRUN. Before the merge, the same 66 at `05e9a8e09a` gave 65 × exit 0,
and `check:dual-build-cjs-loads` exited 3 (PREREQUISITE NOT MET: unbuilt
packages elsewhere in the tree). Every family that CI runs is read from
this PR's check-runs, not re-run here.
## Acceptance notes
- **Boundary, left as is: a file that is not a SQLite database still
steps down, and wasm then moves it aside.** The native probe answers
`SQLITE_NOTADB`, which is not an open failure, so it keeps today's path.
Wasm SQLite then renames the file to `archive.db.corrupt-TIMESTAMP`,
creates an empty database in its place, and the datasource reads
`connected`. The warning says "native better-sqlite3 unavailable (ABI
mismatch or not built)", which is false. Measured at the same public
door on this head: exit 0, the operator's file moved aside. This is
outside the ruling's named route; the report puts it to the seat as an
open question.
- **Boundary, left as is: the read-only boot treats an unopenable path
as an ABSENT file.** `os migrate plan`, and the dry runs that boot with
`readOnlyProbe`, pass `sqliteAbsentFile: 'empty-in-memory'`.
driver-sql's `resolveSqliteAbsentFileTarget` sees `existsSync(path) ===
false` for a path under a regular file and opens an empty in-memory
SQLite in its place. So `bin/run-dev.js migrate plan` with the bound
fixture above prints `datasource 'archive': connected` and plans `+
os24950_note [create_table, 8 column(s)]` into a file that cannot exist,
exit 0. This PR does not change that: no probe failure occurs, and the
fix belongs in `packages/drivers/**`. The report hands it to the seat.
- **Not reachable from here: with an addon that cannot load, wasm's
`connect()` resolves over a file it cannot read** (a directory as the
file, a new file in a read-only directory). Its first query fails
instead. `applyAutoVacuumIncremental` swallows the open error, and the
wasm rung's probe is `connect()` alone. This is the addon-cannot-load
path, which this card does not change. Carrier: none.
- `os db clean` on a target it cannot open now prints the driver's
`SqliteDatabaseFileError` text, which names the file. Before, it printed
its own "native and wasm both failed to load".
`provisionTelemetryDatasource` already swallows any throw, and returns
`undefined` exactly as it did for the memory engine.
---
_Generated by [Claude
Code](https://claude.ai/code/session_011TdZ8GjixNZFLz1xJD9zzK)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2941a19 commit 5d27d32
3 files changed
Lines changed: 333 additions & 21 deletions
File tree
- .changeset
- packages/services/service-datasource/src
- __tests__
| 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 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 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 | + | |
34 | 67 | | |
35 | 68 | | |
36 | 69 | | |
| |||
100 | 133 | | |
101 | 134 | | |
102 | 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 | + | |
103 | 175 | | |
104 | 176 | | |
105 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
106 | 180 | | |
107 | 181 | | |
108 | 182 | | |
| |||
149 | 223 | | |
150 | 224 | | |
151 | 225 | | |
152 | | - | |
| 226 | + | |
153 | 227 | | |
154 | 228 | | |
155 | 229 | | |
156 | 230 | | |
157 | 231 | | |
158 | 232 | | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
163 | 238 | | |
164 | 239 | | |
165 | 240 | | |
166 | | - | |
| 241 | + | |
167 | 242 | | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | | - | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
175 | 249 | | |
176 | 250 | | |
177 | 251 | | |
| |||
193 | 267 | | |
194 | 268 | | |
195 | 269 | | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
| 270 | + | |
203 | 271 | | |
204 | 272 | | |
205 | 273 | | |
| |||
0 commit comments