fix(desktop): apply local-db migrations by identity, not by timestamp watermark
Problem
On one machine, every workspace list fails: workspaces.getAllGrouped selects
from the local SQLite worktrees table and sqlite answers
no such column: "created_by_superset". That query backs the entire sidebar, so
the app's primary navigation is empty. It has been failing since 2026-08-16
across three upgrades the user took (1.22.0 → 1.24.2 → 1.26.0) and never heals.
The column comes from migration 0037_add_created_by_superset_to_worktrees,
which shipped 2026-03-21. The app kept starting anyway.
Root cause
Not a failed migration — a skipped one.
The Sentry evidence narrows this precisely: that machine reports only
created_by_superset. getSettings does select().from(settings) and works
fine there on 1.26.0, so every migration from 0038 through 0055 applied
normally. Exactly one migration in the middle of the sequence was missed. (The
persist_terminal and active_organization_id issues are unrelated machines on
older releases — the known #6450 downgrade incident.)
That is the signature of drizzle's migrator gate. SQLiteSyncDialect.migrate
reads a single row:
SELECT id, hash, created_at FROM __drizzle_migrations ORDER BY created_at DESC LIMIT 1
...
if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) { apply }
That is a watermark, not a set, and it only moves forward. Any migration
arriving with a journal when below the watermark is skipped silently, and
stays skipped through every later upgrade — there is no error, and no retry
that can ever pick it up.
A machine takes on a too-high watermark because when records when
drizzle-kit generated a file, not the order files merged. 0037 was
generated 2026-03-18 and merged 2026-03-21. Any build carrying a migration
generated inside that window but shipped first — a branch build, or a canary,
which scripts/release-canary.sh [commit] builds from an arbitrary commit —
pushes the watermark past 0037 and masks it permanently. Migration order on
main is monotonic today, so main-only installs are not exposed; builds off
main are.
Two mechanisms the issue also proposed are ruled out:
- Partial DDL with the journal advanced cannot happen. Drizzle already runs
the whole pending batch inside oneBEGIN/COMMITand rolls back on failure,
so schema changes are all-or-nothing per launch. Atomicity is not the defect. - A migration failing on that machine would have stalled everything from
0037 onward, andgetSettingswould fail too. It does not.
Fix
Select pending migrations from the set of recorded created_at values
rather than from max(created_at). Affected databases heal on the next launch,
because the skipped migration's when is simply absent from the set.
created_at is the journal when, and drizzle has always recorded it, so
this reads tables written by every previous version and writes rows those
versions still understand — a downgrade after this change still behaves.
It keys on created_at rather than hash deliberately: migrations 0004–0008
and 0011 were edited after they had already shipped (#822, #824, #826,
and the Better Auth revert), so the hashes those machines recorded no longer
match the files on disk and hashing would re-run migrations that are already
applied. 0011's journal when was itself rewritten twice during that churn,
and it is the only entry ever rewritten — it is a no-op (SELECT 1;) today, so
re-running it is harmless. All 56 when values are distinct; a test ratchets
that, since two entries sharing one would let a real migration be mistaken for
an applied one.
Everything else is unchanged: same transaction and rollback semantics, same
__drizzle_migrations table shape, same statement execution path. The existing
catch around the call is untouched — see the note below.
No typed cause is added: nothing reads one. The failing query keeps throwing
the plain TRPCError it throws today.
Deliberately not in scope
apps/desktop/src/main/lib/local-db/index.ts:100 swallows migration failures —
the app boots and serves queries against whatever schema it has, and the real
error only reaches console.error. That is why this issue took 14 symptom
events across three releases to diagnose with zero events naming a cause. It did
not cause this bug (migrations did not fail here, one was skipped), and changing
it means deciding whether a failed migration should refuse to start the app —
a product call. Flagged for the maintainer rather than made here.
Verification
bunx biome check on the four changed files:
Checked 4 files in 53ms. No fixes applied.
cd apps/desktop && bun run typecheck:
$ tsc --noEmit
(clean)
bun test src/main/ src/no-main-process-blocking.test.ts:
714 pass
0 fail
1461 expect() calls
Ran 714 tests across 53 files. [2.70s]
Full apps/desktop suite, with the change:
3634 pass
1 fail
2 snapshots, 59290 expect() calls
Ran 3635 tests across 394 files. [27.04s]
The one failure is pre-existing, not from this change. Same suite with the
change reverted:
3628 pass
1 fail
2 snapshots, 59282 expect() calls
Ran 3629 tests across 393 files. [33.39s]
It is useSidebarDnd.test.ts hitting React.createContext is not a function
inside @dnd-kit/core — cross-file module-registry pollution. It passes in
isolation (8 pass, 0 fail) and has no reference to local-db.
End-to-end, against the real 56-migration set
bun:sqlite cannot register the two custom sqlite functions 0032 needs, so
that one file's uuid_v4() / uuid_is_valid_v4() calls were stubbed
identically in both arms; nothing else was touched.
A) drizzle migrate() tables: 42
B) runMigrations() tables: 42
A === B (full schema identical on the real 56-migration set): true
C) after upgrade via drizzle, worktrees has created_by_superset: false <- reproduces DESKTOP-YF
C) settings column count: 49 (later migrations DID apply, matching the Sentry evidence)
C) after runMigrations, created_by_superset: true <- repaired
C) schema now identical to a clean install: true
Arm C builds the exact reported state — a build shipped without 0037 sets the
watermark past it, the upgrade skips it silently, worktrees loses the column
while settings keeps all 49 — then repairs it to a clean-install schema.
The committed test covers the same ground on synthetic folders, including an arm
that pins drizzle's skip so a revert to migrate() fails loudly, an arm proving
a post-ship edit does not re-run a migration, and an arm proving a failing batch
still rolls back whole.
Refs DESKTOP-YF
https://claude.ai/code/session_01PyGdhAtqsxEKHPTFkMvwVJ
Summary by cubic
Fixes the desktop local-db migration runner so it applies migrations by identity (created_at) instead of Drizzle's timestamp watermark, and heals databases that already skipped one.
Bug Fixes
- Drizzle's watermark only moves forward, so a migration with a
whenbelow the highest recordedcreated_atis skipped silently forever. whenrecords when drizzle-kit generated the file, not when it merged, so branch/canary builds can ship a later migration first and mask an earlier one.- The new runner keys on
created_atrather thanhashbecause migrations 0004–0008 and 0011 were edited after shipping, so recorded hashes no longer match. - Transaction, rollback, and the
__drizzle_migrationstable shape are unchanged; downgrades still work. - The existing catch that swallows migration failures is untouched; whether to refuse startup on failure is left to the maintainer.
Verification
- Tests reproduce the skip, prove healing, confirm post-ship edits don't re-run, and verify a failing batch rolls back whole.
- The real 56-migration journal is checked to confirm every
whenis distinct.
Summary by CodeRabbit
-
Bug Fixes
- Improved local database updates so previously skipped migrations can be applied correctly.
- Prevented already-applied migrations from running again, including when migration files change.
- Ensured database updates roll back safely if any migration fails.
-
Reliability
- Added validation to help maintain consistent migration ordering and prevent incomplete database upgrades.