-- Migration 002: Fix surge insights unique key -- Problem: The old unique key included peak_start_hour and peak_end_hour, -- which meant a new record was inserted every time the peak window shifted, -- instead of updating the existing one. -- Fix: The unique key now only covers (competitor_name, country_code), -- so ON DUPLICATE KEY UPDATE always updates the existing row correctly. -- Step 1: Drop the old unique key that included peak hours ALTER TABLE `competitor_surge_insights` DROP INDEX `unique_surge`; -- Step 2: Add the correct unique key — one row per competitor per country ALTER TABLE `competitor_surge_insights` ADD UNIQUE KEY `unique_surge` (`competitor_name`, `country_code`); -- Verify: show the new index -- SHOW INDEX FROM `competitor_surge_insights`;