Commit ece51e16 authored by Adam Hegyi's avatar Adam Hegyi

Reduce gitlab project import queries

This change reduces the number of DB queries when importing a gitlab
project.
parent 7c36265f
......@@ -66,6 +66,12 @@ class ApplicationRecord < ActiveRecord::Base
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 @@ module Projects
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 @@ module Projects
@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}\"")
......
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