Fix the problem of duplicate users
What does this MR do and why?
Resolve #414557 (closed)
Related Jihu issue: https://jihulab.com/gitlab-cn/gitlab/-/issues/2885
What
On the members list page, there are occasional duplicate users. The current MR is used to solve this problem.
I created a test group to demonstrate the problem: https://gitlab.com/groups/test-repeat-user/subgroup/-/group_members
Another page with the same problem is: https://gitlab.com/groups/test-repeat-user/subgroup/-/boards
Why
When obtaining the member list, a scope named distinct_on_user_with_max_access_level
will be used to deduplicate the members. Now it uses (user_id, invite_email)
instead of (user_id)
to deduplicate (because user_id
may be nil
).
However, after the operation in "How to set up and validate locally", the following types of data
SQL changes
Click to expand
Before
[5] pry(main)> GroupMember.distinct_on_user_with_max_access_level
GroupMember Load (1.4ms) SELECT "members".* FROM (SELECT DISTINCT ON (user_id, invite_email) * FROM "members" WHERE "members"."type" = 'GroupMember' AND "members"."source_type" = 'Namespace' ORDER BY user_id, invite_email, access_level DESC, expires_at DESC, created_at ASC) members WHERE "members"."type" = 'GroupMember' /*application:console,db_config_name:main,console_hostname:MacBook-Pro.local,console_username:deer,line:bin/rails:4:in `<main>'*/
After
[5] pry(main)> GroupMember.distinct_on_user_with_max_access_level
GroupMember Load (1.7ms) SELECT "members".* FROM (SELECT DISTINCT ON (COALESCE(user_id::Text, invite_email)) * FROM "members" WHERE "members"."type" = 'GroupMember' AND "members"."source_type" = 'Namespace' ORDER BY COALESCE(user_id::Text, invite_email), access_level DESC, expires_at DESC, created_at ASC) members WHERE "members"."type" = 'GroupMember' /*application:console,db_config_name:main,console_hostname:MacBook-Pro.local,console_username:deer,line:bin/rails:4:in `<main>'*/
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
Before | After |
---|---|
How to set up and validate locally
This step is used to reproduce the problem:
Click to expand
- Prepare a rootgroup and subgroup
- In the rootgroup, invite user through unregistered email
- The above email will receive an invitation link, through this link (remember to change the browser), register a new user
- In the subgroup, invite the new user again
- You're done! On the members list page of the subgroup, you can see that this user appears repeatedly
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.