← back to Ventura Claw Leads
db/migrations/005_fix_delivered_at_default.sql
17 lines
-- Bug fix: business_interest.delivered_at had DEFAULT now() in 001_init.sql,
-- so every newly-captured lead had delivered_at populated at capture time
-- with the same value as created_at — even though the lead-delivery cron
-- hadn't run yet. The cron's gate is `delivery_msg_id IS NULL` (which is
-- correct), but every other consumer of the field (CSV export, admin
-- inbox UI, billing reconciliation) saw misleading "Delivered" timestamps
-- on undelivered rows.
--
-- Fix: drop the default + backfill NULL on rows that have no delivery_msg_id.
ALTER TABLE business_interest ALTER COLUMN delivered_at DROP DEFAULT;
UPDATE business_interest
SET delivered_at = NULL
WHERE delivery_msg_id IS NULL
AND delivered_at IS NOT NULL;