fix(core): import legacy auth.json when creating a fresh database (#62568)

This commit is contained in:
Dan Smolsky
2026-11-07 07:10:48 -05:00
committed by GitHub
Unverified
parent 75dbf5b8bc
commit 478de99765
3 changed files with 39 additions and 2 deletions
+2
View File
@@ -6,6 +6,7 @@ import type { SqlClient } from "effect/sql"
import { supportsForeignKeyToggle } from "#sqlite"
import { EffectDrizzleSqlite } from "./drizzle.js"
import { migrations } from "./migration.gen.js"
import legacyCredentials from "./migration/20260805200742_import_legacy_credentials.js"
import schema from "./schema.gen.js"
import { Global } from "@opencode/util/global"
@@ -35,6 +36,7 @@ export function apply(db: Database) {
yield* db.transaction((tx) =>
Effect.gen(function* () {
yield* schema.up(tx)
yield* legacyCredentials.up(tx)
yield* tx.run(
sql`CREATE TABLE ${sql.identifier("migration")} (id TEXT PRIMARY KEY, time_completed INTEGER NOT NULL)`,
)
@@ -49,7 +49,7 @@ export function importLegacyCredentials(tx: Parameters<DatabaseMigration.Migrati
if (content === undefined) return
const input = Option.getOrUndefined(decodeJson(content))
if (typeof input !== "object" || input === null || Array.isArray(input)) {
return yield* Effect.fail(new Error("Legacy credential file must contain an object"))
return yield* Effect.logWarning("Skipped legacy credential file that is not a JSON object", { path: filepath })
}
const origins: string[] = []
+36 -1
View File
@@ -460,12 +460,12 @@ describe("DatabaseMigration", () => {
"https://example.com/": { type: "wellknown", key: "TOKEN", token: "wellknown-key" },
invalid: { type: "unknown" },
})
await Bun.write(source, content)
await run(
Effect.gen(function* () {
const db = yield* makeDb
yield* DatabaseMigration.apply(db)
yield* Effect.promise(() => Bun.write(source, content))
const now = Date.now()
yield* db.run(sql`
INSERT INTO credential (id, integration_id, label, value, time_created, time_updated)
@@ -533,6 +533,41 @@ describe("DatabaseMigration", () => {
expect(await Bun.file(source).text()).toBe(content)
})
test("imports legacy credentials when bootstrapping a fresh database", async () => {
await using tmp = await tmpdir()
await Bun.write(
path.join(tmp.path, "auth.json"),
JSON.stringify({ openai: { type: "oauth", refresh: "refresh", access: "access", expires: 123 } }),
)
await run(
Effect.gen(function* () {
const db = yield* makeDb
yield* DatabaseMigration.apply(db)
expect(yield* db.all(sql`SELECT integration_id, label FROM credential`)).toEqual([
{ integration_id: "openai", label: "OAuth" },
])
}),
Global.make({ data: tmp.path }),
)
})
test("bootstraps a fresh database when the legacy credential file is malformed", async () => {
await using tmp = await tmpdir()
await Bun.write(path.join(tmp.path, "auth.json"), "not json")
await run(
Effect.gen(function* () {
const db = yield* makeDb
yield* DatabaseMigration.apply(db)
expect(yield* db.all(sql`SELECT id FROM credential`)).toEqual([])
}),
Global.make({ data: tmp.path }),
)
})
test("skips legacy credential import when the source file is absent", async () => {
await using tmp = await tmpdir()