-- Keep only the newest participant link when historical application bugs linked
-- one customer account to multiple participants in the same table session.
UPDATE sessionCustomers older
INNER JOIN sessionCustomers newer
        ON newer.customerUserId = older.customerUserId
       AND newer.sessionId = older.sessionId
       AND newer.vendorId = older.vendorId
       AND newer.id > older.id
SET older.customerUserId = NULL
WHERE older.customerUserId IS NOT NULL;

-- NULL customerUserId values remain unrestricted, allowing any number of
-- anonymous/device participants in a table session. A signed-in account may
-- own only one participant in that session.
ALTER TABLE sessionCustomers
  ADD UNIQUE KEY uk_sessionCustomers_customer_session_vendor
    (customerUserId, sessionId, vendorId);
