Skip to content

Commit

Permalink
Fix dataset ordering for complex PK (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon authored Aug 9, 2022
1 parent 603b516 commit ef74381
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sequel-batches (2.0.0)
sequel-batches (2.0.1)
sequel

GEM
Expand All @@ -25,7 +25,7 @@ GEM
method_source (1.0.0)
minitest (5.16.2)
parallel (1.22.1)
parser (3.1.2.0)
parser (3.1.2.1)
ast (~> 2.4.1)
pg (1.4.2)
pry (0.14.1)
Expand Down Expand Up @@ -58,7 +58,7 @@ GEM
rubocop-ast (>= 1.18.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.19.1)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-config-umbrellio (1.30.0.65)
rubocop (~> 1.30.0)
Expand Down
2 changes: 1 addition & 1 deletion lib/sequel/extensions/batches/yielder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call
base_ds
end

working_ds_pk = working_ds.select(*qualified_pk).order(order_by).limit(of)
working_ds_pk = working_ds.select(*qualified_pk).order(order_by(qualified: true)).limit(of)
current_instance = db.from(working_ds_pk).select(*pk).order(order_by).last or break
working_ds = working_ds.where(generate_conditions(current_instance.to_h, sign: sign_to_inclusive))

Expand Down
2 changes: 1 addition & 1 deletion sequel-batches.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "sequel-batches"
spec.version = "2.0.0"
spec.version = "2.0.1"
spec.authors = %w[fiscal-cliff umbrellio]
spec.email = ["[email protected]"]
spec.required_ruby_version = ">= 2.7"
Expand Down
6 changes: 6 additions & 0 deletions spec/sequel/extensions/batches_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@
expect { DB[:data, :data2].in_batches }.not_to raise_error
end

it "qualifies pk to mitigate ambiguous column error in case of composite pk" do
scope = DB[:data].join(:data2, Sequel[:data][:id] =~ Sequel[:data2][:id]).order(Sequel[:data][:id])
scope.in_batches(pk: [:created_at, :id], of: 2) { |b| chunks << b.select_map(Sequel[:data][:id]) }
expect(chunks).to eq([[1, 2], [3, 4], [5, 6]])
end

it "respects order option" do
DB[:data].in_batches(of: 3, order: :desc) { |b| chunks << b.select_map(:id) }
expect(chunks).to eq([[6, 5, 4], [3, 2, 1]])
Expand Down

0 comments on commit ef74381

Please sign in to comment.