Make m013_usage migration idempotent to recover interrupted migrations#1520
Open
AnayGarodia wants to merge 1 commit into
Open
Make m013_usage migration idempotent to recover interrupted migrations#1520AnayGarodia wants to merge 1 commit into
AnayGarodia wants to merge 1 commit into
Conversation
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.
Refs #1036
The bug
migrate()applies each migration and records it in_llm_migrationsas two separate auto-committed steps.m013_usageissues threeALTER TABLEstatements, each committed individually. If the process is interrupted after the firstALTERbut before the migration is recorded (Ctrl-C, terminal closed, crash elsewhere on a slow first run), the database is left with theinput_tokenscolumn present butm013_usageunrecorded.Every subsequent invocation then re-runs
m013_usageand dies with:which permanently wedges
llmuntil the user deleteslogs.db— exactly the workaround reported in #1036.The fix
Make
m013_usageidempotent by checkingcolumns_dictbefore eachadd_column, following the same patternm001_initialalready uses. Re-running after an interruption now completes the migration and records it.Test
test_migrate_recovers_from_interrupted_m013simulates the interrupted state (columns added, migration not recorded) by deleting them013_usagerow from_llm_migrationsafter a full migrate, then re-runningmigrate().Fails before the fix with
duplicate column name: input_tokens, passes after. The other migration tests pass unchanged (test_migrations_for_embeddingsandtest_backfill_content_hashfail on my machine both with and without this change — local SQLite version issue, appears related to the same family as #1511).Note: other multi-statement migrations (e.g.
m014_schemas) can wedge the same way. Happy to guard those too in a follow-up if you'd like this pattern applied across the board, or alternatively wrap apply+record in a single transaction inmigrate()itself.