Commit c0a23ae3 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'import-reduce-amount-of-project-find-sql-queries' into 'master'

Do not use `restored_project` to reduce amount of SQL queries

See merge request gitlab-org/gitlab!18005
parents 54ed2eca 59fa3064
...@@ -312,7 +312,7 @@ module Gitlab ...@@ -312,7 +312,7 @@ module Gitlab
class Importer class Importer
def execute def execute
if import_file && check_version! && restorers.all?(&:restore) && overwrite_project if import_file && check_version! && restorers.all?(&:restore) && overwrite_project
project_tree.restored_project project
else else
raise Projects::ImportService::Error.new(@shared.errors.join(', ')) raise Projects::ImportService::Error.new(@shared.errors.join(', '))
end end
......
...@@ -17,7 +17,7 @@ module EE::Gitlab::ImportExport::Importer ...@@ -17,7 +17,7 @@ module EE::Gitlab::ImportExport::Importer
Gitlab::ImportExport::DesignRepoRestorer.new( Gitlab::ImportExport::DesignRepoRestorer.new(
path_to_bundle: design_repo_path, path_to_bundle: design_repo_path,
shared: shared, shared: shared,
project: project_tree.restored_project project: project
) )
end end
......
...@@ -9,11 +9,11 @@ module EE ...@@ -9,11 +9,11 @@ module EE
private private
attr_accessor :project
override :remove_feature_dependent_sub_relations override :remove_feature_dependent_sub_relations
def remove_feature_dependent_sub_relations(relation_item) def remove_feature_dependent_sub_relations(relation_item)
if relation_item.is_a?(Hash) && ::Feature.disabled?(:export_designs, project, default_enabled: true) export_designs_disabled = ::Feature.disabled?(:export_designs, project, default_enabled: true)
if relation_item.is_a?(Hash) && export_designs_disabled
relation_item.except!('designs', 'design_versions') relation_item.except!('designs', 'design_versions')
end end
end end
......
...@@ -19,7 +19,7 @@ module Gitlab ...@@ -19,7 +19,7 @@ module Gitlab
def execute def execute
if import_file && check_version! && restorers.all?(&:restore) && overwrite_project if import_file && check_version! && restorers.all?(&:restore) && overwrite_project
project_tree.restored_project project
else else
raise Projects::ImportService::Error.new(shared.errors.to_sentence) raise Projects::ImportService::Error.new(shared.errors.to_sentence)
end end
...@@ -55,32 +55,32 @@ module Gitlab ...@@ -55,32 +55,32 @@ module Gitlab
end end
def avatar_restorer def avatar_restorer
Gitlab::ImportExport::AvatarRestorer.new(project: project_tree.restored_project, shared: shared) Gitlab::ImportExport::AvatarRestorer.new(project: project, shared: shared)
end end
def repo_restorer def repo_restorer
Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path, Gitlab::ImportExport::RepoRestorer.new(path_to_bundle: repo_path,
shared: shared, shared: shared,
project: project_tree.restored_project) project: project)
end end
def wiki_restorer def wiki_restorer
Gitlab::ImportExport::WikiRestorer.new(path_to_bundle: wiki_repo_path, Gitlab::ImportExport::WikiRestorer.new(path_to_bundle: wiki_repo_path,
shared: shared, shared: shared,
project: ProjectWiki.new(project_tree.restored_project), project: ProjectWiki.new(project),
wiki_enabled: project.wiki_enabled?) wiki_enabled: project.wiki_enabled?)
end end
def uploads_restorer def uploads_restorer
Gitlab::ImportExport::UploadsRestorer.new(project: project_tree.restored_project, shared: shared) Gitlab::ImportExport::UploadsRestorer.new(project: project, shared: shared)
end end
def lfs_restorer def lfs_restorer
Gitlab::ImportExport::LfsRestorer.new(project: project_tree.restored_project, shared: shared) Gitlab::ImportExport::LfsRestorer.new(project: project, shared: shared)
end end
def statistics_restorer def statistics_restorer
Gitlab::ImportExport::StatisticsRestorer.new(project: project_tree.restored_project, shared: shared) Gitlab::ImportExport::StatisticsRestorer.new(project: project, shared: shared)
end end
def path_with_namespace def path_with_namespace
...@@ -105,8 +105,6 @@ module Gitlab ...@@ -105,8 +105,6 @@ module Gitlab
end end
def overwrite_project def overwrite_project
project = project_tree.restored_project
return unless can?(current_user, :admin_namespace, project.namespace) return unless can?(current_user, :admin_namespace, project.namespace)
if overwrite_project? if overwrite_project?
......
...@@ -6,19 +6,21 @@ module Gitlab ...@@ -6,19 +6,21 @@ module Gitlab
# Relations which cannot be saved at project level (and have a group assigned) # Relations which cannot be saved at project level (and have a group assigned)
GROUP_MODELS = [GroupLabel, Milestone].freeze GROUP_MODELS = [GroupLabel, Milestone].freeze
attr_reader :user
attr_reader :shared
attr_reader :project
def initialize(user:, shared:, project:) def initialize(user:, shared:, project:)
@path = File.join(shared.export_path, 'project.json') @path = File.join(shared.export_path, 'project.json')
@user = user @user = user
@shared = shared @shared = shared
@project = project @project = project
@project_id = project.id
@saved = true @saved = true
end end
def restore def restore
begin begin
json = IO.read(@path) @tree_hash = read_tree_hash
@tree_hash = ActiveSupport::JSON.decode(json)
rescue => e rescue => e
Rails.logger.error("Import/Export error: #{e.message}") # rubocop:disable Gitlab/RailsLogger Rails.logger.error("Import/Export error: #{e.message}") # rubocop:disable Gitlab/RailsLogger
raise Gitlab::ImportExport::Error.new('Incorrect JSON format') raise Gitlab::ImportExport::Error.new('Incorrect JSON format')
...@@ -30,26 +32,31 @@ module Gitlab ...@@ -30,26 +32,31 @@ module Gitlab
ActiveRecord::Base.uncached do ActiveRecord::Base.uncached do
ActiveRecord::Base.no_touching do ActiveRecord::Base.no_touching do
update_project_params
create_relations create_relations
end end
end end
# ensure that we have latest version of the restore
@project.reload # rubocop:disable Cop/ActiveRecordAssociationReload
true
rescue => e rescue => e
@shared.error(e) @shared.error(e)
false false
end end
def restored_project private
return @project unless @tree_hash
@restored_project ||= restore_project def read_tree_hash
json = IO.read(@path)
ActiveSupport::JSON.decode(json)
end end
private
def members_mapper def members_mapper
@members_mapper ||= Gitlab::ImportExport::MembersMapper.new(exported_members: @project_members, @members_mapper ||= Gitlab::ImportExport::MembersMapper.new(exported_members: @project_members,
user: @user, user: @user,
project: restored_project) project: @project)
end end
# A Hash of the imported merge request ID -> imported ID. # A Hash of the imported merge request ID -> imported ID.
...@@ -83,12 +90,11 @@ module Gitlab ...@@ -83,12 +90,11 @@ module Gitlab
remove_group_models(relation_hash) if relation_hash.is_a?(Array) remove_group_models(relation_hash) if relation_hash.is_a?(Array)
@saved = false unless restored_project.append_or_update_attribute(relation_key, relation_hash) @saved = false unless @project.append_or_update_attribute(relation_key, relation_hash)
save_id_mappings(relation_key, relation_hash_batch, relation_hash) save_id_mappings(relation_key, relation_hash_batch, relation_hash)
# Restore the project again, extra query that skips holding the AR objects in memory @project.reset
@restored_project = Project.find(@project_id)
end end
# Older, serialized CI pipeline exports may only have a # Older, serialized CI pipeline exports may only have a
...@@ -127,12 +133,10 @@ module Gitlab ...@@ -127,12 +133,10 @@ module Gitlab
reader.attributes_finder.find_relations_tree(:project) reader.attributes_finder.find_relations_tree(:project)
end end
def restore_project def update_project_params
Gitlab::Timeless.timeless(@project) do Gitlab::Timeless.timeless(@project) do
@project.update(project_params) @project.update(project_params)
end end
@project
end end
def project_params def project_params
...@@ -245,7 +249,7 @@ module Gitlab ...@@ -245,7 +249,7 @@ module Gitlab
members_mapper: members_mapper, members_mapper: members_mapper,
merge_requests_mapping: merge_requests_mapping, merge_requests_mapping: merge_requests_mapping,
user: @user, user: @user,
project: @restored_project, project: @project,
excluded_keys: excluded_keys_for_relation(relation_key)) excluded_keys: excluded_keys_for_relation(relation_key))
end.compact end.compact
......
...@@ -520,20 +520,21 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -520,20 +520,21 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
end end
end end
describe '#restored_project' do context 'Minimal JSON' do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:tree_hash) { { 'visibility_level' => visibility } } let(:tree_hash) { { 'visibility_level' => visibility } }
let(:restorer) { described_class.new(user: nil, shared: shared, project: project) } let(:restorer) { described_class.new(user: nil, shared: shared, project: project) }
before do before do
restorer.instance_variable_set(:@tree_hash, tree_hash) expect(restorer).to receive(:read_tree_hash) { tree_hash }
end end
context 'no group visibility' do context 'no group visibility' do
let(:visibility) { Gitlab::VisibilityLevel::PRIVATE } let(:visibility) { Gitlab::VisibilityLevel::PRIVATE }
it 'uses the project visibility' do it 'uses the project visibility' do
expect(restorer.restored_project.visibility_level).to eq(visibility) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(visibility)
end end
end end
...@@ -544,7 +545,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -544,7 +545,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
it 'uses private visibility' do it 'uses private visibility' do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL]) stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL])
expect(restorer.restored_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
end end
end end
end end
...@@ -561,7 +563,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -561,7 +563,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:visibility) { Gitlab::VisibilityLevel::PUBLIC } let(:visibility) { Gitlab::VisibilityLevel::PUBLIC }
it 'uses the group visibility' do it 'uses the group visibility' do
expect(restorer.restored_project.visibility_level).to eq(group_visibility) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(group_visibility)
end end
end end
...@@ -570,7 +573,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -570,7 +573,8 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:visibility) { Gitlab::VisibilityLevel::PRIVATE } let(:visibility) { Gitlab::VisibilityLevel::PRIVATE }
it 'uses the project visibility' do it 'uses the project visibility' do
expect(restorer.restored_project.visibility_level).to eq(visibility) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(visibility)
end end
end end
...@@ -579,14 +583,16 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do ...@@ -579,14 +583,16 @@ describe Gitlab::ImportExport::ProjectTreeRestorer do
let(:visibility) { Gitlab::VisibilityLevel::PUBLIC } let(:visibility) { Gitlab::VisibilityLevel::PUBLIC }
it 'uses the group visibility' do it 'uses the group visibility' do
expect(restorer.restored_project.visibility_level).to eq(group_visibility) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(group_visibility)
end end
context 'with restricted internal visibility' do context 'with restricted internal visibility' do
it 'sets private visibility' do it 'sets private visibility' do
stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL]) stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::INTERNAL])
expect(restorer.restored_project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE) expect(restorer.restore).to eq(true)
expect(restorer.project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
end end
end end
end end
......
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