Commit 6cfaefeb authored by Stan Hu's avatar Stan Hu

Merge branch 'bbm-make-pk-batching-startegy-easy-to-extend' into 'master'

Batched background migrations: Make PrimaryKeyBatchingStrategy easy to extend

See merge request gitlab-org/gitlab!84231
parents a3b97cf8 81fcb99f
......@@ -23,6 +23,7 @@ module Gitlab
quoted_column_name = model_class.connection.quote_column_name(column_name)
relation = model_class.where("#{quoted_column_name} >= ?", batch_min_value)
relation = apply_additional_filters(relation)
next_batch_bounds = nil
relation.each_batch(of: batch_size, column: column_name) do |batch| # rubocop:disable Lint/UnreachableLoop
......@@ -33,6 +34,20 @@ module Gitlab
next_batch_bounds
end
# Strategies based on PrimaryKeyBatchingStrategy can use
# this method to easily apply additional filters.
#
# Example:
#
# class TypeIsNotNull < PrimaryKeyBatchingStrategy
# def apply_additional_filters(relation)
# relation.where.not(type: nil)
# end
# end
def apply_additional_filters(relation)
relation
end
end
end
end
......
......@@ -44,4 +44,23 @@ RSpec.describe Gitlab::BackgroundMigration::BatchingStrategies::PrimaryKeyBatchi
expect(batch_bounds).to be_nil
end
end
context 'additional filters' do
let(:strategy_with_filters) do
Class.new(described_class) do
def apply_additional_filters(relation)
relation.where.not(type: 'Project')
end
end
end
let(:batching_strategy) { strategy_with_filters.new(connection: ActiveRecord::Base.connection) }
let!(:namespace5) { namespaces.create!(name: 'batchtest5', path: 'batch-test5', type: 'Project') }
it 'applies additional filters' do
batch_bounds = batching_strategy.next_batch(:namespaces, :id, batch_min_value: namespace4.id, batch_size: 3, job_arguments: nil)
expect(batch_bounds).to eq([namespace4.id, namespace4.id])
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