fix(broadcasts): qualify contact_id so create_broadcast_with_recipients can execute (#536) - #537
Closed
neerajgupta2407 wants to merge 1 commit into
Closed
neerajgupta2407 wants to merge 1 commit into
neerajgupta2407 wants to merge 1 commit into
Conversation
Contributor
Author
|
@ArnasDon Could you Please review this? |
Owner
|
Merged via #566 with your commit and authorship intact — thank you for the precise diagnosis and the one-word fix. The only change was the file number: #533 landed as migration 040 while this was open, so yours ships as |
pull Bot
pushed a commit
to soitun/wacrm
that referenced
this pull request
Sep 13, 2026
Migration 040 was taken by ArnasDon#533 (business-scoped user ids) while ArnasDon#537 was open, so the fix ships as 041. The SQL is unchanged from ArnasDon#537 — one qualified identifier, broadcast_recipients.contact_id, in the RETURNING clause of create_broadcast_with_recipients. verify-schema.sql now checks pg_get_functiondef for the qualified form, so a replay that silently keeps the 038 body fails the migrations job instead of going green on a function that cannot run. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Fixes #536.
POST /api/v1/broadcastshas never succeeded.create_broadcast_with_recipientsis declaredRETURNS TABLE(broadcast_id, recipient_id, contact_id), and in PL/pgSQL aRETURNS TABLEoutput column is also an in-scope variable. The recipient insert ends in a bareRETURNING id, contact_id, so that name resolves against both the target table's column and the function's own output variable, and Postgres aborts:The change
One qualified identifier:
Qualifying it names the column and nothing else. Signature, arguments and result columns are unchanged from
038, solib/whatsapp/broadcast-core.tsneeds no edit — this is a migration-only PR.The other identifiers in the body are already unambiguous:
broadcast_idappears only in an INSERT column list (never a variable reference), and the finalSELECTreads throughins.Why a new
040_file instead of editing038Applied migrations are recorded in
schema_migrations— that ledger is what made the duplicate-version collision in034_fix_profiles_update_rls.sqla hard failure (23505). So editing038in place would fix only fresh installs: every existing deployment already has038recorded and would keep the broken function forever.040is aCREATE OR REPLACEof the exact038signature, so it is idempotent and safe to re-run. This is the same shape as034repairing017's policy. Happy to switch to an in-place edit of038instead if you'd rather — it's your call on how you want existing installs handled.Verification
Both function bodies — the current
038one and the fixed one — created side by side in a clean Postgres 16, then called:Also confirmed against a live Supabase project inside a
BEGIN … ROLLBACK: the function creates the campaign and returns its recipient row, nothing persisted.Two things worth a follow-up (not in this PR)
A plpgsql body is only parsed at
CREATEtime — name resolution happens on first execution. Applying a migration proves the function exists, not that it can run, so a check that applies migrations without calling the RPCs they define will go green on a function that is dead on arrival. That is exactly what happened here. A smoke test that invokes each RPC once would have caught this the day037landed.The blast radius hid it.
broadcast-core.tsis the only caller, reached from the public API. The dashboard's own broadcast route (POST /api/whatsapp/broadcast) loopssendTemplateMessageand never writes a campaign row, so the UI looks healthy whilebroadcastsandbroadcast_recipientsstay empty — which is why this survived from037(#370) through038(#472) unnoticed.