SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

UPDATE admin_user_withdrawal_applications
SET freeze_command_id = CONCAT('legacy:', id)
WHERE freeze_command_id = '';

SET @idx_exists = (
  SELECT COUNT(*)
  FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'admin_user_withdrawal_applications'
    AND INDEX_NAME = 'uk_admin_withdrawal_freeze_command'
);

SET @ddl = IF(
  @idx_exists = 0,
  'ALTER TABLE admin_user_withdrawal_applications ADD UNIQUE KEY uk_admin_withdrawal_freeze_command (app_code, freeze_command_id)',
  'SELECT 1'
);

PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
