Skip to content

Commit d06f0ff

Browse files
fix(sqlite): retry failed statement preparations (#10584)
Co-authored-by: shivam <[email protected]>
1 parent e5a46d6 commit d06f0ff

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎packages/shared/src/nodeSqliteClient.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1+
import * as NodeSqlite from "node:sqlite";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
13
import { assert, it } from "@effect/vitest";
24
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
36
import * as Layer from "effect/Layer";
7+
import * as Path from "effect/Path";
48
import * as SqlClient from "effect/unstable/sql/SqlClient";
59

610
import * as SqliteClient from "./nodeSqliteClient.ts";
711

812
const layer = it.layer(SqliteClient.layer({ filename: ":memory:" }));
913

1014
layer("NodeSqliteClient", (it) => {
15+
it.effect("retries preparing a query after the missing schema becomes available", () =>
16+
Effect.gen(function* () {
17+
const sql = yield* SqlClient.SqlClient;
18+
const select = sql<{ name: string }>`SELECT name FROM created_after_prepare_failure`;
19+
const error = yield* select.pipe(Effect.flip);
20+
assert.equal(error._tag, "SqlError");
21+
assert.equal(error.reason.operation, "prepare");
22+
23+
yield* sql`CREATE TABLE created_after_prepare_failure(name TEXT NOT NULL)`;
24+
yield* sql`INSERT INTO created_after_prepare_failure VALUES ('recovered')`;
25+
assert.deepEqual(yield* select, [{ name: "recovered" }]);
26+
assert.deepEqual(yield* select.values, [["recovered"]]);
27+
}),
28+
);
29+
1130
it.effect("runs prepared queries and returns positional values", () =>
1231
Effect.gen(function* () {
1332
const sql = yield* SqlClient.SqlClient;
@@ -54,3 +73,32 @@ it.effect("returns a typed failure when the database cannot be opened", () =>
5473
assert.equal(error.reason.operation, "open");
5574
}),
5675
);
76+
77+
it.effect(
78+
"recovers a prepared query immediately after an exclusive database lock is released",
79+
() =>
80+
Effect.gen(function* () {
81+
const fs = yield* FileSystem.FileSystem;
82+
const path = yield* Path.Path;
83+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-prepare-" });
84+
const filename = path.join(directory, "state.sqlite");
85+
const blocker = yield* Effect.acquireRelease(
86+
Effect.sync(() => new NodeSqlite.DatabaseSync(filename)),
87+
(database) => Effect.sync(() => database.close()),
88+
);
89+
yield* Effect.sync(() => {
90+
blocker.exec("CREATE TABLE entries(value TEXT); INSERT INTO entries VALUES ('retained')");
91+
});
92+
yield* Effect.gen(function* () {
93+
const sql = yield* SqlClient.SqlClient;
94+
yield* Effect.sync(() => blocker.exec("BEGIN EXCLUSIVE"));
95+
const select = sql`SELECT value FROM entries`;
96+
const error = yield* select.values.pipe(Effect.flip);
97+
assert.equal(error._tag, "SqlError");
98+
assert.equal(error.reason.operation, "prepare");
99+
yield* Effect.sync(() => blocker.exec("ROLLBACK"));
100+
assert.deepEqual(yield* select.values, [["retained"]]);
101+
assert.deepEqual(yield* select, [{ value: "retained" }]);
102+
}).pipe(Effect.provide(SqliteClient.layer({ filename })));
103+
}).pipe(Effect.provide(NodeServices.layer)),
104+
);

‎packages/shared/src/nodeSqliteClient.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as NodeSqlite from "node:sqlite";
99
import * as Cache from "effect/Cache";
1010
import * as Duration from "effect/Duration";
1111
import * as Effect from "effect/Effect";
12+
import * as Exit from "effect/Exit";
1213
import * as Fiber from "effect/Fiber";
1314
import { identity } from "effect/Function";
1415
import * as Layer from "effect/Layer";
@@ -144,10 +145,11 @@ const make = Effect.fn("makeWithDatabase")(function* (
144145
}),
145146
});
146147

147-
const prepareCache = yield* Cache.make({
148+
const prepareCache = yield* Cache.makeWith(prepare, {
148149
capacity: options.prepareCacheSize ?? 200,
149-
timeToLive: options.prepareCacheTTL ?? Duration.minutes(10),
150-
lookup: prepare,
150+
// A transient prepare failure must not outlive the lock or missing schema.
151+
timeToLive: (exit) =>
152+
Exit.isSuccess(exit) ? (options.prepareCacheTTL ?? Duration.minutes(10)) : Duration.zero,
151153
});
152154

153155
const runStatement = (

0 commit comments

Comments
 (0)