iachat/app
Gabriel Jablonski 079d4b4996
fix(conversations): enforce NOT NULL on contact_id + cleanup orphans (#273)
* fix(conversations): enforce NOT NULL + FK on contact_id

Conversations had contact_id nullable with no FK to contacts. Combined
with dependent: :destroy_async on Contact#conversations, deleting a
contact could leave conversations pointing to a missing contact,
breaking the conversations#index API with
"undefined method 'additional_attributes' for nil" from the contact
partial.

Changes:
- Migration cleans up existing orphans, sets contact_id NOT NULL and
  adds a FK with ON DELETE CASCADE so the invariant is enforced at the
  DB level (complements the existing Rails presence validation).
- ContactMergeAction uses update_all for conversations/messages/notes/
  contact_inboxes so a failing callback cannot silently leave records
  pointing to the mergee contact before it is destroyed.
- Drop the now-redundant orphan filter in Conversations::ResolutionJob
  and its spec; the invariant is enforced at the schema level.

* fix: address review feedback

- Drop the ON DELETE CASCADE FK on conversations.contact_id. Several
  conversation-owned tables (messages, mentions, conversation_participants,
  reporting_events, csat_survey_responses, calls, applied_slas, sla_events,
  and polymorphic notifications) still have plain conversation_id references
  without FK cascades. The DB-level cascade would skip Conversation's
  dependent: cleanup and replace the NULL-contact bug with orphan children,
  and would also conflict with the existing non-cascade FKs on
  scheduled_messages/recurring_scheduled_messages. Keep the invariant at the
  Rails layer (NOT NULL + presence validation + dependent: :destroy_async).
- Clean up orphan conversations in the migration via Rails destroy so
  dependent associations are propagated correctly, instead of a raw
  DELETE FROM conversations that would orphan all child rows.
- Revert ContactMergeAction.merge_* methods back to per-record update! so
  Conversation#after_update_commit still fires (notify_status_change /
  CONVERSATION_CONTACT_CHANGED) for contact_id changes. The bang form
  still removes the silent-failure risk of the original .update call.
2026-04-22 13:57:40 -03:00
..
actions fix(conversations): enforce NOT NULL on contact_id + cleanup orphans (#273) 2026-04-22 13:57:40 -03:00
assets feat: Hide installation identifier (#11722) 2025-06-17 15:45:40 -07:00
builders Merge branch 'main' into chore/merge-4.13.0 2026-04-17 16:23:47 -03:00
channels fix: Move contact events to account stream rather than individual user stream (#11082) 2025-03-13 17:46:48 -07:00
controllers fix(internal-chat): use internal_chat_channel_id in delete payloads (#270) 2026-04-19 14:06:02 -03:00
dashboards fix(super-admin): prefill confirmed_at in new user form (#13662) 2026-03-10 12:14:58 +05:30
dispatchers feat(internal-chat): implement internal chat system for agents (#247) 2026-04-11 13:50:15 -03:00
drops feat: Implement existing template linking for CSAT surveys (#218) 2026-02-18 18:00:29 -03:00
fields fix(super-admin): prefill confirmed_at in new user form (#13662) 2026-03-10 12:14:58 +05:30
finders Merge branch 'chatwoot/develop' into chore/merge-upstream-4.12.0 2026-03-20 00:27:45 -03:00
helpers release v4.12.0-fazer-ai.47 (#259) 2026-04-08 20:52:26 -03:00
javascript fix(guides): point FAZER_AI_GUIDES_URL to /#/guides (#271) 2026-04-21 11:05:17 -03:00
jobs fix(conversations): enforce NOT NULL on contact_id + cleanup orphans (#273) 2026-04-22 13:57:40 -03:00
listeners fix(internal-chat): use internal_chat_channel_id in delete payloads (#270) 2026-04-19 14:06:02 -03:00
mailboxes Chore/merge upstream 4.8.0 (#150) 2025-11-19 16:25:58 -03:00
mailers Merge branch 'main' into chore/merge-4.13.0 2026-04-17 16:23:47 -03:00
models fix(conversations): enforce NOT NULL on contact_id + cleanup orphans (#273) 2026-04-22 13:57:40 -03:00
policies feat(whatsapp): convert inbox between WhatsApp providers (#268) 2026-04-18 20:57:27 -03:00
presenters Merge branch 'main' into chore/merge-4.13.0 2026-04-17 16:23:47 -03:00
services Merge branch 'main' into chore/merge-4.13.0 2026-04-17 16:23:47 -03:00
views Merge branch 'main' into chore/merge-4.13.0 2026-04-17 16:23:47 -03:00