Commit d8d05e5f authored by James Lopez's avatar James Lopez

refactor code a little

parent 4119206f
...@@ -10,6 +10,7 @@ module Gitlab ...@@ -10,6 +10,7 @@ module Gitlab
@shared = shared @shared = shared
@project = project @project = project
@project_id = project.id @project_id = project.id
@saved = []
end end
def restore def restore
...@@ -51,7 +52,6 @@ module Gitlab ...@@ -51,7 +52,6 @@ module Gitlab
# the configuration yaml file too. # the configuration yaml file too.
# Finally, it updates each attribute in the newly imported project. # Finally, it updates each attribute in the newly imported project.
def create_relations def create_relations
@saved = []
default_relation_list.each do |relation| default_relation_list.each do |relation|
next unless relation.is_a?(Hash) || @tree_hash[relation.to_s].present? next unless relation.is_a?(Hash) || @tree_hash[relation.to_s].present?
...@@ -59,10 +59,10 @@ module Gitlab ...@@ -59,10 +59,10 @@ module Gitlab
create_sub_relations(relation, @tree_hash) create_sub_relations(relation, @tree_hash)
else else
relation_key = relation.is_a?(Hash) ? relation.keys.first : relation relation_key = relation.is_a?(Hash) ? relation.keys.first : relation
relation_hash_list = @tree_hash[relation_key.to_s] save_relation_hash(@tree_hash[relation_key.to_s], relation_key)
save_relation_hash(relation_hash_list, relation_key)
end end
end end
@saved.all? @saved.all?
end end
...@@ -70,6 +70,8 @@ module Gitlab ...@@ -70,6 +70,8 @@ module Gitlab
relation_hash = create_relation(relation_key, relation_hash_batch) relation_hash = create_relation(relation_key, relation_hash_batch)
@saved << restored_project.append_or_update_attribute(relation_key, relation_hash) @saved << restored_project.append_or_update_attribute(relation_key, relation_hash)
# Restore the project again, extra query but let us skip holding the AR objects in memory
@restored_project = Project.find_by_id(@project_id) @restored_project = Project.find_by_id(@project_id)
end end
...@@ -107,10 +109,17 @@ module Gitlab ...@@ -107,10 +109,17 @@ module Gitlab
tree_array = [tree_hash[relation_key]].flatten tree_array = [tree_hash[relation_key]].flatten
# Avoid keeping a possible heavy object in memory once we are done with it
while relation_item = tree_array.shift while relation_item = tree_array.shift
# The transaction at this level is less speedy than one single transaction
# But we can't have it in the upper level or GC won't get rid of the AR objects
# after we save the batch.
Project.transaction do Project.transaction do
process_sub_relation(relation, relation_item) process_sub_relation(relation, relation_item)
# For every subrelation that hangs from Project, save the associated records alltogether
# This effectively batches all records per subrelation item, only keeping those in memory
# We have to keep in mind that more batch granularity << Memory, but >> Slowness
if save if save
save_relation_hash([relation_item], relation_key) save_relation_hash([relation_item], relation_key)
tree_hash[relation_key].delete(relation_item) tree_hash[relation_key].delete(relation_item)
......
...@@ -261,7 +261,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver do ...@@ -261,7 +261,7 @@ describe Gitlab::ImportExport::ProjectTreeSaver do
ci_build = create(:ci_build, project: project, when: nil) ci_build = create(:ci_build, project: project, when: nil)
ci_build.pipeline.update(project: project) ci_build.pipeline.update(project: project)
commit_status = create(:commit_status, project: project, pipeline: ci_build.pipeline) create(:commit_status, project: project, pipeline: ci_build.pipeline)
create(:milestone, project: project) create(:milestone, project: project)
create(:note, noteable: issue, project: project) create(:note, noteable: issue, project: project)
......
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