fix: change summary_messages/lineage/context_items FK from RESTRICT to CASCADE/SET NULL#254
Open
Eugene9D wants to merge 1 commit intoMartian-Engineering:mainfrom
Open
Conversation
…o CASCADE/SET NULL Summary_messages used ON DELETE RESTRICT for message_id FK, but SQLite defaults to PRAGMA foreign_keys=0, so deletes silently succeed and leave orphaned rows in summary_messages pointing at non-existent message_ids. Changes: - summary_messages.message_id: RESTRICT → CASCADE (when a message is deleted, its link rows auto-delete) - summary_lineage.parent_summary_id: RESTRICT → CASCADE (when a parent summary is deleted, lineage rows auto-delete) - context_items.message_id/summary_id: RESTRICT → SET NULL (preserve the context row but null out the dead reference) - Add migrateForeignKeyCascade() for existing databases (idempotent table rebuild, cleans orphans during migration)
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.
Problem
summary_messages.message_idusesON DELETE RESTRICT, but SQLite defaults toPRAGMA foreign_keys=0. This means message deletes silently succeed and leave orphaned rows insummary_messagespointing at non-existentmessage_ids.Same issue affects
summary_lineage.parent_summary_idandcontext_items.message_id/summary_id.On a production database (~607MB, 58K messages, 1098 summaries), orphaned
summary_messagesrows accumulated silently over weeks.Fix
summary_messagesmessage_idsummary_lineageparent_summary_idcontext_itemsmessage_idcontext_itemssummary_idMigration for existing databases
Adds
migrateForeignKeyCascade()— idempotent table rebuild that:PRAGMA foreign_key_listINSERT INTO ... SELECT(cleans existing orphans)Testing
Applied locally on a 607MB production LCM database:
summary_messagesrows accumulatingPRAGMA foreign_key_list