Skip to content

Commit 5e699c9

Browse files
committed
chore(storage): update drizzle and channel db handling
1 parent e0ca52e commit 5e699c9

6 files changed

Lines changed: 33 additions & 19 deletions

File tree

bun.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"@tailwindcss/vite": "4.1.11",
4242
"diff": "8.0.2",
4343
"dompurify": "3.3.1",
44-
"drizzle-kit": "1.0.0-beta.12-a5629fb",
45-
"drizzle-orm": "1.0.0-beta.12-a5629fb",
44+
"drizzle-kit": "1.0.0-beta.16-ea816b6",
45+
"drizzle-orm": "1.0.0-beta.16-ea816b6",
4646
"ai": "5.0.124",
4747
"hono": "4.10.7",
4848
"hono-openapi": "1.1.2",

packages/opencode/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
"@types/yargs": "17.0.33",
4646
"@types/which": "3.0.4",
4747
"@typescript/native-preview": "catalog:",
48-
"drizzle-kit": "1.0.0-beta.12-a5629fb",
49-
"drizzle-orm": "1.0.0-beta.12-a5629fb",
48+
"drizzle-kit": "1.0.0-beta.16-ea816b6",
49+
"drizzle-orm": "1.0.0-beta.16-ea816b6",
5050
"typescript": "catalog:",
5151
"vscode-languageserver-types": "3.17.5",
5252
"why-is-node-running": "3.2.2",
@@ -106,7 +106,7 @@
106106
"clipboardy": "4.0.0",
107107
"decimal.js": "10.5.0",
108108
"diff": "catalog:",
109-
"drizzle-orm": "1.0.0-beta.12-a5629fb",
109+
"drizzle-orm": "1.0.0-beta.16-ea816b6",
110110
"fuzzysort": "3.1.0",
111111
"glob": "13.0.5",
112112
"google-auth-library": "10.5.0",
@@ -135,6 +135,6 @@
135135
"zod-to-json-schema": "3.24.5"
136136
},
137137
"overrides": {
138-
"drizzle-orm": "1.0.0-beta.12-a5629fb"
138+
"drizzle-orm": "1.0.0-beta.16-ea816b6"
139139
}
140140
}

packages/opencode/src/flag/flag.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export namespace Flag {
6060
export const OPENCODE_EXPERIMENTAL_MARKDOWN = !falsy("OPENCODE_EXPERIMENTAL_MARKDOWN")
6161
export const OPENCODE_MODELS_URL = process.env["OPENCODE_MODELS_URL"]
6262
export const OPENCODE_MODELS_PATH = process.env["OPENCODE_MODELS_PATH"]
63+
export const OPENCODE_DISABLE_CHANNEL_DB = truthy("OPENCODE_DISABLE_CHANNEL_DB")
6364

6465
function number(key: string) {
6566
const value = process.env[key]

packages/opencode/src/storage/db.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import z from "zod"
1212
import path from "path"
1313
import { readFileSync, readdirSync, existsSync } from "fs"
1414
import * as schema from "./schema"
15+
import { Installation } from "../installation"
16+
import { Flag } from "../flag/flag"
1517

16-
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number }[] | undefined
18+
declare const OPENCODE_MIGRATIONS: { sql: string; timestamp: number; name: string }[] | undefined
1719

1820
export const NotFoundError = NamedError.create(
1921
"NotFoundError",
@@ -25,13 +27,20 @@ export const NotFoundError = NamedError.create(
2527
const log = Log.create({ service: "db" })
2628

2729
export namespace Database {
28-
export const Path = path.join(Global.Path.data, "opencode.db")
30+
export const Path = (() => {
31+
const name =
32+
Installation.CHANNEL !== "latest" && !Flag.OPENCODE_DISABLE_CHANNEL_DB
33+
? `opencode-${Installation.CHANNEL}.db`
34+
: "opencode.db"
35+
return path.join(Global.Path.data, name)
36+
})()
37+
2938
type Schema = typeof schema
3039
export type Transaction = SQLiteTransaction<"sync", void, Schema>
3140

3241
type Client = SQLiteBunDatabase<Schema>
3342

34-
type Journal = { sql: string; timestamp: number }[]
43+
type Journal = { sql: string; timestamp: number; name: string }[]
3544

3645
const state = {
3746
sqlite: undefined as BunDatabase | undefined,
@@ -62,6 +71,7 @@ export namespace Database {
6271
return {
6372
sql: readFileSync(file, "utf-8"),
6473
timestamp: time(name),
74+
name,
6575
}
6676
})
6777
.filter(Boolean) as Journal
@@ -70,9 +80,9 @@ export namespace Database {
7080
}
7181

7282
export const Client = lazy(() => {
73-
log.info("opening database", { path: path.join(Global.Path.data, "opencode.db") })
83+
log.info("opening database", { path: Path })
7484

75-
const sqlite = new BunDatabase(path.join(Global.Path.data, "opencode.db"), { create: true })
85+
const sqlite = new BunDatabase(Path, { create: true })
7686
state.sqlite = sqlite
7787

7888
sqlite.run("PRAGMA journal_mode = WAL")
@@ -143,7 +153,7 @@ export namespace Database {
143153
} catch (err) {
144154
if (err instanceof Context.NotFound) {
145155
const effects: (() => void | Promise<void>)[] = []
146-
const result = Client().transaction((tx) => {
156+
const result = (Client().transaction as any)((tx: TxOrDb) => {
147157
return ctx.provide({ tx, effects }, () => callback(tx))
148158
})
149159
for (const effect of effects) effect()

packages/opencode/test/storage/json-migration.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function createTestDb() {
8484
.map((entry) => ({
8585
sql: readFileSync(path.join(dir, entry.name, "migration.sql"), "utf-8"),
8686
timestamp: Number(entry.name.split("_")[0]),
87+
name: entry.name,
8788
}))
8889
.sort((a, b) => a.timestamp - b.timestamp)
8990
migrate(drizzle({ client: sqlite }), migrations)

0 commit comments

Comments
 (0)