Commit 1d8e8f3e authored by charlie ablett's avatar charlie ablett

Update tests to reflect sub-batched query

parent 7f6ac436
...@@ -7,23 +7,15 @@ module Gitlab ...@@ -7,23 +7,15 @@ module Gitlab
class BackfillUserNamespace class BackfillUserNamespace
include Gitlab::Database::DynamicModelHelpers include Gitlab::Database::DynamicModelHelpers
def perform(start_id, end_id, batch_table, batch_column, sub_batch_size) def perform(start_id, end_id, batch_table, batch_column, sub_batch_size, pause_ms)
parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id) parent_batch_relation = relation_scoped_to_range(batch_table, batch_column, start_id, end_id)
parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size, order_hint: :type) do |sub_batch|
parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
batch_metrics.time_operation(:update_all) do batch_metrics.time_operation(:update_all) do
sub_batch.update_all(type: 'User') sub_batch.update_all(type: 'User')
end end
pause_ms = 0 if pause_ms < 0 pause_ms = 0 if pause_ms < 0
sleep(pause_ms * 0.001) sleep(pause_ms * 0.001)
end end
# ActiveRecord::Base.connection.execute(<<~SQL)
# UPDATE namespaces SET type = 'User'
# WHERE id BETWEEN #{start_id} AND #{end_id}
# AND type IS NULL
# SQL
end end
def batch_metrics def batch_metrics
......
...@@ -2,24 +2,38 @@ ...@@ -2,24 +2,38 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace do RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace, :migration, schema: 20210930211936 do
let(:migration) { described_class.new } let(:migration) { described_class.new }
let(:namespaces) { table(:namespaces) } let(:namespaces_table) { table(:namespaces) }
let(:table_name) { 'namespaces' }
let(:batch_column) { :id }
let(:sub_batch_size) { 100 }
let(:pause_ms) { 0 }
subject(:perform_migration) { migration.perform(1, 10, table_name, batch_column, sub_batch_size, pause_ms) }
before do before do
namespaces.create!(id: 1, name: 'test1', path: 'test1', type: nil) namespaces_table.create!(id: 1, name: 'test1', path: 'test1', type: nil)
namespaces.create!(id: 2, name: 'test2', path: 'test2', type: 'User') namespaces_table.create!(id: 2, name: 'test2', path: 'test2', type: 'User')
namespaces.create!(id: 3, name: 'test3', path: 'test3', type: 'Group') namespaces_table.create!(id: 3, name: 'test3', path: 'test3', type: 'Group')
namespaces.create!(id: 4, name: 'test4', path: 'test4', type: nil) namespaces_table.create!(id: 4, name: 'test4', path: 'test4', type: nil)
namespaces.create!(id: 11, name: 'test11', path: 'test11', type: nil) namespaces_table.create!(id: 11, name: 'test11', path: 'test11', type: nil)
end end
it 'backfills `type` for the selected records' do it 'backfills `type` for the selected records', :aggregate_failures do
queries = ActiveRecord::QueryRecorder.new do queries = ActiveRecord::QueryRecorder.new do
migration.perform(1, 10) perform_migration
end end
expect(queries.count).to be(1) expect(queries.count).to eq(3)
expect(Namespace.where(type: 'User').count).to eq 3 expect(namespaces_table.where(type: 'User').count).to eq 3
expect(namespaces_table.where(type: 'User').pluck(:id)).to match_array([1, 2, 4])
end
it 'tracks timings of queries' do
expect(migration.batch_metrics.timings).to be_empty
expect { perform_migration }.to change { migration.batch_metrics.timings }
end end
end end
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