Skip to content
Snippets Groups Projects
Commit ece51e16 authored by Adam Hegyi's avatar Adam Hegyi
Browse files

Reduce gitlab project import queries

This change reduces the number of DB queries when importing a gitlab
project.
parent 7c36265f
No related branches found
No related tags found
1 merge request!58866Reduce gitlab project import queries [RUN ALL RSPEC] [RUN AS-IF-FOSS]
......@@ -66,6 +66,12 @@ def self.safe_find_or_create_by(*args, &block)
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
Gitlab::SafeRequestStore.fetch("model:#{self}:underscore") { self.to_s.underscore }
end
......
......@@ -104,16 +104,13 @@ class Project < ApplicationRecord
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,
unless: :ci_cd_settings
after_create -> { create_or_load_association(:ci_cd_settings) }
after_create :create_container_expiration_policy,
unless: :container_expiration_policy
after_create -> { create_or_load_association(:container_expiration_policy) }
after_create :create_pages_metadatum,
unless: :pages_metadatum
after_create -> { create_or_load_association(:pages_metadatum) }
after_create :set_timestamps_for_create
after_update :update_forks_visibility_level
......
......@@ -40,7 +40,7 @@ def execute
if namespace_id
# Find matching namespace and check if it allowed
# 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
deny_namespace
return @project
......@@ -83,13 +83,6 @@ def deny_namespace
@project.errors.add(:namespace, "is not valid")
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
log_info("#{@project.owner.name} created a new project \"#{@project.full_name}\"")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment