-- ========================================
-- FIX DUPLICATE TEAM SESSIONS
-- Ensure session codes are truly unique with database constraint
-- ========================================

-- First, check for any duplicates
SELECT session_code, COUNT(*) as count 
FROM team_sessions 
GROUP BY session_code 
HAVING count > 1;

-- If you have duplicates, you'll need to delete them first
-- (Keep the one with the lower ID, delete the higher ID)
-- DELETE FROM team_sessions WHERE id IN (select_the_duplicate_ids_here);

-- Add a unique constraint on session_code if not already there
-- This will prevent duplicates at database level
ALTER TABLE `team_sessions` 
DROP INDEX IF EXISTS `idx_session_code`;

ALTER TABLE `team_sessions` 
ADD UNIQUE KEY `unique_session_code` (`session_code`);

-- Verify the constraint was added
SHOW INDEX FROM team_sessions WHERE Key_name = 'unique_session_code';

