Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6
7import * as NodeServices from "@effect/platform-node/NodeServices";
8import { assert, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as SqlClient from "effect/sql/SqlClient";
12
13import { WAL_SIZE_LIMIT_BYTES } from "./Sqlite.ts";
14import * as SqlitePersistence from "./Sqlite.ts";
15
16const lockHolderSource = `
17const { DatabaseSync } = require("node:sqlite");
18const db = new DatabaseSync(process.argv[1]);
19db.exec("BEGIN IMMEDIATE");
20process.stdout.write("locked\\n");
21setTimeout(() => {
22 db.exec("COMMIT");
23 db.close();
24}, Number(process.argv[2]));
25`;
26
27const spawnWriteLockHolder = (dbPath: string, holdMs: number) =>
28 Effect.promise(
29 () =>
30 new Promise<void>((resolve, reject) => {
31 const holder = NodeChildProcess.spawn(
32 process.execPath,
33 ["-e", lockHolderSource, dbPath, String(holdMs)],
34 { stdio: ["ignore", "pipe", "ignore"] },
35 );
36 holder.stdout.once("data", () => resolve());
37 holder.on("error", reject);
38 holder.on("exit", () =>
39 reject(new Error("lock holder exited before acquiring the write lock")),
40 );
41 }),
42 );
43
44it.effect("waits out a concurrent writer instead of failing with SQLITE_BUSY", () => {
45 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-busy-"));
46 const dbPath = NodePath.join(tempDir, "state.sqlite");
47
48 return Effect.gen(function* () {
49 const sql = yield* SqlClient.SqlClient;
50 yield* sql`CREATE TABLE busy_probe(id INTEGER PRIMARY KEY)`;
51 yield* spawnWriteLockHolder(dbPath, 300);
52 yield* sql`INSERT INTO busy_probe(id) VALUES (${1})`;
53 const rows = yield* sql<{ readonly id: number }>`SELECT id FROM busy_probe`;
54 assert.deepEqual([...rows], [{ id: 1 }]);
55 }).pipe(
56 Effect.provide(SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer))),
57 Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
58 );
59});
60
61it.effect("shrinks the WAL file back to the size limit after a large write", () => {
62 const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-sqlite-wal-"));
63 const dbPath = NodePath.join(tempDir, "state.sqlite");
64 const walFileSize = () => NodeFS.statSync(`${dbPath}-wal`).size;
65 // About 25% more 4 KB rows than the limit holds, in one transaction.
66 const rowCount = Math.ceil((WAL_SIZE_LIMIT_BYTES * 1.25) / 4000);
67
68 return Effect.gen(function* () {
69 const sql = yield* SqlClient.SqlClient;
70 yield* sql`CREATE TABLE wal_probe(payload BLOB)`;
71 yield* sql`
72 WITH RECURSIVE n(i) AS (SELECT 1 UNION ALL SELECT i + 1 FROM n WHERE i < ${rowCount})
73 INSERT INTO wal_probe(payload) SELECT randomblob(4000) FROM n
74 `;
75 assert.isAbove(walFileSize(), WAL_SIZE_LIMIT_BYTES);
76
77 // The auto-checkpoint after the large commit copied every frame into the
78 // database, so the next commit restarts the WAL and cuts the file back.
79 yield* sql`INSERT INTO wal_probe(payload) VALUES (x'00')`;
80 assert.isAtMost(walFileSize(), WAL_SIZE_LIMIT_BYTES);
81 }).pipe(
82 Effect.provide(SqlitePersistence.layerFromPath(dbPath).pipe(Layer.provide(NodeServices.layer))),
83 Effect.ensuring(Effect.sync(() => NodeFS.rmSync(tempDir, { recursive: true, force: true }))),
84 );
85});
86
87it.effect("applies busy_timeout in the shared persistence setup", () =>
88 Effect.gen(function* () {
89 const sql = yield* SqlClient.SqlClient;
90 const rows = yield* sql<{ readonly timeout: number }>`PRAGMA busy_timeout`;
91 assert.equal(rows[0]?.timeout, 5000);
92 }).pipe(Effect.provide(SqlitePersistence.layerMemory)),
93);