-- Migration: Fix user_operation_progress duplicates and add unique constraint
-- Created at: 2026-03-31

-- 1. Cleanup duplicates: Keep the row with the highest id for each user-operation pair
-- This ensures we keep the most recent/advanced progress if multiple exist.
DELETE t1 FROM user_operation_progress t1
INNER JOIN user_operation_progress t2 
ON t1.user_id = t2.user_id 
   AND t1.operation_id = t2.operation_id
   AND t1.id < t2.id;

-- 2. Add unique constraint to prevent future duplication
ALTER TABLE user_operation_progress 
ADD UNIQUE KEY `unique_user_operation` (`user_id`, `operation_id`);
