Commit 9d4482ec authored by George Koltsov's avatar George Koltsov

Use #in_batches in Project Export StreamingSerializer

parent 1232af43
......@@ -69,8 +69,16 @@ module Gitlab
key_preloads = preloads&.dig(key)
records = records.preload(key_preloads) if key_preloads
records.find_each(batch_size: batch_size) do |record|
items << Raw.new(record.to_json(options))
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
......
......@@ -61,6 +61,20 @@ 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