Skip to content

Commit 1395113

Browse files
anandgupta42claude
andcommitted
fix(storage): make read-only Bun opens actually read-only
Bun's `Database` honours `readwrite`/`create` over `readonly`, so the default `readwrite: true, create: true` let a `readonly: true` open write and create a missing file. Disable both when `readonly` is set; the WAL/setup-lock skip for read-only opens now matches what the connection can do. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5a1f1c4 commit 1395113

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎packages/core/src/database/sqlite.bun.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,14 @@ const nativeLayer = (config: Config) =>
158158
Layer.effect(
159159
Sqlite.Native,
160160
Effect.gen(function* () {
161+
// altimate_change start — upstream_fix: Bun honours readwrite/create over readonly, so a read-only open
162+
// with the defaults could still write; read-only now disables both.
161163
const native = new Database(config.filename, {
162164
readonly: config.readonly,
163-
readwrite: config.readwrite ?? true,
164-
create: config.create ?? true,
165+
readwrite: config.readonly === true ? false : (config.readwrite ?? true),
166+
create: config.readonly === true ? false : (config.create ?? true),
165167
})
168+
// altimate_change end
166169
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
167170
// altimate_change start — upstream_fix: the WAL switch needs a write lock and can fail at once with
168171
// SQLITE_BUSY, so install the busy handler first and serialize the switch across processes.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { Database as BunDatabase } from "bun:sqlite"
3+
import path from "path"
4+
import { Effect } from "effect"
5+
import { layer } from "../src/database/sqlite.bun"
6+
import { Sqlite } from "../src/database/sqlite"
7+
import { tmpdir } from "./fixture/tmpdir"
8+
9+
describe("sqlite.bun read-only open", () => {
10+
test("rejects writes and leaves the journal mode alone", async () => {
11+
await using tmp = await tmpdir()
12+
const filename = path.join(tmp.path, "ro.sqlite")
13+
const seed = new BunDatabase(filename, { create: true })
14+
seed.run("CREATE TABLE t (x INTEGER)")
15+
seed.close()
16+
17+
const result = await Effect.runPromise(
18+
Effect.gen(function* () {
19+
const native = (yield* Sqlite.Native) as BunDatabase
20+
const mode = native.query("PRAGMA journal_mode").get() as { journal_mode: string }
21+
const write = (() => {
22+
try {
23+
native.run("INSERT INTO t VALUES (1)")
24+
return "written"
25+
} catch (error) {
26+
return (error as Error).message
27+
}
28+
})()
29+
return { mode: mode.journal_mode, write }
30+
}).pipe(Effect.provide(layer({ filename, readonly: true })), Effect.scoped),
31+
)
32+
33+
expect(result.mode).toBe("delete")
34+
expect(result.write).toContain("readonly")
35+
})
36+
37+
test("does not create a missing file", async () => {
38+
await using tmp = await tmpdir()
39+
const filename = path.join(tmp.path, "missing.sqlite")
40+
const exit = await Effect.runPromiseExit(
41+
Effect.gen(function* () {
42+
yield* Sqlite.Native
43+
}).pipe(Effect.provide(layer({ filename, readonly: true })), Effect.scoped),
44+
)
45+
expect(exit._tag).toBe("Failure")
46+
expect(await Bun.file(filename).exists()).toBe(false)
47+
})
48+
})

0 commit comments

Comments
 (0)