Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
df7d65a7
Commit
df7d65a7
authored
Feb 16, 2018
by
Andreas Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify database queries in MembersFinder.
Closes #41461.
parent
5048c8d5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
11 deletions
+22
-11
app/finders/members_finder.rb
app/finders/members_finder.rb
+22
-11
No files found.
app/finders/members_finder.rb
View file @
df7d65a7
...
...
@@ -10,23 +10,34 @@ class MembersFinder
def
execute
project_members
=
project
.
project_members
project_members
=
project_members
.
non_invite
unless
can?
(
current_user
,
:admin_project
,
project
)
wheres
=
[
"members.id IN (
#{
project_members
.
select
(
:id
).
to_sql
}
)"
]
if
group
# We need `.where.not(user_id: nil)` here otherwise when a group has an
# invitee, it would make the following query return 0 rows since a NULL
# user_id would be present in the subquery
# See http://stackoverflow.com/questions/129077/not-in-clause-and-null-values
non_null_user_ids
=
project_members
.
where
.
not
(
user_id:
nil
).
select
(
:user_id
)
group_members
=
GroupMembersFinder
.
new
(
group
).
execute
group_members
=
group_members
.
where
.
not
(
user_id:
non_null_user_ids
)
group_members
=
group_members
.
non_invite
unless
can?
(
current_user
,
:admin_group
,
group
)
wheres
<<
"members.id IN (
#{
group_members
.
select
(
:id
).
to_sql
}
)"
end
union
=
Gitlab
::
SQL
::
Union
.
new
([
project_members
,
group_members
],
remove_duplicates:
false
)
Member
.
where
(
wheres
.
join
(
' OR '
))
# We're interested in a list of members without duplicates by user_id.
# We prefer project members over group members, project members should go first.
#
# We could have used a DISTINCT ON here, but MySQL does not support this.
sql
=
<<-
SQL
SELECT member_numbered.*
FROM (
SELECT
member_union.*,
ROW_NUMBER() OVER (
PARTITION BY user_id ORDER BY CASE WHEN type = 'ProjectMember' THEN 1 WHEN type = 'GroupMember' THEN 2 ELSE 3 END
) AS row_number
FROM (
#{
union
.
to_sql
}
) AS member_union
) AS member_numbered
WHERE row_number = 1
SQL
Member
.
from
(
"(
#{
sql
}
) AS
#{
Member
.
table_name
}
"
)
else
project_members
end
end
def
can?
(
*
args
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment