2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class ProjectAuthorization < ApplicationRecord
|
2021-03-11 19:13:27 +05:30
|
|
|
extend SuppressCompositePrimaryKeyWarning
|
2018-12-05 23:21:45 +05:30
|
|
|
include FromUnion
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :user
|
|
|
|
belongs_to :project
|
|
|
|
|
|
|
|
validates :project, presence: true
|
|
|
|
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
|
|
|
|
validates :user, uniqueness: { scope: [:project, :access_level] }, presence: true
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def self.select_from_union(relations)
|
|
|
|
from_union(relations)
|
|
|
|
.select(['project_id', 'MAX(access_level) AS access_level'])
|
2017-09-10 17:25:29 +05:30
|
|
|
.group(:project_id)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
2021-12-11 22:18:48 +05:30
|
|
|
INSERT INTO project_authorizations (user_id, project_id, access_level)
|
|
|
|
VALUES #{tuples.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
|
|
|
|
ON CONFLICT DO NOTHING
|
2017-08-17 22:00:37 +05:30
|
|
|
EOF
|
|
|
|
end
|
|
|
|
end
|
2021-09-04 01:27:46 +05:30
|
|
|
|
|
|
|
# This method overrides its ActiveRecord's version in order to work correctly
|
|
|
|
# with composite primary keys and fix the tests for Rails 6.1
|
|
|
|
#
|
|
|
|
# Consider using BulkInsertSafe module instead since we plan to refactor it in
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/331264
|
|
|
|
def self.insert_all(attributes)
|
|
|
|
super(attributes, unique_by: connection.schema_cache.primary_keys(table_name))
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
ProjectAuthorization.prepend_mod_with('ProjectAuthorization')
|