Files
warmbly/internal/infrastructure/db/migrations/000163_campaign_lead_source.up.sql
T
Matthew Meszaros 50711a8e66 fix: a campaign's linked segments are its audience, so detaching one withdraws its leads (#510) (#523)
* fix: make a campaign's linked segments the audience rather than an accumulator, so detaching one withdraws the leads it enrolled instead of leaving the old list mixed in with the new, tracked by a new campaign_leads.source that keeps a hand-picked lead, an overlapping segment's member and anyone the campaign has already emailed out of the withdrawal, and reported back as withdrawn/contacted counts the dialog confirms and explains (issue #510)

* fix: serialize the linked-segment sweep against a link replacement by taking the same campaign lock, so a sweep that read the old set cannot re-enrol the audience the replacement just withdrew, and word the dialog's confirm and toast so the campaign, not the segment, is what has already emailed a lead

* fix: stop the one-shot segment enrol from re-stamping leads that are already in the campaign, since the Leads tab's Add back runs through it and pinning a whole linked audience as hand-picked because one held-out member was restored is the accumulation this change exists to end

* fix: lock the leads a detachment is about to withdraw before deciding, because a send is reserved by stamping campaign_contact_progress and only then locking the lead row, so a reservation committing mid-pass was invisible to the delete's snapshot and could withdraw a lead whose first email had already gone; and report the already-emailed count on every toast branch, since a detach where the whole audience had been emailed changed no count and said nothing after confirming a removal

* fix: add the campaign_leads.source check constraint NOT VALID, which still enforces every insert and update while skipping a full scan of the largest table in the product under ACCESS EXCLUSIVE to learn that a one-statement-old column holds its own default everywhere
2026-09-14 22:12:15 -07:00

27 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Where a lead came from, so detaching a linked segment can take back the
-- audience it brought without touching a lead somebody chose by hand.
--
-- Before this, linking segment A, changing your mind and linking segment B
-- left the campaign holding A ∪ B: the links are a live audience but detaching
-- one only ever stopped future enrolment, so the wrong list stayed and got
-- emailed (issue #510).
--
-- 'manual' is the default, which is also the grandfathering: a lead that
-- predates this column was written by a path we cannot re-derive, and the safe
-- reading of an unknown provenance is that a person meant it. Only the
-- automatic enrolment of a linked segment writes 'segment', and only that is
-- ever withdrawn automatically.
ALTER TABLE campaign_leads
ADD COLUMN source text NOT NULL DEFAULT 'manual';
-- NOT VALID, and deliberately never validated: a NOT VALID check is still
-- enforced on every insert and update, which is the whole job of a
-- discriminator constraint, and the column is one statement old, so every
-- existing row already holds the default it allows. Validating would scan the
-- largest table in the product under ACCESS EXCLUSIVE to learn that. Splitting
-- the validation into the same file would not help either: golang-migrate runs
-- one file in one transaction, so the lock is held across both statements.
ALTER TABLE campaign_leads
ADD CONSTRAINT campaign_leads_source_check
CHECK (source IN ('manual', 'segment')) NOT VALID;