Procházet zdrojové kódy

allow customizing DB location

Dax Raad před 1 měsícem
rodič
revize
5dc47905a9

+ 1 - 0
packages/opencode/src/flag/flag.ts

@@ -69,6 +69,7 @@ export namespace Flag {
   export const OPENCODE_EXPERIMENTAL_MARKDOWN = !falsy("OPENCODE_EXPERIMENTAL_MARKDOWN")
   export const OPENCODE_MODELS_URL = process.env["OPENCODE_MODELS_URL"]
   export const OPENCODE_MODELS_PATH = process.env["OPENCODE_MODELS_PATH"]
+  export const OPENCODE_DB = process.env["OPENCODE_DB"]
   export const OPENCODE_DISABLE_CHANNEL_DB = truthy("OPENCODE_DISABLE_CHANNEL_DB")
   export const OPENCODE_SKIP_MIGRATIONS = truthy("OPENCODE_SKIP_MIGRATIONS")
   export const OPENCODE_STRICT_CONFIG_DEPS = truthy("OPENCODE_STRICT_CONFIG_DEPS")

+ 4 - 0
packages/opencode/src/storage/db.ts

@@ -28,6 +28,10 @@ const log = Log.create({ service: "db" })
 
 export namespace Database {
   export const Path = iife(() => {
+    if (Flag.OPENCODE_DB) {
+      if (path.isAbsolute(Flag.OPENCODE_DB)) return Flag.OPENCODE_DB
+      return path.join(Global.Path.data, Flag.OPENCODE_DB)
+    }
     const channel = Installation.CHANNEL
     if (["latest", "beta"].includes(channel) || Flag.OPENCODE_DISABLE_CHANNEL_DB)
       return path.join(Global.Path.data, "opencode.db")

+ 10 - 5
packages/opencode/test/storage/db.test.ts

@@ -1,14 +1,19 @@
 import { describe, expect, test } from "bun:test"
 import path from "path"
+import { Global } from "../../src/global"
 import { Installation } from "../../src/installation"
 import { Database } from "../../src/storage/db"
 
 describe("Database.Path", () => {
   test("returns database path for the current channel", () => {
-    const file = path.basename(Database.Path)
-    const expected = ["latest", "beta"].includes(Installation.CHANNEL)
-      ? "opencode.db"
-      : `opencode-${Installation.CHANNEL.replace(/[^a-zA-Z0-9._-]/g, "-")}.db`
-    expect(file).toBe(expected)
+    const db = process.env["OPENCODE_DB"]
+    const expected = db
+      ? path.isAbsolute(db)
+        ? db
+        : path.join(Global.Path.data, db)
+      : ["latest", "beta"].includes(Installation.CHANNEL)
+        ? path.join(Global.Path.data, "opencode.db")
+        : path.join(Global.Path.data, `opencode-${Installation.CHANNEL.replace(/[^a-zA-Z0-9._-]/g, "-")}.db`)
+    expect(Database.Path).toBe(expected)
   })
 })