diff options
author | Nick Thomas <nick@gitlab.com> | 2016-12-20 13:16:07 +0000 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2016-12-20 13:16:07 +0000 |
commit | 1139da2745eb28be17c8fd6aa0f5de113c0f06a9 (patch) | |
tree | cccbae8b3a7d46cc9d673b114cae143c8f70b5c5 /app/models/user.rb | |
parent | a85220506eb3a7516008a1ffe67beff9550cdd1c (diff) | |
parent | f73193c328b871a9a3af803012c10d9bc1bd0904 (diff) | |
download | gitlab-ce-1139da2745eb28be17c8fd6aa0f5de113c0f06a9.tar.gz |
Merge branch 'project-authorizations-diff' into 'master'
Smarter refreshing of authorized projects
This MR reworks the way `User#refresh_authorized_projects`, resulting in a much more efficient process leading to fewer dead tuples.
Related issue: https://gitlab.com/gitlab-org/gitlab-ce/issues/25257
See merge request !7956
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 38 |
1 files changed, 10 insertions, 28 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index d771eaff472..899a89a4eaa 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -439,22 +439,16 @@ class User < ActiveRecord::Base end def refresh_authorized_projects - transaction do - project_authorizations.delete_all - - # project_authorizations_union can return multiple records for the same - # project/user with different access_level so we take row with the maximum - # access_level - project_authorizations.connection.execute <<-SQL - INSERT INTO project_authorizations (user_id, project_id, access_level) - SELECT user_id, project_id, MAX(access_level) AS access_level - FROM (#{project_authorizations_union.to_sql}) sub - GROUP BY user_id, project_id - SQL - - unless authorized_projects_populated - update_column(:authorized_projects_populated, true) - end + Users::RefreshAuthorizedProjectsService.new(self).execute + end + + def remove_project_authorizations(project_ids) + project_authorizations.where(id: project_ids).delete_all + end + + def set_authorized_projects_column + unless authorized_projects_populated + update_column(:authorized_projects_populated, true) end end @@ -901,18 +895,6 @@ class User < ActiveRecord::Base private - # Returns a union query of projects that the user is authorized to access - def project_authorizations_union - relations = [ - personal_projects.select("#{id} AS user_id, projects.id AS project_id, #{Gitlab::Access::MASTER} AS access_level"), - groups_projects.select_for_project_authorization, - projects.select_for_project_authorization, - groups.joins(:shared_projects).select_for_project_authorization - ] - - Gitlab::SQL::Union.new(relations) - end - def ci_projects_union scope = { access_level: [Gitlab::Access::MASTER, Gitlab::Access::OWNER] } groups = groups_projects.where(members: scope) |