debian-mirror-gitlab/app/models/project_authorization.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class ProjectAuthorization < ApplicationRecord
2022-11-25 23:54:43 +05:30
BATCH_SIZE = 1000
SLEEP_DELAY = 0.1
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
2022-05-07 20:08:51 +05:30
validates :user, uniqueness: { scope: :project }, presence: true
2017-08-17 22:00:37 +05:30
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
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
2022-01-26 12:08:38 +05:30
2022-11-25 23:54:43 +05:30
def self.insert_all_in_batches(attributes, per_batch = BATCH_SIZE)
add_delay = add_delay_between_batches?(entire_size: attributes.size, batch_size: per_batch)
2022-01-26 12:08:38 +05:30
attributes.each_slice(per_batch) do |attributes_batch|
insert_all(attributes_batch)
2022-11-25 23:54:43 +05:30
perform_delay if add_delay
end
end
def self.delete_all_in_batches_for_project(project:, user_ids:, per_batch: BATCH_SIZE)
add_delay = add_delay_between_batches?(entire_size: user_ids.size, batch_size: per_batch)
user_ids.each_slice(per_batch) do |user_ids_batch|
project.project_authorizations.where(user_id: user_ids_batch).delete_all
perform_delay if add_delay
end
end
def self.delete_all_in_batches_for_user(user:, project_ids:, per_batch: BATCH_SIZE)
add_delay = add_delay_between_batches?(entire_size: project_ids.size, batch_size: per_batch)
project_ids.each_slice(per_batch) do |project_ids_batch|
user.project_authorizations.where(project_id: project_ids_batch).delete_all
perform_delay if add_delay
2022-01-26 12:08:38 +05:30
end
end
2022-11-25 23:54:43 +05:30
private_class_method def self.add_delay_between_batches?(entire_size:, batch_size:)
# The reason for adding a delay is to give the replica database enough time to
# catch up with the primary when large batches of records are being added/removed.
# Hance, we add a delay only if the GitLab installation has a replica database configured.
entire_size > batch_size &&
!::Gitlab::Database::LoadBalancing.primary_only? &&
Feature.enabled?(:enable_minor_delay_during_project_authorizations_refresh)
end
private_class_method def self.perform_delay
sleep(SLEEP_DELAY)
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')