Commit e5e8f13a authored by Matthias Käppler's avatar Matthias Käppler

Merge branch '21041-recduce-gitlab-project-import-queries' into 'master'

Reduce gitlab project import queries [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!58866
parents f5d4687c ece51e16
...@@ -66,6 +66,12 @@ class ApplicationRecord < ActiveRecord::Base ...@@ -66,6 +66,12 @@ class ApplicationRecord < ActiveRecord::Base
end end
end end
def create_or_load_association(association_name)
association(association_name).create unless association(association_name).loaded?
rescue ActiveRecord::RecordNotUnique, PG::UniqueViolation
association(association_name).reader
end
def self.underscore def self.underscore
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore } Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
end end
......
...@@ -104,16 +104,13 @@ class Project < ApplicationRecord ...@@ -104,16 +104,13 @@ class Project < ApplicationRecord
after_save :create_import_state, if: ->(project) { project.import? && project.import_state.nil? } after_save :create_import_state, if: ->(project) { project.import? && project.import_state.nil? }
after_create :create_project_feature, unless: :project_feature after_create -> { create_or_load_association(:project_feature) }
after_create :create_ci_cd_settings, after_create -> { create_or_load_association(:ci_cd_settings) }
unless: :ci_cd_settings
after_create :create_container_expiration_policy, after_create -> { create_or_load_association(:container_expiration_policy) }
unless: :container_expiration_policy
after_create :create_pages_metadatum, after_create -> { create_or_load_association(:pages_metadatum) }
unless: :pages_metadatum
after_create :set_timestamps_for_create after_create :set_timestamps_for_create
after_update :update_forks_visibility_level after_update :update_forks_visibility_level
......
...@@ -40,7 +40,7 @@ module Projects ...@@ -40,7 +40,7 @@ module Projects
if namespace_id if namespace_id
# Find matching namespace and check if it allowed # Find matching namespace and check if it allowed
# for current user if namespace_id passed. # for current user if namespace_id passed.
unless allowed_namespace?(current_user, namespace_id) unless current_user.can?(:create_projects, project_namespace)
@project.namespace_id = nil @project.namespace_id = nil
deny_namespace deny_namespace
return @project return @project
...@@ -83,13 +83,6 @@ module Projects ...@@ -83,13 +83,6 @@ module Projects
@project.errors.add(:namespace, "is not valid") @project.errors.add(:namespace, "is not valid")
end end
# rubocop: disable CodeReuse/ActiveRecord
def allowed_namespace?(user, namespace_id)
namespace = Namespace.find_by(id: namespace_id)
current_user.can?(:create_projects, namespace)
end
# rubocop: enable CodeReuse/ActiveRecord
def after_create_actions def after_create_actions
log_info("#{@project.owner.name} created a new project \"#{@project.full_name}\"") log_info("#{@project.owner.name} created a new project \"#{@project.full_name}\"")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment