-- 1. Очищаем таблицу от автоматически созданных записей.
TRUNCATE TABLE `xf_username_change`;
-- 2. Воссоздаём новую историю изменения имён на основе данных из таблицы дополнения.
INSERT INTO `xf_username_change`
(`user_id`, `old_username`, `new_username`, `change_user_id`, `change_date`, `visible`)
SELECT
`xf_cmtv_uc_username_change`.`user_id`,
`xf_cmtv_uc_username_change`.`old_username`,
IFNULL(`xf_cmtv_uc_username_change_NEXT_CHANGE`.`old_username`, `xf_user`.`username`) AS `new_username`,
`xf_cmtv_uc_username_change`.`user_id` AS `change_user_id`,
`xf_cmtv_uc_username_change`.`change_date`,
IF(`xf_cmtv_uc_username_change`.`from_acp` = 0, 1, 0) AS `visible`
FROM
`xf_cmtv_uc_username_change`
LEFT JOIN
`xf_cmtv_uc_username_change` `xf_cmtv_uc_username_change_NEXT_CHANGE` ON `xf_cmtv_uc_username_change`.`user_id` = `xf_cmtv_uc_username_change_NEXT_CHANGE`.`user_id` AND `xf_cmtv_uc_username_change`.`change_id` < `xf_cmtv_uc_username_change_NEXT_CHANGE`.`change_id`
INNER JOIN
`xf_user` ON `xf_cmtv_uc_username_change`.`user_id` = `xf_user`.`user_id`
GROUP BY `xf_cmtv_uc_username_change`.`change_id`