Class: Gitlab::ImportExport::Json::StreamingSerializer

Inherits:
Object
  • Object
show all
Includes:
CommandLineUtil
Defined in:
lib/gitlab/import_export/json/streaming_serializer.rb

Defined Under Namespace

Classes: Raw

Constant Summary collapse

BATCH_SIZE =
100

Constants included from CommandLineUtil

CommandLineUtil::CLEAN_DIR_IGNORE_FILE_NAMES, CommandLineUtil::CommandLineUtilError, CommandLineUtil::DEFAULT_DIR_MODE, CommandLineUtil::FileOversizedError, CommandLineUtil::HardLinkError, CommandLineUtil::UNTAR_MASK

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandLineUtil

#gunzip, #gzip, #gzip_with_options, #mkdir_p, #tar_cf, #tar_czf, #untar_xf, #untar_zxf

Constructor Details

#initialize(exportable, relations_schema, json_writer, current_user:, exportable_path:, logger: Gitlab::Export::Logger) ⇒ StreamingSerializer

Returns a new instance of StreamingSerializer.



19
20
21
22
23
24
25
26
27
# File 'lib/gitlab/import_export/json/streaming_serializer.rb', line 19

def initialize(exportable, relations_schema, json_writer, current_user:, exportable_path:, logger: Gitlab::Export::Logger)
  @exportable = exportable
  @current_user = current_user
  @exportable_path = exportable_path
  @relations_schema = relations_schema
  @json_writer = json_writer
  @logger = logger
  @exported_objects_count = 0
end

Instance Attribute Details

#exported_objects_countObject (readonly)

Returns the value of attribute exported_objects_count.



11
12
13
# File 'lib/gitlab/import_export/json/streaming_serializer.rb', line 11

def exported_objects_count
  @exported_objects_count
end

Instance Method Details

#executeObject



29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/import_export/json/streaming_serializer.rb', line 29

def execute
  read_from_replica_if_available do
    serialize_root

    includes.each do |relation_definition|
      serialize_relation(relation_definition)
    end
  end
end

#serialize_relation(definition, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gitlab/import_export/json/streaming_serializer.rb', line 50

def serialize_relation(definition, options = {})
  raise ArgumentError, 'definition needs to be Hash' unless definition.is_a?(Hash)
  raise ArgumentError, 'definition needs to have exactly one Hash element' unless definition.one?

  key, definition_options = definition.first

  record = exportable.public_send(key) # rubocop: disable GitlabSecurity/PublicSend

  if options[:batch_ids]
    record = record.where(record.model.primary_key => Array.wrap(options[:batch_ids]).map(&:to_i))
  end

  if record.is_a?(ActiveRecord::Relation)
    serialize_many_relations(key, record, definition_options)
  elsif record.respond_to?(:each) # this is to support `project_members` that return an Array
    serialize_many_each(key, record, definition_options)
  else
    serialize_single_relation(key, record, definition_options)
  end
end

#serialize_root(exportable_path = @exportable_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/gitlab/import_export/json/streaming_serializer.rb', line 39

def serialize_root(exportable_path = @exportable_path)
  log_relation_export('root')

  attributes = exportable.as_json(
    relations_schema.merge(include: nil, preloads: nil, unsafe: true))

  json_writer.write_attributes(exportable_path, attributes)

  increment_exported_objects_counter
end