Skip to content

fix(opencode): DB migrating on every run for non-latest channels - #21056

Closed
bew wants to merge 2 commits into
anomalyco:devfrom
bew:fix-repeated-migration
Closed

fix(opencode): DB migrating on every run for non-latest channels#21056
bew wants to merge 2 commits into
anomalyco:devfrom
bew:fix-repeated-migration

Conversation

@bew

@bewbew commented Apr 4, 2026

Copy link
Copy Markdown

Issue for this PR

Closes#21057
Closes#16885
Replaces #16884

Type of change

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

What does this PR do?

This PR fixes DB migration that was running on every start of opencode when installed from nixpkgs.
The nix package in nixpkgs sets OPENCODE_CHANNEL = "stable", which makes the DB have the name opencode-stable.db instead of opencode.db that the marker implied.

👉 Using the actual database path which already takes the OPENCODE_CHANNEL into account fixes the problem.

(change done manually, zero AI involved)

How did you verify your code works?

Yes, locally.

Screenshots / recordings

image The first run is from `main`, the second is with this PR.

Checklist

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

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@github-actionsgithub-actionsBot added the needs:compliance This means the issue will auto-close after 2 hours. label Apr 4, 2026
@github-actionsgithub-actionsBot removed needs:compliance This means the issue will auto-close after 2 hours. needs:issue labels Apr 4, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@bew
bewforce-pushed the fix-repeated-migration branch from 5defc3f to 400fd62CompareApril 5, 2026 00:02
@bewbew changed the title fix: DB migrating on every run when OPENCODE_CHANNEL is e.g. 'stable'fix: DB migrating on every run for non-latest channelsApr 5, 2026
@bew

bew commented Apr 5, 2026

Copy link
Copy Markdown
Author

The failing test / unit (linux) (pull_request) pipeline seems to be for another reason 🤔

1 tests failed:
(fail) memory: abort controller leak > webfetch does not leak memory over many invocations [487.59ms]

@jaredmontoya

jaredmontoya commented Apr 13, 2026

Copy link
Copy Markdown

@bew what if the test is flaky? Can you rebase and force push so that CI runs again?

@bew
bewforce-pushed the fix-repeated-migration branch from 400fd62 to 704fca0CompareApril 14, 2026 16:02
@bew

bew commented Apr 14, 2026

Copy link
Copy Markdown
Author

@bew what if the test is flaky? Can you rebase and force push so that CI runs again?

I rebased on dev, let me know if there is anything else I can do!

@blake-hamm

Copy link
Copy Markdown

This branch is working for me on nixos with. TY!

@jaredmontoya

Copy link
Copy Markdown

@thdxr can this PR get a review and hopefully get merged please?
It seems like tests fail for reasons unrelated to the changes in this PR.

@jhol

jhol commented Apr 24, 2026

Copy link
Copy Markdown

I ported this patch to 1.14.19 - the version I have via nixpkgs. It fixes the issue for me.

@bew
bewforce-pushed the fix-repeated-migration branch 2 times, most recently from a36c166 to 62e338eCompareApril 24, 2026 19:30
@bew

bew commented Apr 24, 2026

Copy link
Copy Markdown
Author

I rebased on latest dev branch, which changed a few import paths.
Also added a proper commit description for this fix.

@bew
bewforce-pushed the fix-repeated-migration branch from 62e338e to cd39d93CompareMay 15, 2026 01:29
@bew

bew commented May 15, 2026

Copy link
Copy Markdown
Author

Hello, I rebased on latest dev branch, any way to actually get this merged?

@jaredmontoya

Copy link
Copy Markdown

@simonklee@thdxr@adamdotdevin@Hona@Brendonovich@nexxeln anyone?
Don't let this simple PR rot here even longer than 40 days for no reason please.

@bew
bewforce-pushed the fix-repeated-migration branch from cd39d93 to ef217e9CompareMay 19, 2026 16:36
@pwiens

Copy link
Copy Markdown

Heads up: this will silently break after rebase on fc34c7456 (#27615, May 15). That refactor renamed Database.Path (const) → Database.getPath() (function) and updated the four call sites in packages/opencode/src/cli/cmd/db.ts, but the marker in this PR isn't on main yet so it wasn't included.

Post-rebase, marker is undefined. fs.existsSync(undefined) returns false in both Bun 1.3.13 and Node 24, so !exists(undefined) is always true and the migration runs on every launch. The Bun build is transpile-only, so the missing namespace property doesn't surface at build time.

One-liner:

- if (!(await Filesystem.exists(Database.Path))) {+ if (!(await Filesystem.exists(Database.getPath()))) {

Cost is more than a noisy log line: JsonMigration.run re-scans the full JSON storage tree (6662 todos plus sessions/messages/parts in my case) on every launch. Not destructive thanks to onConflictDoNothing, but several seconds of disk IO at startup.

I hit this in a downstream fork that pulled in the same fix you have here, then rebased past fc34c7456.

pwiens added a commit to pwiens/opencode that referenced this pull request May 20, 2026
The marker check in index.ts was hardcoded to Global.Path.data/opencode.db,
but the actual DB path is channel-aware (db.ts:getPath). On non-default
channels (local dev builds, named user channels), the real file is
opencode-<channel>.db, so the marker never exists and the 'one time'
migration runs on every launch.
Use Database.getPath() as the marker so it matches the channel-specific
filename that Database.Client() actually creates.
Note: upstream PR anomalyco#27615 (fc34c74) renamed Database.Path (const) to
Database.getPath() (function). The earlier version of this patch used
Database.Path and silently re-broke when rebased past the rename. Flagged
upstream at anomalyco#21056.
@bew
bewforce-pushed the fix-repeated-migration branch from ef217e9 to 1f95617CompareMay 20, 2026 17:07
@bew

bew commented May 20, 2026

Copy link
Copy Markdown
Author

Thanks @pwiens, I updated the PR

@bewbew changed the title fix: DB migrating on every run for non-latest channelsfix(opencode): DB migrating on every run for non-latest channelsMay 23, 2026
@mmonfox

Copy link
Copy Markdown

I have the same issue, agent recommended `ln -s opencode-stable.db '~/.local/share/opencode/opencode.db' on my system and referenced this PR.

@bew

bew commented Jun 13, 2026

Copy link
Copy Markdown
Author

Hello everyone!
In #30461 the json migration system was removed (first released in v1.16.0).
👉 So this PR is not needed anymore!

@bewbew closed this Jun 13, 2026
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

6 participants

@bew@jaredmontoya@blake-hamm@jhol@pwiens@mmonfox