Skip to content

Commit a335be2

Browse files
Merge pull request #431 from Shopify/ac-fix-cpk-enumerators
Support AR Enumerators for models with composite primary keys using :id
2 parents 1ebc697 + 9518cc9 commit a335be2

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

lib/job-iteration/active_record_cursor.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ def position=(position)
5555
def update_from_record(record)
5656
self.position = @columns.map do |column|
5757
method = column.to_s.split(".").last
58-
record.send(method.to_sym)
58+
59+
if ActiveRecord.version >= Gem::Version.new("7.1.0.alpha") && method == "id"
60+
record.id_value
61+
else
62+
record.send(method.to_sym)
63+
end
5964
end
6065
end
6166

test/test_helper.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class TravelRoute < ActiveRecord::Base
5353
self.primary_key = [:origin, :destination]
5454
end
5555

56-
class TravelRoute < ActiveRecord::Base
57-
self.primary_key = [:origin, :destination]
56+
class Order < ActiveRecord::Base
57+
self.primary_key = [:shop_id, :id]
5858
end
5959

6060
host = ENV["USING_DEV"] == "1" ? "job-iteration.railgun" : "localhost"
@@ -96,6 +96,11 @@ class TravelRoute < ActiveRecord::Base
9696
t.string(:destination)
9797
t.string(:origin)
9898
end
99+
100+
create_table(:orders, force: true) do |t|
101+
t.integer(:shop_id)
102+
t.string(:name)
103+
end
99104
end
100105

101106
module LoggingHelpers

test/unit/active_record_enumerator_test.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,33 @@ class ActiveRecordEnumeratorTest < IterationUnitTest
105105
assert_equal(10, enum.size)
106106
end
107107

108-
test "enumerator for a relation with a composite primary key" do
109-
TravelRoute.create!(origin: "A", destination: "B")
110-
TravelRoute.create!(origin: "A", destination: "C")
111-
TravelRoute.create!(origin: "B", destination: "A")
108+
if ActiveRecord.version >= Gem::Version.new("7.1.0.alpha")
109+
test "enumerator for a relation with a composite primary key" do
110+
TravelRoute.create!(origin: "A", destination: "B")
111+
TravelRoute.create!(origin: "A", destination: "C")
112+
TravelRoute.create!(origin: "B", destination: "A")
112113

113-
enum = build_enumerator(relation: TravelRoute.all, batch_size: 2)
114+
enum = build_enumerator(relation: TravelRoute.all, batch_size: 2)
114115

115-
cursors = []
116-
enum.records.each { |_record, cursor| cursors << cursor }
116+
cursors = []
117+
enum.records.each { |_record, cursor| cursors << cursor }
117118

118-
assert_equal([["A", "B"], ["A", "C"], ["B", "A"]], cursors)
119-
end if ActiveRecord.version >= Gem::Version.new("7.1.0.alpha")
119+
assert_equal([["A", "B"], ["A", "C"], ["B", "A"]], cursors)
120+
end
121+
122+
test "enumerator for a relation with a composite primary key using :id" do
123+
Order.create!(name: "Yellow socks", shop_id: 3)
124+
Order.create!(name: "Red hat", shop_id: 1)
125+
Order.create!(name: "Blue jeans", shop_id: 1)
126+
127+
enum = build_enumerator(relation: Order.all, batch_size: 2)
128+
129+
order_names = []
130+
enum.records.each { |record, _cursor| order_names << record.name }
131+
132+
assert_equal(["Red hat", "Blue jeans", "Yellow socks"], order_names)
133+
end
134+
end
120135

121136
private
122137

0 commit comments

Comments
 (0)