Repository navigation
Give livechat_messages a foreign key to their conversation - #12
Merged
Merged
Conversation
The install migration never declared one, likely because t.references :conversation cannot infer livechat_conversations on its own. So nothing but the dependent: :destroy callback kept a message from outliving its conversation, and a host clearing conversations with delete_all, or from a console, left orphaned messages and their attachments behind. Fresh installs now get the key, and livechat:foreign_key adds it to existing ones. On PostgreSQL that migration adds the key unvalidated and validates it in a separate statement outside the transaction, so the scan of existing messages does not block chat writes and strong_migrations accepts it. The cost is that a bulk delete_all on conversations now raises rather than silently orphaning, which is the point; the engine's own purge already goes through destroy.
The key ships in 1.4.0 alongside the chat page, and only AGENTS.md mentioned the upgrade step. The README's upgrade notes now say it is optional, what it prevents, and the one way it fails.
yshmarov
force-pushed
the
conversation-foreign-key
branch
from
November 21, 2026 08:55
fc183bf to
f5e424e
Compare
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.
active_record_doctor flagged
livechat_messages.conversation_idas a reference with no foreign key. The install migration usest.references :conversationwith noforeign_key:, probably because the table name can't be inferred. Until now, onlydependent: :destroykept messages from outliving their conversation.foreign_key: { to_table: :livechat_conversations }.livechat:foreign_key. On PostgreSQL it adds the key withvalidate: false, then validates it outside the transaction. strong_migrations accepts that, and the validation scan doesn't block chat writes. The generator prints a query for orphaned messages, because those would make validation fail.purge_resolved, seeds) all go throughdestroy, so nothing changes for them. A host callingdelete_allon conversations now gets an error instead of silently orphaned messages.The generator's doc comment says "before 1.4", which assumes this ships as 1.4.0. The CHANGELOG entry is under
Unreleased, and the version is not bumped.Verified
rake testpassed 4 times in a row: 144 runs, 0 failures.rubocopis clean.convalidated = t.