diff --git a/packages/core/src/database/migration.ts b/packages/core/src/database/migration.ts index 8de838ff6d3..9f006d1eb56 100644 --- a/packages/core/src/database/migration.ts +++ b/packages/core/src/database/migration.ts @@ -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)`, ) diff --git a/packages/core/src/database/migration/20260805200742_import_legacy_credentials.ts b/packages/core/src/database/migration/20260805200742_import_legacy_credentials.ts index 0da25f13041..dced59eefc5 100644 --- a/packages/core/src/database/migration/20260805200742_import_legacy_credentials.ts +++ b/packages/core/src/database/migration/20260805200742_import_legacy_credentials.ts @@ -49,7 +49,7 @@ export function importLegacyCredentials(tx: Parameters { "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()