Open History to see the last commit
1import * as NodeSqlite from "node:sqlite";
2import type * as NodeWorkerThreads from "node:worker_threads";
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { assert, it } from "@effect/vitest";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as FileSystem from "effect/FileSystem";
9import * as Path from "effect/Path";
10import * as SqlClient from "effect/sql/SqlClient";
11import * as Statement from "effect/sql/Statement";
12import { ChildProcess, ChildProcessSpawner } from "effect/process";
13import * as Stream from "effect/Stream";
14import type * as Tracer from "effect/Tracer";
15import { vi } from "vite-plus/test";
16
17import * as SqliteClient from "./nodeSqliteClient.ts";
18
19const startedWorkers = vi.hoisted((): Array<NodeWorkerThreads.Worker> => []);
20const silentWorkers = vi.hoisted(() => ({ on: false, readyOnTerminate: false }));
21const failingWorkers = vi.hoisted(() => ({ message: undefined as string | undefined }));
22
23vi.mock("node:worker_threads", async (importOriginal) => {
24 const actual = await importOriginal<typeof NodeWorkerThreads>();
25 class RecordedWorker extends actual.Worker {
26 constructor(...args: ConstructorParameters<typeof actual.Worker>) {
27 super(
28 ...((silentWorkers.on
29 ? ["setInterval(() => {}, 1 << 30);", { eval: true }]
30 : failingWorkers.message !== undefined
31 ? [`throw new Error(${JSON.stringify(failingWorkers.message)});`, { eval: true }]
32 : args) as ConstructorParameters<typeof actual.Worker>),
33 );
34 startedWorkers.push(this);
35 }
36 override terminate() {
37 // A "ready" already queued when termination starts reaches the parent before "exit".
38 if (silentWorkers.readyOnTerminate) this.emit("message", { type: "ready" });
39 return super.terminate();
40 }
41 }
42 return { ...actual, Worker: RecordedWorker };
43});
44
45const withReaderClient = <A, E>(
46 use: (sql: SqlClient.SqlClient) => Effect.Effect<A, E, SqlClient.SqlClient>,
47) =>
48 Effect.gen(function* () {
49 const fs = yield* FileSystem.FileSystem;
50 const path = yield* Path.Path;
51 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-reader-" });
52 const filename = path.join(directory, "state.sqlite");
53 return yield* Effect.gen(function* () {
54 const sql = yield* SqlClient.SqlClient;
55 yield* sql`PRAGMA journal_mode = WAL`;
56 yield* sql`CREATE TABLE entries(name TEXT NOT NULL)`;
57 yield* sql`INSERT INTO entries VALUES ('first')`;
58 return yield* use(sql);
59 }).pipe(Effect.provide(SqliteClient.layer({ filename, readerWorker: true })));
60 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
61
62const countEntries = (sql: SqlClient.SqlClient) =>
63 Effect.map(
64 sql<{ count: number }>`SELECT COUNT(*) AS count FROM entries`,
65 (rows) => rows[0]?.count,
66 );
67
68const layer = it.layer(SqliteClient.layer({ filename: ":memory:" }));
69
70layer("NodeSqliteClient", (it) => {
71 it.effect("records the writer connection owner on a waiting query", () =>
72 Effect.gen(function* () {
73 const sql = yield* SqlClient.SqlClient;
74 const held = yield* Deferred.make<void>();
75 const release = yield* Deferred.make<void>();
76 const holder = yield* sql
77 .withTransaction(
78 sql`SELECT 1`.pipe(
79 Effect.andThen(Deferred.succeed(held, undefined)),
80 Effect.andThen(Deferred.await(release)),
81 ),
82 )
83 .pipe(Effect.withSpan("test.writerHolder"), Effect.forkChild);
84 yield* Deferred.await(held);
85
86 const waitingSpan = yield* Deferred.make<Tracer.Span>();
87 const observeWaiter: Statement.Transformer = (statement, _sql, _fiber, span) =>
88 Deferred.succeed(waitingSpan, span).pipe(Effect.as(statement));
89 const waiter = yield* sql`SELECT 1`.pipe(
90 Effect.provideService(Statement.CurrentTransformer, observeWaiter),
91 Effect.provideService(Statement.SpanPropagationEnabled, true),
92 (effect) => Effect.forkChild(effect, { startImmediately: true }),
93 );
94 const span = yield* Deferred.await(waitingSpan);
95 yield* Deferred.succeed(release, undefined);
96 yield* Fiber.join(holder);
97 yield* Fiber.join(waiter);
98
99 assert.equal(span.attributes.get("db.lock_owner_at_start.name"), "test.writerHolder");
100 assert.typeOf(span.attributes.get("db.lock_owner_at_start.fiber_id"), "number");
101 assert.typeOf(span.attributes.get("db.lock_owner_at_start.trace_id"), "string");
102 assert.typeOf(span.attributes.get("db.lock_owner_at_start.span_id"), "string");
103 }),
104 );
105
106 it.effect("retries preparing a query after the missing schema becomes available", () =>
107 Effect.gen(function* () {
108 const sql = yield* SqlClient.SqlClient;
109 const select = sql<{ name: string }>`SELECT name FROM created_after_prepare_failure`;
110 const error = yield* select.pipe(Effect.flip);
111 assert.equal(error._tag, "SqlError");
112 assert.equal(error.reason.operation, "prepare");
113
114 yield* sql`CREATE TABLE created_after_prepare_failure(name TEXT NOT NULL)`;
115 yield* sql`INSERT INTO created_after_prepare_failure VALUES ('recovered')`;
116 assert.deepEqual(yield* select, [{ name: "recovered" }]);
117 assert.deepEqual(yield* select.values, [["recovered"]]);
118 }),
119 );
120
121 it.effect("classifies constraint failures by their SQLite result code", () =>
122 Effect.gen(function* () {
123 const sql = yield* SqlClient.SqlClient;
124 yield* sql`CREATE TABLE constrained(name TEXT NOT NULL UNIQUE)`;
125 yield* sql`INSERT INTO constrained VALUES ('taken')`;
126
127 const duplicate = yield* sql`INSERT INTO constrained VALUES ('taken')`.pipe(Effect.flip);
128 assert(duplicate.reason._tag === "UniqueViolation");
129 assert.equal(duplicate.reason.constraint, "constrained.name");
130
131 const missing = yield* sql`INSERT INTO constrained VALUES (NULL)`.pipe(Effect.flip);
132 assert.equal(missing.reason._tag, "ConstraintError");
133 }),
134 );
135});
136
137const largeText = "x".repeat(64 * 1024 + 1);
138
139it.effect("reads integers on the values path as the caller asked, cached or not", () =>
140 Effect.gen(function* () {
141 const sql = yield* SqlClient.SqlClient;
142 for (const value of ["small", largeText]) {
143 const values = sql`SELECT 1 AS id, length(${value}) AS size`.values;
144 assert.deepEqual(yield* values.pipe(Effect.provideService(SqlClient.SafeIntegers, true)), [
145 [1n, BigInt(value.length)],
146 ]);
147 assert.deepEqual(yield* values, [[1, value.length]]);
148 }
149 }).pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:" }))),
150);
151
152const makeTempDatabase = Effect.gen(function* () {
153 const fs = yield* FileSystem.FileSystem;
154 const path = yield* Path.Path;
155 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-transaction-" });
156 const filename = path.join(directory, "state.sqlite");
157 const other = yield* Effect.acquireRelease(
158 Effect.sync(() => new NodeSqlite.DatabaseSync(filename)),
159 (database) => Effect.sync(() => database.close()),
160 );
161 yield* Effect.sync(() => {
162 other.exec(`
163 PRAGMA journal_mode = WAL;
164 CREATE TABLE counters(id INTEGER PRIMARY KEY, value INTEGER NOT NULL);
165 INSERT INTO counters VALUES (1, 0);
166 `);
167 });
168 return { filename, other };
169});
170
171it.effect("keeps another connection from committing between a transaction's read and write", () =>
172 Effect.gen(function* () {
173 const { filename, other } = yield* makeTempDatabase;
174 yield* Effect.gen(function* () {
175 const sql = yield* SqlClient.SqlClient;
176 yield* sql.withTransaction(
177 Effect.gen(function* () {
178 const [row] = yield* sql<{ readonly value: number }>`
179 SELECT value FROM counters WHERE id = 1
180 `;
181 // Deferred BEGIN permits a competing commit, causing SQLITE_BUSY_SNAPSHOT that no timeout can resolve.
182 yield* Effect.sync(() =>
183 assert.throws(
184 () => other.exec("UPDATE counters SET value = value + 1 WHERE id = 1"),
185 /database is locked/,
186 ),
187 );
188 yield* sql`UPDATE counters SET value = ${(row?.value ?? 0) + 10} WHERE id = 1`;
189 }),
190 );
191 yield* Effect.sync(() => other.exec("UPDATE counters SET value = value + 1 WHERE id = 1"));
192 assert.deepEqual(yield* sql`SELECT value FROM counters WHERE id = 1`.values, [[11]]);
193 }).pipe(Effect.provide(SqliteClient.layer({ filename })));
194 }).pipe(Effect.provide(NodeServices.layer)),
195);
196
197it.effect("waits for another process's lock while the database opens", () =>
198 Effect.gen(function* () {
199 const fs = yield* FileSystem.FileSystem;
200 const path = yield* Path.Path;
201 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-open-" });
202 const filename = path.join(directory, "state.sqlite");
203 const seed = new NodeSqlite.DatabaseSync(filename);
204 seed.exec("PRAGMA journal_mode = WAL; CREATE TABLE entries(name TEXT NOT NULL)");
205 seed.close();
206 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
207 const holder = yield* spawner.spawn(
208 ChildProcess.make(process.execPath, [
209 "-e",
210 `const db = new (require("node:sqlite").DatabaseSync)(process.argv[1]);
211 db.exec("PRAGMA locking_mode = EXCLUSIVE; BEGIN EXCLUSIVE");
212 console.log("held");
213 setTimeout(() => db.close(), 300);`,
214 filename,
215 ]),
216 );
217 yield* Stream.runHead(holder.stdout);
218 yield* Effect.gen(function* () {
219 const sql = yield* SqlClient.SqlClient;
220 assert.deepEqual(yield* sql`SELECT COUNT(*) FROM entries`.values, [[0]]);
221 }).pipe(Effect.provide(SqliteClient.layer({ filename, checkpointWorker: true })));
222 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
223);
224
225it.effect("reports a transaction blocked by another writer as a lock timeout", () =>
226 Effect.gen(function* () {
227 const { filename, other } = yield* makeTempDatabase;
228 yield* Effect.gen(function* () {
229 const sql = yield* SqlClient.SqlClient;
230 yield* sql`PRAGMA busy_timeout = 0`;
231 const read = sql.withTransaction(sql`SELECT value FROM counters WHERE id = 1`.values);
232
233 yield* Effect.sync(() => other.exec("BEGIN IMMEDIATE"));
234 const error = yield* read.pipe(Effect.flip);
235 assert.equal(error.reason._tag, "LockTimeoutError");
236
237 yield* Effect.sync(() => other.exec("COMMIT"));
238 assert.deepEqual(yield* read, [[0]]);
239 }).pipe(Effect.provide(SqliteClient.layer({ filename })));
240 }).pipe(Effect.provide(NodeServices.layer)),
241);
242
243it.effect("lets the writer commit while a read-only transaction is open", () =>
244 withReaderClient((sql) =>
245 Effect.gen(function* () {
246 const snapshotTaken = yield* Deferred.make<void>();
247 const writer = yield* Effect.forkChild(
248 Deferred.await(snapshotTaken).pipe(
249 Effect.andThen(sql`INSERT INTO entries VALUES ('second')`),
250 ),
251 );
252 const counts = yield* sql
253 .withTransaction(
254 Effect.gen(function* () {
255 const before = yield* countEntries(sql);
256 yield* Deferred.succeed(snapshotTaken, undefined);
257 // Holding the writer lock here would deadlock this join.
258 yield* Fiber.join(writer);
259 const after = yield* countEntries(sql);
260 return { before, after };
261 }),
262 )
263 .pipe(SqliteClient.readOnly);
264
265 assert.deepEqual(counts, { before: 1, after: 1 });
266 assert.equal(yield* countEntries(sql).pipe(SqliteClient.readOnly), 2);
267 }),
268 ),
269);
270
271it.effect("serves another read while a read-only transaction is open", () =>
272 withReaderClient((sql) =>
273 Effect.gen(function* () {
274 const opened = yield* Deferred.make<void>();
275 const other = yield* Effect.forkChild(
276 Deferred.await(opened).pipe(Effect.andThen(countEntries(sql).pipe(SqliteClient.readOnly))),
277 );
278 yield* sql
279 .withTransaction(
280 Effect.gen(function* () {
281 yield* countEntries(sql);
282 yield* Deferred.succeed(opened, undefined);
283 // One shared reader would make this join wait on the transaction forever.
284 assert.equal(yield* Fiber.join(other), 1);
285 }),
286 )
287 .pipe(SqliteClient.readOnly);
288 }),
289 ),
290);
291
292// regression: #19226 read-only transactions run on the separate reader connection
293it.effect("rejects a write in a read-only transaction", () =>
294 withReaderClient((sql) =>
295 Effect.gen(function* () {
296 const error = yield* sql
297 .withTransaction(sql`INSERT INTO entries VALUES ('blocked')`)
298 .pipe(SqliteClient.readOnly, Effect.flip);
299
300 assert.equal(error._tag, "SqlError");
301 assert.include(String(error.reason.cause), "readonly database");
302 assert.equal(yield* countEntries(sql), 1);
303 }),
304 ),
305);
306
307it.effect("keeps the reader read-only even if a statement turns query_only off", () =>
308 withReaderClient((sql) =>
309 Effect.gen(function* () {
310 const error = yield* sql
311 .withTransaction(
312 Effect.gen(function* () {
313 yield* sql`PRAGMA query_only = OFF`;
314 yield* sql`INSERT INTO entries VALUES ('blocked')`;
315 }),
316 )
317 .pipe(SqliteClient.readOnly, Effect.flip);
318
319 assert.equal(error._tag, "SqlError");
320 assert.include(String(error.reason.cause), "readonly database");
321 assert.equal(yield* countEntries(sql), 1);
322 }),
323 ),
324);
325
326it.effect("keeps read-only work inside a write transaction on the writer", () =>
327 withReaderClient((sql) =>
328 sql.withTransaction(
329 Effect.gen(function* () {
330 yield* sql`INSERT INTO entries VALUES ('uncommitted')`;
331 const count = yield* sql.withTransaction(countEntries(sql)).pipe(SqliteClient.readOnly);
332 assert.equal(count, 2);
333 }),
334 ),
335 ),
336);
337
338it.effect("fails a read-only transaction whose reader stops, then starts a new reader", () =>
339 withReaderClient((sql) =>
340 Effect.gen(function* () {
341 const startedBefore = startedWorkers.length;
342 const error = yield* sql
343 .withTransaction(
344 Effect.gen(function* () {
345 yield* countEntries(sql);
346 yield* Effect.promise(() => startedWorkers.at(-1)!.terminate());
347 return yield* countEntries(sql);
348 }),
349 )
350 .pipe(SqliteClient.readOnly, Effect.flip);
351
352 assert.equal(error._tag, "SqlError");
353 assert.include(String(error.reason.cause), "Database reader exited");
354 assert.equal(yield* countEntries(sql).pipe(SqliteClient.readOnly), 1);
355 assert.equal(startedWorkers.length, startedBefore + 2);
356 }),
357 ),
358);
359
360const withFileClient = <A, E>(
361 setup: (
362 sql: SqlClient.SqlClient,
363 filename: string,
364 ) => Effect.Effect<A, E, SqlClient.SqlClient | FileSystem.FileSystem>,
365) =>
366 Effect.gen(function* () {
367 const fs = yield* FileSystem.FileSystem;
368 const path = yield* Path.Path;
369 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-reader-off-" });
370 const filename = path.join(directory, "state.sqlite");
371 return yield* Effect.gen(function* () {
372 return yield* setup(yield* SqlClient.SqlClient, filename);
373 }).pipe(Effect.provide(SqliteClient.layer({ filename, readerWorker: true })));
374 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
375
376it.effect("uses the writer when the reader cannot open the database", () =>
377 withFileClient((sql, filename) =>
378 Effect.gen(function* () {
379 yield* sql`PRAGMA journal_mode = WAL`;
380 yield* sql`CREATE TABLE entries(name TEXT NOT NULL)`;
381 yield* sql`INSERT INTO entries VALUES ('kept')`;
382 yield* (yield* FileSystem.FileSystem).remove(filename);
383 const startedBefore = startedWorkers.length;
384
385 assert.equal(yield* sql.withTransaction(countEntries(sql)).pipe(SqliteClient.readOnly), 1);
386 assert.equal(yield* sql.withTransaction(countEntries(sql)).pipe(SqliteClient.readOnly), 1);
387
388 // A read beside an open read transaction must not start a worker on the next slot.
389 const held = yield* Deferred.make<void>();
390 const release = yield* Deferred.make<void>();
391 const holder = yield* sql
392 .withTransaction(
393 countEntries(sql).pipe(
394 Effect.andThen(Deferred.succeed(held, undefined)),
395 Effect.andThen(Deferred.await(release)),
396 ),
397 )
398 .pipe(SqliteClient.readOnly, Effect.forkChild);
399 yield* Deferred.await(held);
400 const other = yield* countEntries(sql).pipe(SqliteClient.readOnly, (effect) =>
401 Effect.forkChild(effect, { startImmediately: true }),
402 );
403 yield* Deferred.succeed(release, undefined);
404 yield* Fiber.join(holder);
405 assert.equal(yield* Fiber.join(other), 1);
406 assert.equal(startedWorkers.length, startedBefore + 1);
407 }),
408 ),
409);
410
411it.effect("finishes masked database cleanup after its caller is interrupted", () =>
412 Effect.gen(function* () {
413 const sql = yield* SqlClient.SqlClient;
414 yield* sql`CREATE TABLE cleanup_writes(value INTEGER PRIMARY KEY)`;
415 const entered = yield* Deferred.make<void>();
416 const work = yield* Effect.forkChild(
417 Deferred.succeed(entered, undefined).pipe(
418 Effect.andThen(Effect.never),
419 Effect.onInterrupt(() =>
420 Effect.gen(function* () {
421 yield* sql`INSERT INTO cleanup_writes VALUES (1)`;
422 yield* sql.withTransaction(sql`INSERT INTO cleanup_writes VALUES (2)`);
423 }),
424 ),
425 ),
426 { startImmediately: true },
427 );
428 yield* Deferred.await(entered);
429 yield* Fiber.interrupt(work);
430 assert.deepEqual(yield* sql`SELECT value FROM cleanup_writes ORDER BY value`.values, [
431 [1],
432 [2],
433 ]);
434 }).pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:" }))),
435);
436
437// oxlint-disable-next-line t3code/no-real-time-wait-in-tests -- a zero-delay macrotask boundary that lets the worker's queued messages land, not a wait for time
438const eventLoopTurn = Effect.promise(() => new Promise<void>((resolve) => setImmediate(resolve)));
439
440it.effect.each([
441 { reservation: false, masked: false },
442 { reservation: false, masked: true },
443 { reservation: true, masked: false },
444 { reservation: true, masked: true },
445])(
446 "preserves caller cancellation while waiting (reservation=$reservation, masked=$masked)",
447 ({ reservation, masked }) =>
448 Effect.gen(function* () {
449 const sql = yield* SqlClient.SqlClient;
450 yield* sql`CREATE TABLE entries(value INTEGER)`;
451 const held = yield* Deferred.make<void>();
452 const release = yield* Deferred.make<void>();
453 const holder = yield* Effect.forkChild(
454 sql.withTransaction(
455 Deferred.succeed(held, undefined).pipe(Effect.andThen(Deferred.await(release))),
456 ),
457 { startImmediately: true },
458 );
459 yield* Deferred.await(held);
460 const entered = yield* Deferred.make<void>();
461 const body = sql`INSERT INTO entries VALUES (1)`.pipe(
462 Effect.andThen(Deferred.succeed(entered, undefined)),
463 );
464 const acquire = reservation
465 ? Effect.scoped(
466 Effect.gen(function* () {
467 const connection = yield* sql.reserve;
468 yield* connection.executeUnprepared("INSERT INTO entries VALUES (1)", [], undefined);
469 yield* Deferred.succeed(entered, undefined);
470 }),
471 )
472 : sql.withTransaction(body);
473 const waiter = yield* Effect.forkChild(masked ? Effect.uninterruptible(acquire) : acquire, {
474 startImmediately: true,
475 });
476 const cancelled = yield* Effect.forkChild(Fiber.interrupt(waiter), {
477 startImmediately: true,
478 });
479 yield* Effect.gen(function* () {
480 yield* eventLoopTurn;
481 assert.equal(cancelled.pollUnsafe() !== undefined, !masked);
482 assert.isFalse(yield* Deferred.isDone(entered));
483 }).pipe(Effect.ensuring(Deferred.succeed(release, undefined)));
484 yield* Fiber.join(holder);
485 yield* Fiber.join(cancelled);
486 assert.equal(yield* Deferred.isDone(entered), masked);
487 yield* sql.withTransaction(sql`INSERT INTO entries VALUES (2)`);
488 assert.deepEqual(
489 yield* sql`SELECT value FROM entries ORDER BY value`.values,
490 masked ? [[1], [2]] : [[2]],
491 );
492 }).pipe(Effect.provide(SqliteClient.layer({ filename: ":memory:" }))),
493);
494
495const PAYLOAD = new Uint8Array(1024 * 1024).fill(7);
496const WAL_SIZE_LIMIT = 32 * 1024 * 1024;
497
498// Fails instead of hanging when admission holds the write: the worker retries a blocked
499// checkpoint on its own, so two blocked reports for one WAL generation mean a held write.
500const insertUnlessHeld = Effect.fnUntraced(function* (
501 sql: SqlClient.SqlClient,
502 worker: NodeWorkerThreads.Worker,
503 label: string,
504) {
505 const stalled = yield* Deferred.make<void>();
506 let seen: unknown;
507 const onMessage = (message: { type?: unknown; blocked?: unknown; generation?: unknown }) => {
508 if (message?.type !== "checkpoint" || message.blocked !== true) return;
509 if (message.generation === seen) Deferred.doneUnsafe(stalled, Effect.void);
510 seen = message.generation;
511 };
512 worker.on("message", onMessage);
513 const held = yield* Effect.raceFirst(
514 sql`INSERT INTO blobs VALUES (${PAYLOAD})`.pipe(Effect.as(false)),
515 Deferred.await(stalled).pipe(Effect.as(true)),
516 ).pipe(Effect.ensuring(Effect.sync(() => worker.off("message", onMessage))));
517 if (held) assert.fail(`${label} is held by admission while an outside reader pins the WAL`);
518});
519
520// `outside` stands in for `sqlite3 statev2.sqlite` or a backup script reading the database.
521const withOutsideConnection = <A, E>(
522 use: (options: {
523 readonly sql: SqlClient.SqlClient;
524 readonly worker: NodeWorkerThreads.Worker;
525 readonly outside: NodeSqlite.DatabaseSync;
526 readonly walSize: Effect.Effect<number>;
527 }) => Effect.Effect<A, E, SqlClient.SqlClient>,
528) =>
529 Effect.gen(function* () {
530 const fs = yield* FileSystem.FileSystem;
531 const path = yield* Path.Path;
532 const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-pinned-wal-" });
533 const filename = path.join(directory, "state.sqlite");
534 const outside = yield* Effect.acquireRelease(
535 Effect.sync(() => new NodeSqlite.DatabaseSync(filename)),
536 (db) =>
537 Effect.sync(() => {
538 if (db.isTransaction) db.exec("COMMIT");
539 db.close();
540 }),
541 );
542 outside.exec(`
543 PRAGMA journal_mode = WAL;
544 PRAGMA wal_autocheckpoint = 0;
545 CREATE TABLE blobs(data BLOB NOT NULL);
546 `);
547 const walSize = fs.stat(`${filename}-wal`).pipe(
548 Effect.map((info) => Number(info.size)),
549 Effect.orDie,
550 );
551 yield* Effect.gen(function* () {
552 const sql = yield* SqlClient.SqlClient;
553 yield* sql.unsafe(`PRAGMA journal_size_limit = ${WAL_SIZE_LIMIT}`);
554 return yield* use({ sql, worker: startedWorkers.at(-1)!, outside, walSize });
555 }).pipe(Effect.provide(SqliteClient.layer({ filename, checkpointWorker: true })));
556 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
557
558const openReadTransaction = (db: NodeSqlite.DatabaseSync) => {
559 db.exec("BEGIN");
560 db.prepare("SELECT COUNT(*) FROM blobs").get();
561};
562
563it.effect("keeps admitting writes while an outside read transaction pins the WAL", () =>
564 withOutsideConnection(({ sql, worker, outside }) =>
565 Effect.gen(function* () {
566 yield* sql`INSERT INTO blobs VALUES (${PAYLOAD})`;
567 openReadTransaction(outside);
568 // 140 MiB passes both the 32 MiB size limit and the 32,768-frame ceiling.
569 for (let write = 1; write <= 140; write++) {
570 yield* insertUnlessHeld(sql, worker, `write ${write}`);
571 }
572 const rows = yield* sql<{ count: number }>`SELECT COUNT(*) AS count FROM blobs`;
573 assert.equal(rows[0]?.count, 141);
574 }),
575 ),
576);
577
578it.effect("keeps admitting writes while a reader stops TRUNCATE, then resets the WAL", () =>
579 withOutsideConnection(({ sql, worker, outside, walSize }) =>
580 Effect.gen(function* () {
581 const insert = outside.prepare("INSERT INTO blobs VALUES (?)");
582 for (let write = 1; write <= 40; write++) insert.run(PAYLOAD);
583 // A snapshot of an unbackfilled WAL lets PASSIVE finish but stops TRUNCATE.
584 openReadTransaction(outside);
585 const stopped = yield* Deferred.make<void>();
586 const reset = yield* Deferred.make<void>();
587 worker.on("message", (message: { type?: unknown; truncateId?: unknown; log?: unknown }) => {
588 if (message?.type !== "checkpoint" || message.truncateId === undefined) return;
589 Deferred.doneUnsafe(message.log === 0 ? reset : stopped, Effect.void);
590 });
591 // Starts a checkpoint without appending a frame past the reader's snapshot.
592 yield* sql`DELETE FROM blobs WHERE 0`;
593 yield* Deferred.await(stopped);
594 yield* insertUnlessHeld(sql, worker, "a write after the failed TRUNCATE");
595 assert.isAbove(yield* walSize, WAL_SIZE_LIMIT);
596 outside.exec("COMMIT");
597 // No further write arrives; the worker has to retry on its own.
598 yield* Deferred.await(reset);
599 assert.equal(yield* walSize, 0);
600 }),
601 ),
602);