Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions packages/opencode/src/storage/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
type Journal = { sql: string; timestamp: number; name: string }[]

// Drizzle's migrate overloads trigger expensive variance checks here; narrow to the journal overload we actually use.
const migrateFromJournal = migrate as unknown as (db: SQLiteBunDatabase, entries: Journal) => void

Check warning on line 36 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type '(db: SQLiteBunDatabase<Record<string, never>, EmptyRelations>, entries: Journal) => void' is more narrow than the original type.

function applyMigrations(db: SQLiteBunDatabase, entries: Journal) {
migrateFromJournal(db, entries)
Expand All @@ -57,15 +57,15 @@
.filter((entry) => entry.isDirectory())
.map((entry) => entry.name)

const sql = dirs
.map((name) => {
const file = path.join(dir, name, "migration.sql")
if (!existsSync(file)) return
return {
sql: readFileSync(file, "utf-8"),
timestamp: time(name),
name,
}

Check warning on line 68 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(consistent-return)

Function expected no return value.
})
.filter(Boolean) as Journal

Check warning on line 70 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'Journal' is more narrow than the original type.

Expand All @@ -81,7 +81,7 @@

export const Client = Object.assign(
(): TxOrDb => {
if (loaded) return client as TxOrDb

Check warning on line 84 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'TxOrDb' is more narrow than the original type.

const dbPath = getPath()
log.info("opening database", { path: dbPath })
Expand All @@ -105,6 +105,45 @@
count: entries.length,
mode: typeof OPENCODE_MIGRATIONS !== "undefined" ? "bundled" : "dev",
})
// Drizzle beta.19 (used before the upstream sync to rc.2) tracked applied
// migrations in a "migration" table with column "id" = folder name.
// rc.2 switched to "__drizzle_migrations" with a "name" column. On an
// existing EFS database the old table is present but the new one is not,
// so rc.2 sees all migrations as unapplied and tries to re-run them —
// which then crashes because the schema columns already exist.
// Fix: seed "__drizzle_migrations" from the legacy "migration" table
// before handing off to applyMigrations so rc.2 skips already-done work.
const hasLegacyTable = (db as unknown as TxOrDb).$client

Check warning on line 116 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'TxOrDb' is more narrow than the original type.
.prepare("SELECT 1 FROM sqlite_master WHERE type='table' AND name='migration'")
.get()
if (hasLegacyTable) {
const oldRows = (db as unknown as TxOrDb).$client

Check warning on line 120 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'TxOrDb' is more narrow than the original type.
.prepare("SELECT id FROM migration")
.all() as Array<{ id: string }>

Check warning on line 122 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type '{ id: string; }[]' is more narrow than the original type.
const oldNames = new Set(oldRows.map((r) => r.id));
(db as unknown as TxOrDb).$client.exec(`

Check warning on line 124 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'TxOrDb' is more narrow than the original type.
CREATE TABLE IF NOT EXISTS "__drizzle_migrations" (
id INTEGER PRIMARY KEY,
hash text NOT NULL,
created_at numeric,
name text,
applied_at TEXT
)
`)
const insert = (db as unknown as TxOrDb).$client.prepare(

Check warning on line 133 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-unsafe-type-assertion)

Unsafe type assertion: type 'TxOrDb' is more narrow than the original type.
`INSERT OR IGNORE INTO "__drizzle_migrations" (hash, created_at, name, applied_at) VALUES (?, ?, ?, ?)`,
)
const now = Date.now()
let seeded = 0
for (const entry of entries) {
if (oldNames.has(entry.name)) {
insert.run("legacy", now, entry.name, new Date().toISOString())
seeded++
}
}
if (seeded > 0)
log.info("seeded legacy migration records into __drizzle_migrations", { count: seeded })
}
applyMigrations(db, entries)
}

Expand Down Expand Up @@ -139,7 +178,7 @@
if (err instanceof LocalContext.NotFound) {
const effects: (() => void | Promise<void>)[] = []
const result = ctx.provide({ effects, tx: Client() }, () => callback(Client()))
for (const effect of effects) effect()

Check warning on line 181 in packages/opencode/src/storage/db.ts

View workflow job for this annotation

GitHub Actions / Lint changed files (oxlint)

typescript-eslint(no-floating-promises)

Promises must be awaited, add void operator to ignore.
return result
}
throw err
Expand Down
Loading