mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
refactor(core): use Effect's official Bun and Node SQLite clients (#60798)
This commit is contained in:
committed by
GitHub
Unverified
parent
7ce35879b8
commit
b95c1f379c
@@ -251,6 +251,8 @@
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.19",
|
||||
"@ai-sdk/provider-utils": "4.0.62",
|
||||
"@effect/sql-sqlite-bun": "catalog:",
|
||||
"@effect/sql-sqlite-node": "catalog:",
|
||||
"@ff-labs/fff-bun": "0.10.5",
|
||||
"@ff-labs/fff-node": "0.10.5",
|
||||
"@lydell/node-pty": "catalog:",
|
||||
@@ -288,7 +290,6 @@
|
||||
"@ai-sdk/openai-compatible": "2.0.41",
|
||||
"@anthropic-ai/claude-agent-sdk": "0.3.301",
|
||||
"@effect/platform-node": "catalog:",
|
||||
"@effect/sql-sqlite-bun": "catalog:",
|
||||
"@opencode/http-recorder": "workspace:*",
|
||||
"@parcel/watcher-darwin-arm64": "2.5.1",
|
||||
"@parcel/watcher-darwin-x64": "2.5.1",
|
||||
@@ -1047,6 +1048,7 @@
|
||||
"@effect/platform-node": "4.1.0",
|
||||
"@effect/platform-node-shared": "4.1.0",
|
||||
"@effect/sql-sqlite-bun": "4.1.0",
|
||||
"@effect/sql-sqlite-node": "4.1.0",
|
||||
"@kobalte/core": "0.13.13",
|
||||
"@lydell/node-pty": "1.2.0-beta.12",
|
||||
"@npmcli/arborist": "9.4.0",
|
||||
@@ -1538,6 +1540,8 @@
|
||||
|
||||
"@effect/sql-sqlite-bun": ["@effect/sql-sqlite-bun@4.1.0", "", { "peerDependencies": { "effect": "^4.1.0" } }, "sha512-V05HFjGmSnzIvXuxiHABw67SNf70dNbIvhUfyM7woDrgOu0JlqA8BTIhrwEPy0+GPwyvS5ALolcyNOMRgPVxyA=="],
|
||||
|
||||
"@effect/sql-sqlite-node": ["@effect/sql-sqlite-node@4.1.0", "", { "peerDependencies": { "effect": "^4.1.0" } }, "sha512-Z8PxzlVlfp5JCGYAg9CwehHkvLEwMf/NF7htdDkKy65G4vE78ARkDMUSyeIzZ1zpadMxbbCEUJ2FM9M5/0Nxgg=="],
|
||||
|
||||
"@electron-internal/extract-zip": ["@electron-internal/extract-zip@1.0.5", "", {}, "sha512-+bqFCP98pLI0Tt0XQo1TmlXtwjWchISndDOxCkEcIuUgXWpBnLyRI+2DU+mesvnMMX6L1XDqYNA0lXNDHd/yiA=="],
|
||||
|
||||
"@electron/asar": ["@electron/asar@3.4.1", "", { "dependencies": { "commander": "^5.0.0", "glob": "^7.1.6", "minimatch": "^3.0.4" }, "bin": { "asar": "bin/asar.js" } }, "sha512-i4/rNPRS84t0vSRa2HorerGRXWyF4vThfHesw0dmcWHp+cspK743UanA0suA5Q5y8kzY2y6YKrvbIUn69BCAiA=="],
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
"@effect/platform-node": "4.1.0",
|
||||
"@effect/platform-node-shared": "4.1.0",
|
||||
"@effect/sql-sqlite-bun": "4.1.0",
|
||||
"@effect/sql-sqlite-node": "4.1.0",
|
||||
"@npmcli/arborist": "9.4.0",
|
||||
"@types/bun": "1.4.3",
|
||||
"@types/cross-spawn": "6.0.6",
|
||||
|
||||
@@ -78,7 +78,6 @@
|
||||
"@ai-sdk/openai-compatible": "2.0.41",
|
||||
"@anthropic-ai/claude-agent-sdk": "0.3.301",
|
||||
"@effect/platform-node": "catalog:",
|
||||
"@effect/sql-sqlite-bun": "catalog:",
|
||||
"@opencode/http-recorder": "workspace:*",
|
||||
"@parcel/watcher-darwin-arm64": "2.5.1",
|
||||
"@parcel/watcher-darwin-x64": "2.5.1",
|
||||
@@ -98,6 +97,8 @@
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.19",
|
||||
"@ai-sdk/provider-utils": "4.0.62",
|
||||
"@effect/sql-sqlite-bun": "catalog:",
|
||||
"@effect/sql-sqlite-node": "catalog:",
|
||||
"@ff-labs/fff-bun": "0.10.5",
|
||||
"@ff-labs/fff-node": "0.10.5",
|
||||
"@lydell/node-pty": "catalog:",
|
||||
|
||||
@@ -1,103 +1,13 @@
|
||||
import { Database, type SQLQueryBindings } from "bun:sqlite"
|
||||
import { Context, Effect, Layer } from "effect"
|
||||
import { Reactivity } from "effect/reactivity"
|
||||
import { SqlClient } from "effect/sql"
|
||||
import { classifySqliteError, SqlError } from "effect/sql/SqlError"
|
||||
import { Sqlite } from "./sqlite.js"
|
||||
|
||||
const TypeId = "~@opencode/core/database/SqliteBun" as const
|
||||
import { SqliteClient } from "@effect/sql-sqlite-bun"
|
||||
import { Layer } from "effect"
|
||||
import type { SqlClient } from "effect/sql"
|
||||
|
||||
export const supportsTuningPragmas = true
|
||||
|
||||
// Foreign keys default OFF and can be toggled per connection.
|
||||
export const supportsForeignKeyToggle = true
|
||||
|
||||
interface Config extends Sqlite.ClientConfig {
|
||||
readonly filename: string
|
||||
readonly readonly?: boolean
|
||||
readonly create?: boolean
|
||||
readonly readwrite?: boolean
|
||||
readonly disableWAL?: boolean
|
||||
}
|
||||
|
||||
const make = (options: Config) =>
|
||||
Effect.gen(function* () {
|
||||
const native = (yield* Sqlite.Native) as Database
|
||||
|
||||
const run = (query: string, params: ReadonlyArray<unknown> = []) =>
|
||||
Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => {
|
||||
const statement = native.query<Record<string, unknown>, SQLQueryBindings[]>(query)
|
||||
// @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627
|
||||
statement.safeIntegers(Context.get(fiber.context, SqlClient.SafeIntegers))
|
||||
try {
|
||||
return Effect.succeed(statement.all(...(params as SQLQueryBindings[])) ?? [])
|
||||
} catch (cause) {
|
||||
return Effect.fail(
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const runValues = (query: string, params: ReadonlyArray<unknown> = []) =>
|
||||
Effect.withFiber<Array<unknown[]>, SqlError>((fiber) => {
|
||||
const statement = native.query<unknown, SQLQueryBindings[]>(query)
|
||||
// @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627
|
||||
statement.safeIntegers(Context.get(fiber.context, SqlClient.SafeIntegers))
|
||||
try {
|
||||
return Effect.succeed(statement.values(...(params as SQLQueryBindings[])) ?? [])
|
||||
} catch (cause) {
|
||||
return Effect.fail(
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const connection = Sqlite.makeConnection(run, runValues, {
|
||||
export: Effect.try({
|
||||
try: () => native.serialize(),
|
||||
catch: (cause) =>
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to export database", operation: "export" }),
|
||||
}),
|
||||
}),
|
||||
loadExtension: (path: string) =>
|
||||
Effect.try({
|
||||
try: () => native.loadExtension(path),
|
||||
catch: (cause) =>
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to load extension", operation: "loadExtension" }),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
return yield* Sqlite.makeClient(options, connection, TypeId, (acquirer) => ({
|
||||
export: Effect.flatMap(acquirer, (_) => _.export),
|
||||
loadExtension: (path: string) => Effect.flatMap(acquirer, (_) => _.loadExtension(path)),
|
||||
}))
|
||||
})
|
||||
|
||||
const nativeLayer = (config: Config) =>
|
||||
Layer.effect(
|
||||
Sqlite.Native,
|
||||
Effect.gen(function* () {
|
||||
const native = new Database(config.filename, {
|
||||
readonly: config.readonly,
|
||||
readwrite: config.readwrite ?? true,
|
||||
create: config.create ?? true,
|
||||
})
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
|
||||
if (config.disableWAL !== true) native.run("PRAGMA journal_mode = WAL;")
|
||||
return native
|
||||
}),
|
||||
)
|
||||
|
||||
const clientLayer = (config: Config) => Layer.effect(SqlClient.SqlClient, make(config))
|
||||
|
||||
export const sqliteLayer = (config: Config) => {
|
||||
const native = nativeLayer(config)
|
||||
return Layer.merge(native, clientLayer(config).pipe(Layer.provide(native))).pipe(Layer.provide(Reactivity.layer))
|
||||
}
|
||||
// Opening the database can only fail on a broken file or a lock held past
|
||||
// busyTimeout; Database.layer treats both as fatal.
|
||||
export const sqliteLayer = (config: SqliteClient.SqliteClientConfig): Layer.Layer<SqlClient.SqlClient> =>
|
||||
Layer.orDie(SqliteClient.layer(config))
|
||||
|
||||
@@ -1,100 +1,13 @@
|
||||
import { DatabaseSync, type SQLInputValue } from "node:sqlite"
|
||||
import { Context, Effect, Layer } from "effect"
|
||||
import { Reactivity } from "effect/reactivity"
|
||||
import { SqlClient } from "effect/sql"
|
||||
import { classifySqliteError, SqlError } from "effect/sql/SqlError"
|
||||
import { Sqlite } from "./sqlite.js"
|
||||
|
||||
const TypeId = "~@opencode/core/database/SqliteNode" as const
|
||||
import { SqliteClient } from "@effect/sql-sqlite-node"
|
||||
import { Layer } from "effect"
|
||||
import type { SqlClient } from "effect/sql"
|
||||
|
||||
export const supportsTuningPragmas = true
|
||||
|
||||
// Foreign keys default OFF and can be toggled per connection.
|
||||
// Foreign keys default ON in node:sqlite and can be toggled per connection.
|
||||
export const supportsForeignKeyToggle = true
|
||||
|
||||
interface Config extends Sqlite.ClientConfig {
|
||||
readonly filename: string
|
||||
readonly readonly?: boolean
|
||||
readonly create?: boolean
|
||||
readonly readwrite?: boolean
|
||||
readonly disableWAL?: boolean
|
||||
readonly timeout?: number
|
||||
readonly allowExtension?: boolean
|
||||
}
|
||||
|
||||
const make = (options: Config) =>
|
||||
Effect.gen(function* () {
|
||||
const native = (yield* Sqlite.Native) as DatabaseSync
|
||||
|
||||
const run = (query: string, params: ReadonlyArray<unknown> = []) =>
|
||||
Effect.withFiber<Array<Record<string, unknown>>, SqlError>((fiber) => {
|
||||
const statement = native.prepare(query)
|
||||
statement.setReadBigInts(Context.get(fiber.context, SqlClient.SafeIntegers))
|
||||
try {
|
||||
return Effect.succeed(statement.all(...(params as SQLInputValue[])) as Array<Record<string, unknown>>)
|
||||
} catch (cause) {
|
||||
return Effect.fail(
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const runValues = (query: string, params: ReadonlyArray<unknown> = []) =>
|
||||
Effect.withFiber<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>((fiber) => {
|
||||
const statement = native.prepare(query)
|
||||
statement.setReadBigInts(Context.get(fiber.context, SqlClient.SafeIntegers))
|
||||
statement.setReturnArrays(true)
|
||||
try {
|
||||
return Effect.succeed(
|
||||
statement.all(...(params as SQLInputValue[])) as unknown as ReadonlyArray<ReadonlyArray<unknown>>,
|
||||
)
|
||||
} catch (cause) {
|
||||
return Effect.fail(
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to execute statement", operation: "execute" }),
|
||||
}),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
const connection = Sqlite.makeConnection(run, runValues, {
|
||||
loadExtension: (path: string) =>
|
||||
Effect.try({
|
||||
try: () => native.loadExtension(path),
|
||||
catch: (cause) =>
|
||||
new SqlError({
|
||||
reason: classifySqliteError(cause, { message: "Failed to load extension", operation: "loadExtension" }),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
return yield* Sqlite.makeClient(options, connection, TypeId, (acquirer) => ({
|
||||
loadExtension: (path: string) => Effect.flatMap(acquirer, (_) => _.loadExtension(path)),
|
||||
}))
|
||||
})
|
||||
|
||||
const nativeLayer = (config: Config) =>
|
||||
Layer.effect(
|
||||
Sqlite.Native,
|
||||
Effect.gen(function* () {
|
||||
const native = new DatabaseSync(config.filename, {
|
||||
readOnly: config.readonly,
|
||||
timeout: config.timeout,
|
||||
allowExtension: config.allowExtension,
|
||||
enableForeignKeyConstraints: true,
|
||||
open: true,
|
||||
})
|
||||
yield* Effect.addFinalizer(() => Effect.sync(() => native.close()))
|
||||
if (config.disableWAL !== true && config.readonly !== true) native.exec("PRAGMA journal_mode = WAL;")
|
||||
return native
|
||||
}),
|
||||
)
|
||||
|
||||
const clientLayer = (config: Config) => Layer.effect(SqlClient.SqlClient, make(config))
|
||||
|
||||
export const sqliteLayer = (config: Config) => {
|
||||
const native = nativeLayer(config)
|
||||
return Layer.merge(native, clientLayer(config).pipe(Layer.provide(native))).pipe(Layer.provide(Reactivity.layer))
|
||||
}
|
||||
// Opening the database can only fail on a broken file or a lock held past
|
||||
// busyTimeout; Database.layer treats both as fatal.
|
||||
export const sqliteLayer = (config: SqliteClient.SqliteClientConfig): Layer.Layer<SqlClient.SqlClient> =>
|
||||
Layer.orDie(SqliteClient.layer(config))
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
export * as Sqlite from "./sqlite.js"
|
||||
|
||||
import { Context, Effect, type Scope, Semaphore, Stream } from "effect"
|
||||
import { identity } from "effect/Function"
|
||||
import { SqlClient, Statement } from "effect/sql"
|
||||
import type { Connection } from "effect/sql/SqlConnection"
|
||||
import type { SqlError } from "effect/sql/SqlError"
|
||||
|
||||
export class Native extends Context.Service<Native, unknown>()("@opencode/core/database/SqliteNative") {}
|
||||
|
||||
export interface ClientConfig {
|
||||
readonly spanAttributes?: Record<string, unknown>
|
||||
readonly transformResultNames?: (str: string) => string
|
||||
readonly transformQueryNames?: (str: string) => string
|
||||
}
|
||||
|
||||
type Run = (
|
||||
query: string,
|
||||
params?: ReadonlyArray<unknown>,
|
||||
) => Effect.Effect<ReadonlyArray<Record<string, unknown>>, SqlError>
|
||||
|
||||
type RunValues = (
|
||||
query: string,
|
||||
params?: ReadonlyArray<unknown>,
|
||||
) => Effect.Effect<ReadonlyArray<ReadonlyArray<unknown>>, SqlError>
|
||||
|
||||
export const makeConnection = <Extensions extends object>(run: Run, runValues: RunValues, extensions: Extensions) =>
|
||||
identity<Connection & Extensions>({
|
||||
execute(query, params, transformRows) {
|
||||
return transformRows ? Effect.map(run(query, params), transformRows) : run(query, params)
|
||||
},
|
||||
executeRaw(query, params) {
|
||||
return run(query, params)
|
||||
},
|
||||
executeValues(query, params) {
|
||||
return runValues(query, params)
|
||||
},
|
||||
executeValuesUnprepared(query, params) {
|
||||
return runValues(query, params)
|
||||
},
|
||||
executeUnprepared(query, params, transformRows) {
|
||||
return this.execute(query, params, transformRows)
|
||||
},
|
||||
executeStream() {
|
||||
return Stream.die("executeStream not implemented")
|
||||
},
|
||||
...extensions,
|
||||
})
|
||||
|
||||
export const makeClient = <
|
||||
Config extends ClientConfig,
|
||||
SqliteConnection extends Connection,
|
||||
const TypeId extends string,
|
||||
Extensions extends object,
|
||||
>(
|
||||
options: Config,
|
||||
connection: SqliteConnection,
|
||||
typeId: TypeId,
|
||||
extensions: (acquirer: Effect.Effect<SqliteConnection, SqlError, Scope.Scope>) => Extensions,
|
||||
) =>
|
||||
Effect.gen(function* () {
|
||||
const semaphore = yield* Semaphore.make(1)
|
||||
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
||||
const transactionAcquirer = Effect.uninterruptibleMask((restore) =>
|
||||
Effect.as(restore(semaphore.takeScoped(1)), connection),
|
||||
)
|
||||
const transformRows = options.transformResultNames
|
||||
? Statement.defaultTransforms(options.transformResultNames).array
|
||||
: undefined
|
||||
|
||||
return Object.assign(
|
||||
yield* SqlClient.make({
|
||||
acquirer,
|
||||
compiler: Statement.makeCompilerSqlite(options.transformQueryNames),
|
||||
transactionAcquirer,
|
||||
driver: "sqlite",
|
||||
spanAttributes: [
|
||||
...(options.spanAttributes ? Object.entries(options.spanAttributes) : []),
|
||||
["db.system.name", "sqlite"],
|
||||
],
|
||||
transformRows,
|
||||
}),
|
||||
{
|
||||
[typeId]: typeId,
|
||||
config: options,
|
||||
...extensions(acquirer),
|
||||
},
|
||||
) as SqlClient.SqlClient &
|
||||
Record<TypeId, TypeId> & {
|
||||
readonly config: Config
|
||||
readonly updateValues: never
|
||||
} & Extensions
|
||||
})
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Context, Effect, Exit, Layer, Semaphore } from "effect"
|
||||
import { Context, Effect, Exit, Layer, Semaphore, Stream } from "effect"
|
||||
import { Reactivity } from "effect/reactivity"
|
||||
import { SqlClient, Statement } from "effect/sql"
|
||||
import type { Connection } from "effect/sql/SqlConnection"
|
||||
import { classifySqliteError, SqlError, UnknownError } from "effect/sql/SqlError"
|
||||
import type { NativeTransactionSqlClient } from "./drizzle/effect-sqlite/session.js"
|
||||
import { Sqlite } from "./sqlite.js"
|
||||
|
||||
const ATTR_DB_SYSTEM_NAME = "db.system.name"
|
||||
|
||||
@@ -122,7 +121,7 @@ const makeWithTransaction =
|
||||
|
||||
const make = (options: Config) =>
|
||||
Effect.gen(function* () {
|
||||
const native = (yield* Sqlite.Native) as DurableObjectStorage
|
||||
const native = options.storage
|
||||
|
||||
const compiler = Statement.makeCompilerSqlite(options.transformQueryNames)
|
||||
const transformRows = options.transformResultNames
|
||||
@@ -166,7 +165,16 @@ const make = (options: Config) =>
|
||||
}),
|
||||
})
|
||||
|
||||
const connection = Sqlite.makeConnection(run, runValues, {})
|
||||
const connection: Connection = {
|
||||
execute: (query, params, transformRows) =>
|
||||
transformRows ? Effect.map(run(query, params), transformRows) : run(query, params),
|
||||
executeRaw: (query, params) => run(query, params),
|
||||
executeValues: (query, params) => runValues(query, params),
|
||||
executeValuesUnprepared: (query, params) => runValues(query, params),
|
||||
executeUnprepared: (query, params, transformRows) =>
|
||||
transformRows ? Effect.map(run(query, params), transformRows) : run(query, params),
|
||||
executeStream: () => Stream.die("executeStream not implemented"),
|
||||
}
|
||||
|
||||
const semaphore = yield* Semaphore.make(1)
|
||||
const acquirer = semaphore.withPermits(1)(Effect.succeed(connection))
|
||||
@@ -202,19 +210,14 @@ const make = (options: Config) =>
|
||||
|
||||
// Defends against the shared path-based Database.layer, which passes a
|
||||
// filename instead of storage when resolved under the workerd condition.
|
||||
const nativeLayer = (config: Config) =>
|
||||
export const sqliteLayer = (config: Config): Layer.Layer<SqlClient.SqlClient> =>
|
||||
config.storage
|
||||
? Layer.succeed(Sqlite.Native, config.storage)
|
||||
? Layer.effect(SqlClient.SqlClient, make(config)).pipe(Layer.provide(Reactivity.layer))
|
||||
: Layer.effect(
|
||||
Sqlite.Native,
|
||||
SqlClient.SqlClient,
|
||||
Effect.die(
|
||||
"workerd sqlite cannot open a database from a path; use Database.layerFromClient.pipe(Layer.provide(sqliteLayer({ storage })))",
|
||||
new Error(
|
||||
"workerd sqlite cannot open a database from a path; use Database.layerFromClient.pipe(Layer.provide(sqliteLayer({ storage })))",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
const clientLayer = (config: Config) => Layer.effect(SqlClient.SqlClient, make(config))
|
||||
|
||||
export const sqliteLayer = (config: Config) => {
|
||||
const native = nativeLayer(config)
|
||||
return Layer.merge(native, clientLayer(config).pipe(Layer.provide(native))).pipe(Layer.provide(Reactivity.layer))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Bundled for Node by sqlite-node.test.ts and run with `node <dir>`, so #sqlite
|
||||
// resolves to the node:sqlite client that the standalone Node build ships.
|
||||
import { join } from "node:path"
|
||||
import { Global } from "@opencode/util/global"
|
||||
import { sql } from "drizzle-orm"
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import { Cause, Effect, Fiber, Layer, References } from "effect"
|
||||
import { SqlClient } from "effect/sql"
|
||||
import { isSqlError } from "effect/sql/SqlError"
|
||||
import { Database } from "../../src/database/database"
|
||||
import { sqliteLayer } from "../../src/database/sqlite.node"
|
||||
|
||||
const typed = sqliteTable("typed", { id: integer().primaryKey(), name: text().notNull() })
|
||||
|
||||
// Same sweep as sqlite-interrupt.test.ts. It runs here because
|
||||
// @effect/sql-sqlite-node cannot load inside `bun test` on macOS.
|
||||
const sweep = (queue: (sql: SqlClient.SqlClient) => Effect.Effect<unknown, unknown>) =>
|
||||
Effect.gen(function* () {
|
||||
const leaked: Array<number> = []
|
||||
for (let delay = 0; delay < 31; delay++) {
|
||||
yield* Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient
|
||||
const holder = yield* Effect.forkChild(
|
||||
Effect.scoped(Effect.andThen(sql.reserve, Effect.andThen(Effect.yieldNow, Effect.yieldNow))),
|
||||
)
|
||||
const queued = yield* queue(sql).pipe(
|
||||
Effect.provideService(References.MaxOpsBeforeYield, 3),
|
||||
Effect.forkChild,
|
||||
)
|
||||
for (let i = 0; i < delay; i++) yield* Effect.yieldNow
|
||||
yield* Fiber.interrupt(queued)
|
||||
yield* Fiber.join(holder)
|
||||
const next = yield* Effect.forkChild(sql`SELECT 3`, { startImmediately: true })
|
||||
if (next.pollUnsafe() === undefined) {
|
||||
leaked.push(delay)
|
||||
yield* Fiber.interrupt(next)
|
||||
}
|
||||
}).pipe(Effect.provide(sqliteLayer({ filename: ":memory:", disableWAL: true }))),
|
||||
)
|
||||
}
|
||||
return leaked
|
||||
})
|
||||
|
||||
const dir = process.argv[2]!
|
||||
|
||||
const program = Effect.gen(function* () {
|
||||
const { db } = yield* Database.Service
|
||||
const journal = yield* db.get<{ journal_mode: string }>(sql`PRAGMA journal_mode`)
|
||||
yield* db.run(sql`CREATE TABLE probe (id TEXT PRIMARY KEY)`)
|
||||
yield* db.transaction((tx) =>
|
||||
Effect.all([tx.run(sql`INSERT INTO probe (id) VALUES ('a')`), tx.run(sql`INSERT INTO probe (id) VALUES ('b')`)]),
|
||||
)
|
||||
const error = yield* db.run(sql`INSERT INTO probe (id) VALUES ('a')`).pipe(Effect.flip)
|
||||
const cause = Cause.isCause(error.cause) ? Cause.squash(error.cause) : error.cause
|
||||
const rows = yield* db.all<{ count: number }>(sql`SELECT count(*) AS count FROM probe`)
|
||||
|
||||
yield* db.run(sql`CREATE TABLE typed (id integer PRIMARY KEY, name text NOT NULL)`)
|
||||
const inserted = yield* db.insert(typed).values({ id: 1, name: "Ada" }).returning()
|
||||
const duplicate = yield* db.insert(typed).values({ id: 1, name: "Bob" }).returning().pipe(Effect.flip)
|
||||
const afterFailure = yield* db.select().from(typed)
|
||||
const raw = yield* db.get<{ count: number }>(sql`SELECT count(*) AS count FROM typed`)
|
||||
|
||||
const leakedTransaction = yield* sweep((client) => client.withTransaction(client`SELECT 2`))
|
||||
const leakedReserve = yield* sweep((client) => Effect.scoped(Effect.andThen(client.reserve, Effect.yieldNow)))
|
||||
return {
|
||||
journal: journal?.journal_mode,
|
||||
reason: isSqlError(cause) ? cause.reason._tag : "not a SqlError",
|
||||
rows: rows[0]?.count,
|
||||
inserted,
|
||||
duplicate: duplicate._tag,
|
||||
afterFailure,
|
||||
raw: raw?.count,
|
||||
leakedTransaction,
|
||||
leakedReserve,
|
||||
}
|
||||
})
|
||||
|
||||
const global = Global.layerWith({
|
||||
data: dir,
|
||||
cache: dir,
|
||||
config: dir,
|
||||
state: dir,
|
||||
tmp: dir,
|
||||
bin: dir,
|
||||
log: dir,
|
||||
repos: dir,
|
||||
})
|
||||
|
||||
const database = Database.layer({ path: join(dir, "opencode.db") }).pipe(Layer.provide(global))
|
||||
console.log(JSON.stringify(await Effect.runPromise(program.pipe(Effect.provide(database)))))
|
||||
@@ -3,10 +3,11 @@ import type { BunPlugin } from "bun"
|
||||
import { join } from "path"
|
||||
|
||||
// Pins the #sqlite subpath-import condition gate: bundling database.ts for a
|
||||
// non-Bun runtime must never reach the static `import "bun:sqlite"` in
|
||||
// sqlite.bun.ts, which crashes workerd (and any other non-Bun bundle) at
|
||||
// module load. Bare imports are externalized so only core-relative modules —
|
||||
// including the #sqlite resolution under test — end up in the bundle.
|
||||
// non-Bun runtime must never reach @effect/sql-sqlite-bun (and its static
|
||||
// `import "bun:sqlite"`), which crashes workerd (and any other non-Bun bundle)
|
||||
// at module load. Bare imports are externalized so only core-relative modules —
|
||||
// including the #sqlite resolution under test — end up in the bundle, and the
|
||||
// client package each runtime picked stays visible as an import specifier.
|
||||
const externalizeBare: BunPlugin = {
|
||||
name: "externalize-bare",
|
||||
setup(build) {
|
||||
@@ -38,12 +39,15 @@ describe.skipIf(process.platform === "win32")("sqlite bundle conditions", () =>
|
||||
const output = await bundle(["workerd"])
|
||||
expect(output).not.toContain("bun:sqlite")
|
||||
expect(output).not.toContain("node:sqlite")
|
||||
expect(output).not.toContain("@effect/sql-sqlite-bun")
|
||||
expect(output).not.toContain("@effect/sql-sqlite-node")
|
||||
expect(output).toContain("SqliteWorkerd")
|
||||
})
|
||||
|
||||
test("default conditions fall back to node:sqlite, not bun:sqlite", async () => {
|
||||
const output = await bundle([])
|
||||
test.each([[["node"]], [[]]])("%p conditions select the Node client, not bun:sqlite", async (conditions) => {
|
||||
const output = await bundle(conditions)
|
||||
expect(output).not.toContain("bun:sqlite")
|
||||
expect(output).toContain("SqliteNode")
|
||||
expect(output).not.toContain("@effect/sql-sqlite-bun")
|
||||
expect(output).toContain("@effect/sql-sqlite-node")
|
||||
})
|
||||
})
|
||||
|
||||
@@ -48,6 +48,9 @@ const sweep = (
|
||||
}),
|
||||
)
|
||||
|
||||
// The Node client is swept in fixture/sqlite-node-client.ts under real Node,
|
||||
// because @effect/sql-sqlite-node cannot load inside `bun test` on macOS. It
|
||||
// shares makeSqliteAcquirers with the Bun client.
|
||||
describe("sqlite transaction interrupts", () => {
|
||||
test("bun client releases the connection when a queued transaction is interrupted", async () => {
|
||||
const leaked = await sweep(
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import { mkdtemp, rm } from "node:fs/promises"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { Effect } from "effect"
|
||||
import { SqlClient } from "effect/sql"
|
||||
import { sqliteLayer } from "@opencode/core/database/sqlite.bun"
|
||||
|
||||
const node = Bun.which("node")
|
||||
|
||||
// Skipped on Windows for the same Bun.build panic as sqlite-bundle.test.ts.
|
||||
describe.skipIf(process.platform === "win32" || !node)("node sqlite client", () => {
|
||||
test("boots migrations, commits drizzle transactions and types constraint errors", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "sqlite-node-"))
|
||||
try {
|
||||
const build = await Bun.build({
|
||||
entrypoints: [join(import.meta.dir, "fixture/sqlite-node-client.ts")],
|
||||
target: "node",
|
||||
conditions: ["node"],
|
||||
outdir: join(dir, "bundle"),
|
||||
throw: false,
|
||||
})
|
||||
expect(build.logs).toEqual([])
|
||||
expect(build.success).toBe(true)
|
||||
const output = await build.outputs[0].text()
|
||||
expect(output).not.toContain("bun:sqlite")
|
||||
|
||||
const child = Bun.spawn([node!, build.outputs[0].path, dir], { stdout: "pipe", stderr: "pipe" })
|
||||
const [stdout, stderr, code] = await Promise.all([
|
||||
new Response(child.stdout).text(),
|
||||
new Response(child.stderr).text(),
|
||||
child.exited,
|
||||
])
|
||||
expect({ code, stderr: code === 0 ? "" : stderr }).toEqual({ code: 0, stderr: "" })
|
||||
expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({
|
||||
journal: "wal",
|
||||
reason: "ConstraintError",
|
||||
rows: 2,
|
||||
inserted: [{ id: 1, name: "Ada" }],
|
||||
duplicate: "EffectDrizzleQueryError",
|
||||
afterFailure: [{ id: 1, name: "Ada" }],
|
||||
raw: 1,
|
||||
leakedTransaction: [],
|
||||
leakedReserve: [],
|
||||
})
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
}, 30_000)
|
||||
})
|
||||
|
||||
describe("bun sqlite client", () => {
|
||||
test("types constraint errors and opens read-only", async () => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "sqlite-bun-"))
|
||||
const filename = join(dir, "probe.db")
|
||||
try {
|
||||
const reason = await Effect.runPromise(
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient
|
||||
yield* sql`CREATE TABLE probe (id TEXT PRIMARY KEY)`
|
||||
yield* sql`INSERT INTO probe (id) VALUES ('a')`
|
||||
const error = yield* sql`INSERT INTO probe (id) VALUES ('a')`.pipe(Effect.flip)
|
||||
return error.reason._tag
|
||||
}).pipe(Effect.provide(sqliteLayer({ filename }))),
|
||||
)
|
||||
expect(reason).toBe("ConstraintError")
|
||||
|
||||
const rows = await Effect.runPromise(
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient
|
||||
return yield* sql<{ id: string }>`SELECT id FROM probe`
|
||||
}).pipe(Effect.provide(sqliteLayer({ filename, readonly: true }))),
|
||||
)
|
||||
expect(rows).toEqual([{ id: "a" }])
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user