Skip to content

fix(db): unique index parcial em inbox_messages.whatsapp_message_id - #64

Open
felipetruman wants to merge 1 commit into
thaleslaray:mainfrom
felipetruman:fix/unique-inbox-whatsapp-msg-id
Open

fix(db): unique index parcial em inbox_messages.whatsapp_message_id#64
felipetruman wants to merge 1 commit into
thaleslaray:mainfrom
felipetruman:fix/unique-inbox-whatsapp-msg-id

Conversation

@felipetruman

@felipetruman felipetruman commented Jun 16, 2026

Copy link
Copy Markdown

Contexto

Companion no nível do banco do PR #58 (guard app-level de dedup inbound). O índice do baseline em whatsapp_message_id era não-unique (só performance), então retries do Meta conseguiam inserir a mesma mensagem 2x.

Correção

  • Dropa idx_inbox_messages_whatsapp_msg_id (não-unique).
  • Cria ux_inbox_messages_whatsapp_msg_idUNIQUE INDEX parcial WHERE whatsapp_message_id IS NOT NULL.
  • Dedup garantido no DB mesmo se a guard app-level falhar (defesa em profundidade).

⚠️ Pré-requisito

A criação do UNIQUE falha se houver duplicatas existentes. Instalações novas têm a tabela vazia. Para bases em uso, deduplicar antes (snippet de DELETE com row_number() no header da migration).

Já aplicado na produção do fork (smartzap-v4) após deduplicar 1 par existente.

Test plan

  • Install novo → index criado, tabela vazia.
  • Inserir 2x o mesmo whatsapp_message_id → 2ª falha por unique violation.

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced WhatsApp message deduplication to prevent duplicate messages from being stored in conversations, improving data integrity and conversation reliability.

Companion no nível do banco da guard app-level (thaleslaray#58). O índice do baseline
era não-unique (só performance), permitindo duplicatas em retries do Meta.
Troca por UNIQUE INDEX parcial (WHERE whatsapp_message_id IS NOT NULL),
garantindo dedup no DB mesmo se a guard app-level falhar.

Inclui nota de deduplicação prévia para bases já em uso (UNIQUE falha se
houver duplicatas existentes).
@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

@felipetruman is attempting to deploy a commit to the Thales Laray Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 60c1f7ac-c4b9-43fc-befe-cecbdc84ee76

📥 Commits

Reviewing files that changed from the base of the PR and between 17c69e2 and ae00269.

📒 Files selected for processing (1)
  • supabase/migrations/20260616000001_unique_inbox_whatsapp_msg_id.sql

📝 Walkthrough

Walkthrough

A single Supabase SQL migration drops the existing non-unique index on public.inbox_messages.whatsapp_message_id and replaces it with a partial unique index (ux_inbox_messages_whatsapp_msg_id) that enforces uniqueness only for non-NULL values, preventing duplicate inbound WhatsApp messages at the database level.

Changes

WhatsApp Message Deduplication Index

Layer / File(s) Summary
Partial unique index migration
supabase/migrations/20260616000001_unique_inbox_whatsapp_msg_id.sql
Drops the previous plain index on whatsapp_message_id and creates ux_inbox_messages_whatsapp_msg_id, a partial UNIQUE INDEX WHERE whatsapp_message_id IS NOT NULL, blocking duplicate non-NULL values from being inserted into public.inbox_messages. The migration notes that existing duplicates must be resolved beforehand or the index creation will fail.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~5 minutes

Poem

🐇 Hop hop, no doubles today!
A unique index hops into the fray,
NULL values pass, duplicates must flee,
The inbox stays tidy as it should be.
One message per ID — that's the bunny way! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: adding a partial unique index to the inbox_messages table for WhatsApp message deduplication.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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