Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/read-only-bun-sqlite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/sql-sqlite-bun": patch
---

Enforce read-only mode when opening Bun SQLite databases.
9 changes: 5 additions & 4 deletions packages/sql/sqlite-bun/src/SqliteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,15 @@ export const make = (
undefined

const makeConnection = Effect.gen(function*() {
const readonly = options.readonly === true
const db = new Database(options.filename, {
readonly: options.readonly,
readwrite: options.readwrite ?? true,
create: options.create ?? true
readonly,
readwrite: readonly ? false : options.readwrite ?? true,
create: readonly ? false : options.create ?? true
} as any)
yield* Effect.addFinalizer(() => Effect.sync(() => db.close()))

if (options.disableWAL !== true) {
if (options.disableWAL !== true && !readonly) {
db.run("PRAGMA journal_mode = WAL;")
}

Expand Down
31 changes: 30 additions & 1 deletion packages/sql/sqlite-bun/test/Client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
import { describe, it } from "@effect/vitest"
import { assert, describe, it } from "@effect/vitest"
import { Effect } from "effect"
import { Reactivity } from "effect/unstable/reactivity"
import { rm } from "node:fs/promises"

const isBun = "bun" in process.versions

describe("Client", () => {
it.effect("should work", () => Effect.void)

it.effect.skipIf(!isBun)("readonly clients reject writes", () =>
Effect.gen(function*() {
const { SqliteClient } = yield* Effect.promise(() => import("@effect/sql-sqlite-bun"))
const filename = `/tmp/effect-sqlite-bun-readonly-${crypto.randomUUID()}.db`
yield* Effect.acquireRelease(
Effect.void,
() => Effect.promise(() => rm(filename, { force: true }))
)

yield* Effect.scoped(
Effect.gen(function*() {
const sql = yield* SqliteClient.make({ filename })
yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY)`
})
)

const sql = yield* SqliteClient.make({ filename, readonly: true })
assert.deepStrictEqual(yield* sql`SELECT * FROM test`, [])

const error = yield* Effect.flip(sql`INSERT INTO test DEFAULT VALUES`)
assert.strictEqual(error._tag, "SqlError")
assert(error.reason.cause instanceof Error)
assert.match(error.reason.cause.message, /attempt to write a readonly database/i)
}).pipe(Effect.provide(Reactivity.layer)))
})
Loading