fix(core): surface schema errors from a database migrated by a newer version#36429
Open
H3XDaemon wants to merge 1 commit into
Open
fix(core): surface schema errors from a database migrated by a newer version#36429H3XDaemon wants to merge 1 commit into
H3XDaemon wants to merge 1 commit into
Conversation
74a93bd to
711e02e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #35986
Type of change
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).
Two things make the failure this opaque, and this PR fixes both:
no such column: agent— exactly what a schema mismatch produces) escape as raw defects instead of typed SqlErrors. Moved prepare inside the try.Error.messageis 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