-- Revert billing_communications uniqueness back to (billing_account_no, month).

-- 1. Drop the per-customer unique key
ALTER TABLE billing_communications DROP INDEX uniq_customer_period;

-- 2. Re-adding the old (billing_account_no, month) key requires deduping;
--    the per-customer backfill from the up migration adds rows that share
--    the old key. Keep the lowest-id row in each (billing_account_no, month)
--    group and discard the rest.
DELETE bc FROM billing_communications bc
INNER JOIN (
  SELECT MIN(id) AS keep_id, billing_account_no, month
  FROM billing_communications
  GROUP BY billing_account_no, month
) keepers
  ON bc.billing_account_no = keepers.billing_account_no
 AND bc.month = keepers.month
 AND bc.id <> keepers.keep_id;

-- 3. Restore the original unique key
ALTER TABLE billing_communications
  ADD UNIQUE KEY uniq_account_month (billing_account_no, month);
