wacrm/supabase/migrations
Arnas Donauskas faf0f740a4
fix: stop inbound chats fragmenting into duplicate conversations (#363)
An inbound message could create a second conversation for a contact
under a race (Meta retries a delivery, or a batch fans out to concurrent
after() runs). There was no UNIQUE index and no unique-violation
backstop on conversations, unlike contacts (migration 022). Worse, once
two conversations existed the `.single()` lookup errored on every later
message and the webhook created yet another conversation each time,
snowballing into a wall of duplicate chats.

- findOrCreateConversation now resolves oldest-first with .limit(1)
  instead of .single(), so pre-existing duplicates converge, and catches
  23505 on insert to re-resolve the winning row.
- Same hardening applied to the public-API resolver (resolve-conversation).
- Migration 036 merges existing duplicate conversations into the oldest
  thread (re-pointing messages, reactions, deals, flow_runs,
  notifications, ai_usage_log — no data loss) then adds a
  UNIQUE (account_id, contact_id) index as the authoritative guarantee.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-10 12:27:46 +03:00
..
001_initial_schema.sql fix: make migration idempotent 2026-04-16 19:46:41 +03:00
002_pipelines_enhancements.sql feat(pipelines): Kanban drag-drop, analytics, deal sheet, contact deals tab 2026-04-16 22:38:45 +03:00
003_broadcast_recipient_wamid.sql fix(broadcasts): per-recipient variables, webhook status sync, rate limit 2026-04-17 11:22:38 +03:00
004_contact_delete_set_null.sql fix(contacts): allow deletion — SET NULL on broadcast_recipients, deals 2026-04-17 16:56:45 +03:00
005_broadcast_counts_incremental.sql fix(broadcasts): senior-review hardening pass 2026-04-17 17:38:52 +03:00
006_automations.sql feat(automations): visual flow builder, engine, and logs viewer 2026-04-17 19:39:02 +03:00
007_automations_increment_counter.sql fix(automations): atomic execution_count via SQL function (migration 007) 2026-04-17 21:45:27 +03:00
008_profile_avatars_storage.sql feat(settings): user profile management 2026-04-20 17:40:39 +03:00
009_message_actions.sql feat(inbox): reactions, reply-to, and copy on chat messages 2026-05-19 16:17:26 +03:00
010_flows.sql feat(flows): foundation for interactive WhatsApp messages (PR 1/4) 2026-05-22 13:12:27 +03:00
011_profile_beta_features.sql feat(beta): per-account beta_features flag on profiles, gating Flows 2026-05-22 12:57:33 +03:00
012_flows_increment_counter.sql fix(flows): atomic execution_count increment via SECURITY DEFINER RPC 2026-05-22 15:39:00 +03:00
013_whatsapp_config_phone_number_id_unique.sql fix(whatsapp): prevent silent inbound-message drop when two users share a phone_number_id 2026-05-26 10:35:59 +02:00
014_message_templates_meta_integration.sql fix(migration-014): drop subquery from buttons CHECK (Postgres disallows it) 2026-05-29 12:21:14 +02:00
015_whatsapp_config_registration.sql fix(whatsapp): register phone numbers on Meta + verify subscription state 2026-05-29 18:12:38 +02:00
016_flow_media.sql feat(flows): add send_media node for image / video / document sends 2026-05-29 19:11:29 +02:00
017_account_sharing.sql fix(migrations): make 017 RLS rewrite re-runnable 2026-06-22 09:48:47 +03:00
018_account_member_rpcs.sql feat(api): account + members management endpoints 2026-05-31 15:00:27 +03:00
019_invitation_rpcs.sql feat(api): invitation create + peek + redeem endpoints 2026-05-31 17:32:46 +03:00
020_account_sharing_followups.sql perf(db,webhook): backend follow-ups from the multi-user review 2026-05-31 19:18:58 +03:00
021_account_default_currency.sql feat(deals): make default deal currency configurable per account (#218) 2026-06-08 13:59:48 +03:00
022_contact_phone_dedup.sql feat(contacts): prevent duplicate phone numbers on create/import (#212) 2026-06-08 15:52:38 +03:00
023_chat_media.sql feat(inbox): send photo, video, document & voice in chat (#213) 2026-06-18 10:07:16 +03:00
024_member_presence.sql feat(team): member presence (online / away / offline) on the roster 2026-06-22 14:37:29 +03:00
025_filter_contacts_by_tags.sql feat(contacts): add filter_contacts_by_tags RPC migration 2026-06-22 15:50:40 +03:00
026_api_keys.sql feat(api): public API groundwork — scoped API keys + /api/v1/me 2026-06-23 15:35:43 +03:00
027_notifications.sql feat(notifications): implement account-based notification retrieval and update logic 2026-06-30 11:16:20 +05:30
028_webhook_endpoints.sql feat(api): outbound event webhooks — /api/v1/webhooks (#245) 2026-07-01 14:53:18 +03:00
029_ai_reply.sql fix(ai): grant EXECUTE on claim_ai_reply_slot so auto-reply works (#345) 2026-07-07 10:43:03 +03:00
030_ai_knowledge.sql fix(ai-kb): address code-review findings 2026-07-02 17:20:38 +03:00
031_ai_reply_slot_grant.sql fix(ai): grant EXECUTE on claim_ai_reply_slot so auto-reply works (#345) 2026-07-07 10:43:03 +03:00
032_fix_ai_knowledge_membership.sql fix(security): patch DB-layer authz bypasses (GHSA-fg5p-2qc3-jmxr) 2026-07-08 09:10:07 +03:00
033_ai_reply_polish.sql feat(ai): polish auto-reply — inbox controls, real handoff, usage logging 2026-07-08 14:03:33 +03:00
034_fix_profiles_update_rls.sql fix(db): resolve duplicate migration version 031 (rename profiles-RLS fix to 034) 2026-07-08 16:04:39 +03:00
035_interactive_messages.sql feat: WhatsApp interactive messages (reply buttons + lists) 2026-07-08 19:03:15 +03:00
036_conversation_contact_dedup.sql fix: stop inbound chats fragmenting into duplicate conversations (#363) 2026-07-10 12:27:46 +03:00