2021-10-13 15:12:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
class TreeExportService
|
|
|
|
def initialize(portable, export_path, relation)
|
|
|
|
@portable = portable
|
|
|
|
@export_path = export_path
|
|
|
|
@relation = relation
|
|
|
|
@config = FileTransfer.config_for(portable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-12-11 00:15:10 +00:00
|
|
|
return serializer.serialize_root(config.class::SELF_RELATION) if self_relation?
|
|
|
|
|
2021-10-13 15:12:51 +00:00
|
|
|
relation_definition = config.tree_relation_definition_for(relation)
|
|
|
|
|
|
|
|
raise BulkImports::Error, 'Unsupported relation export type' unless relation_definition
|
|
|
|
|
|
|
|
serializer.serialize_relation(relation_definition)
|
|
|
|
end
|
|
|
|
|
|
|
|
def exported_filename
|
2021-12-11 00:15:10 +00:00
|
|
|
return "#{relation}.json" if self_relation?
|
|
|
|
|
2021-10-13 15:12:51 +00:00
|
|
|
"#{relation}.ndjson"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :export_path, :portable, :relation, :config
|
|
|
|
|
|
|
|
# rubocop: disable CodeReuse/Serializer
|
|
|
|
def serializer
|
|
|
|
::Gitlab::ImportExport::Json::StreamingSerializer.new(
|
|
|
|
portable,
|
|
|
|
config.portable_tree,
|
|
|
|
json_writer,
|
|
|
|
exportable_path: ''
|
|
|
|
)
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/Serializer
|
|
|
|
|
|
|
|
def json_writer
|
|
|
|
::Gitlab::ImportExport::Json::NdjsonWriter.new(export_path)
|
|
|
|
end
|
2021-12-11 00:15:10 +00:00
|
|
|
|
|
|
|
def self_relation?
|
|
|
|
relation == config.class::SELF_RELATION
|
|
|
|
end
|
2021-10-13 15:12:51 +00:00
|
|
|
end
|
|
|
|
end
|