Skip to content

fix(server): migration 050 no longer stalls startup on large databases - #234

Merged
badcuban merged 1 commit into
mainfrom
fix/migration-050-backfill-speed
Sep 6, 2026
Merged

fix(server): migration 050 no longer stalls startup on large databases#234
badcuban merged 1 commit into
mainfrom
fix/migration-050-backfill-speed

Conversation

@badcuban

@badcuban badcuban commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator

Updating to nightly .274 or .275 on a database with real history left the desktop app stuck at "Threadlines couldn't start". The backend printed "Running all migrations..." and never answered, the 60-second readiness timer killed it, and Try Again repeated the same work.

The cause is migration 050 from #221. Its backfills use a correlated subquery over a CTE, which SQLite executes as a co-routine that re-scans the whole orchestration_events table for every projected row. On a 3.2 GB database (about 487k events and 222k activity rows) the first backfill alone ran past four minutes. Because each migration runs in a transaction, the kill rolled everything back and the database stayed at migration 49, so no data was harmed.

The fix rewrites the three backfills as UPDATE ... FROM (subquery), so the first-event lookup is built once and joined. Same result, different plan. Running the real migrator against a copy of that same database now completes migrations 050 and 051 in about eight seconds. The existing migration test still passes, and the repo guidelines gain one line asking for row-rewriting migrations to be timed against real history before merge.

Follow-up, separate PR: the desktop should recognize a backend that is still migrating instead of killing it after 60 seconds, and the migrator should log each migration's duration.

Nightly .274/.275 (PR #221) added migration 050, which backfills an
event_sequence column with a correlated subquery over a CTE. SQLite plans
that as a co-routine that re-scans the entire orchestration_events table
for every projected row. On a 3.2 GB database (~487k events, ~222k
activities) the first of three backfills did not finish in four minutes,
the desktop's 60-second readiness timer killed the backend, the
transaction rolled back, and every "Try Again" repeated the same work.

Rewrite the three backfills as UPDATE ... FROM so the first-event lookup
is materialized once and joined. Same result; both new migrations run in
about eight seconds on the same database. Add a guideline line asking for
migrations that rewrite rows to be timed against real history.
@vercel

vercel Bot commented Sep 6, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
threadlines Ready Ready Preview Sep 6, 2026 4:21am UTC

Request Review

@github-actions github-actions Bot added vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. size:M labels Sep 6, 2026
@badcuban
badcuban added this pull request to the merge queue Sep 6, 2026
Merged via the queue into main with commit 7a7d50b Sep 6, 2026
16 checks passed
@badcuban
badcuban deleted the fix/migration-050-backfill-speed branch September 6, 2026 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant