mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-03 07:34:35 +01:00
26 lines
864 B
MySQL
26 lines
864 B
MySQL
|
/*
|
||
|
Rename the duplicate names before adding "UNIQUE" constraint
|
||
|
*/
|
||
|
DO $$
|
||
|
BEGIN
|
||
|
WHILE EXISTS (SELECT count(*) FROM user_group GROUP BY group_name HAVING count(*) > 1) LOOP
|
||
|
UPDATE user_group AS r
|
||
|
SET group_name = (
|
||
|
/*
|
||
|
truncate the name if it is too long after appending the sequence number
|
||
|
*/
|
||
|
CASE WHEN (length(group_name)+length(v.seq::text)+1) > 256
|
||
|
THEN
|
||
|
substring(group_name from 1 for (255-length(v.seq::text))) || '_' || v.seq
|
||
|
ELSE
|
||
|
group_name || '_' || v.seq
|
||
|
END
|
||
|
)
|
||
|
FROM (SELECT id, row_number() OVER (PARTITION BY group_name ORDER BY id) AS seq FROM user_group) AS v
|
||
|
WHERE r.id = v.id AND v.seq > 1;
|
||
|
END LOOP;
|
||
|
END $$;
|
||
|
|
||
|
ALTER TABLE user_group ADD CONSTRAINT unique_group_name UNIQUE (group_name);
|
||
|
|