Skip to content

fix(core): surface schema errors from a database migrated by a newer version#36429

Open
H3XDaemon wants to merge 1 commit into
anomalyco:devfrom
H3XDaemon:fix/db-newer-schema-guard
Open

fix(core): surface schema errors from a database migrated by a newer version#36429
H3XDaemon wants to merge 1 commit into
anomalyco:devfrom
H3XDaemon:fix/db-newer-schema-guard

Conversation

@H3XDaemon

@H3XDaemon H3XDaemon commented Jul 11, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #35986

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When two installs of different versions share the default opencode.db (in my case a desktop build from June 5 and a bun-installed CLI on 1.17.18), the newer one migrates the schema and the older one keeps running its old SQL against it. Nothing fails at startup — it dies later inside prompt_async with the opaque defect below, which looks exactly like db corruption. I deleted my db over this before working out what happened; the reporter of #35986 went down the same road (VACUUM, pruning sessions, finally a fresh db).

{"code":"ERR_SQLITE_ERROR","errcode":1,"errstr":"SQL logic error"}

Two things make the failure this opaque, and this PR fixes both:

  1. In sqlite.bun.ts / sqlite.node.ts the prepare call sits outside the try block, so prepare-time errors (no such column: agent — exactly what a schema mismatch produces) escape as raw defects instead of typed SqlErrors. Moved prepare inside the try.
  2. Even the raw message never reaches the log: Error.message is non-enumerable, so JSON-serializing the defect keeps only {code, errcode, errstr}. Now, when a statement fails with SQLITE_ERROR and the migration table contains ids newer than this build knows, the SqlError message says so: database was migrated by a newer version of opencode (…); update this installation.

DatabaseMigration.apply() also logs a warning at startup when it sees migrations from a newer build. It deliberately does not refuse to start: most migrations are additive (this db has 38, and the breakage came from a single one dropping three columns), so a mixed-version setup usually still works and blocking it would trade one failure for another.

How did you verify your code works?

New test drives the real client (core's sqlite.bun layer): db at head + a future migration id + a query against a missing column now yields a typed SqlError (previously a defect) whose message names the future migration. Full database-migration.test.ts passes (18/18), bun typecheck clean. The original failure is reproducible on my machine by restoring a newer-schema db against the old desktop build; the error text this PR produces is exactly what that situation needed.

Screenshots / recordings

n/a

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@H3XDaemon H3XDaemon force-pushed the fix/db-newer-schema-guard branch from 74a93bd to 711e02e Compare July 12, 2026 04:34
@H3XDaemon H3XDaemon changed the title fix(core): refuse to start against a database migrated by a newer version fix(core): surface schema errors from a database migrated by a newer version Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GUI stuck on "thinking" after sending messages

1 participant