summaryrefslogtreecommitdiff
path: root/app/models/project_authorization.rb
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2016-12-06 17:31:58 +0100
committerYorick Peterse <yorickpeterse@gmail.com>2016-12-19 17:11:03 +0100
commitf73193c328b871a9a3af803012c10d9bc1bd0904 (patch)
tree016eafe1c321574b87b607325fd62e0f95779583 /app/models/project_authorization.rb
parenta50cd9eb4b4392004e47e57b2fa37c12def5827f (diff)
downloadgitlab-ce-f73193c328b871a9a3af803012c10d9bc1bd0904.tar.gz
Smarter refreshing of authorized projectsproject-authorizations-diff
Prior to this commit the refreshing of authorized projects was done in two steps: 1. Remove existing authorizations 2. Insert a new list of all authorizations This can lead to a high amount of dead tuples as every time all rows are being replaced. For example, if a user with 100 authorizations is given access to a new project this would lead to: * 100 rows being removed * 101 new rows being inserted This commit changes the way this system works so it only removes/inserts what is necessary. Using the above example this would lead to only 1 new row being inserted, with the initial 100 being left untouched. Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/25257
Diffstat (limited to 'app/models/project_authorization.rb')
-rw-r--r--app/models/project_authorization.rb13
1 files changed, 13 insertions, 0 deletions
diff --git a/app/models/project_authorization.rb b/app/models/project_authorization.rb
index a00d43773d9..4c7f4f5a429 100644
--- a/app/models/project_authorization.rb
+++ b/app/models/project_authorization.rb
@@ -5,4 +5,17 @@ class ProjectAuthorization < ActiveRecord::Base
validates :project, presence: true
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
validates :user, uniqueness: { scope: [:project, :access_level] }, presence: true
+
+ def self.insert_authorizations(rows, per_batch = 1000)
+ rows.each_slice(per_batch) do |slice|
+ tuples = slice.map do |tuple|
+ tuple.map { |value| connection.quote(value) }
+ end
+
+ connection.execute <<-EOF.strip_heredoc
+ INSERT INTO project_authorizations (user_id, project_id, access_level)
+ VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
+ EOF
+ end
+ end
end