Skip to content

Give livechat_messages a foreign key to their conversation - #12

Merged
yshmarov merged 3 commits into
mainfrom
conversation-foreign-key
Nov 21, 2026
Merged

yshmarov merged 3 commits into
mainfrom
conversation-foreign-key

Conversation

@yshmarov

Copy link
Copy Markdown
Owner

active_record_doctor flagged livechat_messages.conversation_id as a reference with no foreign key. The install migration uses t.references :conversation with no foreign_key:, probably because the table name can't be inferred. Until now, only dependent: :destroy kept messages from outliving their conversation.

  • Fresh installs: foreign_key: { to_table: :livechat_conversations }.
  • Existing installs: new upgrade generator livechat:foreign_key. On PostgreSQL it adds the key with validate: 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.
  • Behavior: the engine's own deletes (purge_resolved, seeds) all go through destroy, so nothing changes for them. A host calling delete_all on conversations now gets an error instead of silently orphaned messages.
  • AGENTS.md lists the generator's two failure modes.

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 test passed 4 times in a row: 144 runs, 0 failures. rubocop is clean.
  • The new model test fails without the foreign key and passes with it.
  • Tested against real PostgreSQL in paidcollabs with strong_migrations enabled and existing messages in the table: migrate, roll back, migrate. The key ends up convalidated = t.

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
yshmarov force-pushed the conversation-foreign-key branch from fc183bf to f5e424e Compare November 21, 2026 08:55
@yshmarov
yshmarov merged commit 1312985 into main Nov 21, 2026
22 checks passed
@yshmarov
yshmarov deleted the conversation-foreign-key branch November 21, 2026 08:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant