Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions app/models/data_drip/backfill_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ def backfill_class_properly_configured?
errors.add(:backfill_class_name, "must inherit from DataDrip::Backfill")
end

def validate_scope
return unless backfill_class_name.present?
return unless backfill_class

begin
backfill =
backfill_class.new(
batch_size: batch_size || 100,
sleep_time: 5,
backfill_options: options || {}
)
scope = backfill.scope
def validate_scope
return unless backfill_class_name.present?
return unless backfill_class

begin
DataDrip.before_backfill&.call

backfill =
backfill_class.new(
batch_size: batch_size || 100,
sleep_time: 5,
backfill_options: options || {}
)
scope = backfill.scope

scope =
scope.limit(amount_of_elements) if amount_of_elements.present? &&
Expand Down
24 changes: 13 additions & 11 deletions app/models/data_drip/backfill_run_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ def enqueue
enqueued!
end

def run!
running!
migration =
backfill_run.backfill_class.new(
batch_size: batch_size,
sleep_time: 5,
backfill_options: backfill_run.options
)
def run!
running!
DataDrip.before_backfill&.call

migration
.scope
.in_batches(
migration =
backfill_run.backfill_class.new(
batch_size: batch_size,
sleep_time: 5,
backfill_options: backfill_run.options
)

migration
.scope
.in_batches(
of: batch_size,
start: start_id,
finish: finish_id
Expand Down
60 changes: 60 additions & 0 deletions spec/models/data_drip/backfill_run_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,66 @@
let!(:employee4) { Employee.create!(name: "Alice", role: "manager", age: 25) }

describe "#run!" do
describe "before_backfill callback" do
let(:backfill_run) do
backfill_run =
DataDrip::BackfillRun.new(
backfill_class_name: "AddRoleToEmployee",
batch_size: 10,
start_at: 1.hour.from_now,
backfiller: backfiller,
options: {}
)
backfill_run.save!(validate: false)
backfill_run
end

let(:batch) do
batch =
DataDrip::BackfillRunBatch.new(
backfill_run: backfill_run,
batch_size: 10,
start_id: employee1.id,
finish_id: employee3.id,
status: :pending
)
batch.save!(validate: false)
batch.update_column(:status, 0)
batch
end

it "calls DataDrip.before_backfill before processing" do
callback_called = false
original_callback = DataDrip.before_backfill

DataDrip.before_backfill = -> { callback_called = true }

batch.run!

expect(callback_called).to be true
ensure
DataDrip.before_backfill = original_callback
end

it "calls before_backfill before accessing scope" do
call_order = []
original_callback = DataDrip.before_backfill

DataDrip.before_backfill = -> { call_order << :before_backfill }

allow_any_instance_of(AddRoleToEmployee).to receive(:scope).and_wrap_original do |original_method|
call_order << :scope_accessed
original_method.call
end

batch.run!

expect(call_order).to eq([:before_backfill, :scope_accessed])
ensure
DataDrip.before_backfill = original_callback
end
end

let(:backfill_run) do
# Skip validation to allow creation for testing
backfill_run =
Expand Down
41 changes: 41 additions & 0 deletions spec/models/data_drip/backfill_run_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,47 @@
end

describe "validate_scope" do
describe "before_backfill callback" do
it "calls DataDrip.before_backfill during validation" do
callback_called = false
original_callback = DataDrip.before_backfill

DataDrip.before_backfill = -> { callback_called = true }

backfill_run =
DataDrip::BackfillRun.new(
valid_attributes.merge(options: { age: 25 })
)
backfill_run.valid?

expect(callback_called).to be true
ensure
DataDrip.before_backfill = original_callback
end

it "calls before_backfill before accessing scope" do
call_order = []
original_callback = DataDrip.before_backfill

DataDrip.before_backfill = -> { call_order << :before_backfill }

allow_any_instance_of(AddRoleToEmployee).to receive(:scope).and_wrap_original do |original_method|
call_order << :scope_accessed
original_method.call
end

backfill_run =
DataDrip::BackfillRun.new(
valid_attributes.merge(options: { age: 25 })
)
backfill_run.valid?

expect(call_order).to eq([:before_backfill, :scope_accessed])
ensure
DataDrip.before_backfill = original_callback
end
end

context "when scope has records" do
it "is valid" do
backfill_run =
Expand Down
Loading