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
class BackfillUserNamespace
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.each_batch(column: batch_column, of: sub_batch_size) do |sub_batch|
parent_batch_relation.each_batch(column: batch_column, of: sub_batch_size, order_hint: :type) do |sub_batch|
batch_metrics.time_operation(:update_all) do
sub_batch.update_all(type: 'User')
end
pause_ms = 0 if pause_ms < 0
sleep(pause_ms * 0.001)
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
def batch_metrics
......
......@@ -2,24 +2,38 @@
require 'spec_helper'
RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace do
RSpec.describe Gitlab::BackgroundMigration::BackfillUserNamespace, :migration, schema: 20210930211936 do
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
namespaces.create!(id: 1, name: 'test1', path: 'test1', type: nil)
namespaces.create!(id: 2, name: 'test2', path: 'test2', type: 'User')
namespaces.create!(id: 3, name: 'test3', path: 'test3', type: 'Group')
namespaces.create!(id: 4, name: 'test4', path: 'test4', type: nil)
namespaces.create!(id: 11, name: 'test11', path: 'test11', type: nil)
namespaces_table.create!(id: 1, name: 'test1', path: 'test1', type: nil)
namespaces_table.create!(id: 2, name: 'test2', path: 'test2', type: 'User')
namespaces_table.create!(id: 3, name: 'test3', path: 'test3', type: 'Group')
namespaces_table.create!(id: 4, name: 'test4', path: 'test4', type: nil)
namespaces_table.create!(id: 11, name: 'test11', path: 'test11', type: nil)
end
it 'backfills `type` for the selected records' do
it 'backfills `type` for the selected records', :aggregate_failures do
queries = ActiveRecord::QueryRecorder.new do
migration.perform(1, 10)
perform_migration
end
expect(queries.count).to be(1)
expect(Namespace.where(type: 'User').count).to eq 3
expect(queries.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
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