Commit fe2c7148 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'georgekoltsov/export-in-batches-instead-of-for-each' into 'master'

Use #in_batches in Project Export StreamingSerializer

See merge request gitlab-org/gitlab!35125
parents 40081c42 9d4482ec
......@@ -69,10 +69,18 @@ module Gitlab
key_preloads = preloads&.dig(key)
records = records.preload(key_preloads) if key_preloads
records.find_each(batch_size: batch_size) do |record|
records.in_batches(of: batch_size) do |batch| # rubocop:disable Cop/InBatches
# order each batch by its primary key to ensure
# consistent and predictable ordering of each exported relation
# as additional `WHERE` clauses can impact the order in which data is being
# returned by database when no `ORDER` is specified
batch = batch.reorder(batch.klass.primary_key)
batch.each do |record|
items << Raw.new(record.to_json(options))
end
end
end
json_writer.write_relation_array(@exportable_path, key, enumerator)
end
......
......@@ -61,6 +61,20 @@ RSpec.describe Gitlab::ImportExport::JSON::StreamingSerializer do
subject.execute
end
context 'relation ordering' do
before do
create_list(:issue, 5, project: exportable)
end
it 'orders exported issues by primary key' do
expected_issues = exportable.issues.reorder(:id).map(&:to_json)
expect(json_writer).to receive(:write_relation_array).with(exportable_path, :issues, expected_issues)
subject.execute
end
end
end
context 'with single relation' do
......
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