From 87b776f349df8d772734f7fab3435928ccefc23c Mon Sep 17 00:00:00 2001 From: Geoffrey Huck Date: Wed, 16 Aug 2023 11:03:59 +0200 Subject: [PATCH] Deduplicate passwords in database migration --- ...505_add_unique_index_sPassword_on_groups.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/db/migrations/1908210505_add_unique_index_sPassword_on_groups.sql b/db/migrations/1908210505_add_unique_index_sPassword_on_groups.sql index fde97c450..22476d445 100644 --- a/db/migrations/1908210505_add_unique_index_sPassword_on_groups.sql +++ b/db/migrations/1908210505_add_unique_index_sPassword_on_groups.sql @@ -1,5 +1,22 @@ -- +migrate Up UPDATE `groups` SET `sPassword` = NULL WHERE `sPassword` = ''; + +# Deduplicate groups.sPassword: appends a suffix to make all sPassword unique. +UPDATE `groups` + JOIN ( + SELECT `ID`, ROW_NUMBER() OVER (PARTITION BY `groups`.`sPassword` ORDER BY `ID`) AS `order` + FROM `groups` + JOIN ( + SELECT `ID`, `sPassword` FROM `groups` AS `groups1` + WHERE EXISTS( + SELECT `ID` from `groups` AS `groups2` + WHERE `groups1`.`sPassword`=`groups2`.`sPassword` AND `groups1`.`ID` > `groups2`.`ID` + ) + GROUP BY `ID` + ) AS `duplicates` USING (`ID`) + ) AS `orders` USING (`ID`) +SET `groups`.`sPassword` = CONCAT(`groups`.`sPassword`, '@dup', `orders`.`order`); + ALTER TABLE `groups` ADD UNIQUE INDEX `sPassword` (`sPassword`); -- +migrate Down