Skip to content

Commit

Permalink
Deduplicate passwords in database migration
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoffreyHuck committed Sep 7, 2023
1 parent fd2b561 commit 87b776f
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions db/migrations/1908210505_add_unique_index_sPassword_on_groups.sql
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 87b776f

Please sign in to comment.