Skip to content

Commit 5d18040

Browse files
committed
Use Arel instead of String for AR Enumerator conditionals
1 parent 4b310bd commit 5d18040

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

lib/job-iteration/active_record_cursor.rb

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,16 @@ def initialize
1818
end
1919
end
2020

21-
def initialize(relation, columns = nil, position = nil)
22-
@columns = if columns
23-
Array(columns)
24-
else
25-
Array(relation.primary_key).map { |pk| "#{relation.table_name}.#{pk}" }
26-
end
21+
def initialize(relation, columns, position = nil)
22+
@columns = columns
2723
self.position = Array.wrap(position)
2824
raise ArgumentError, "Must specify at least one column" if columns.empty?
29-
if relation.joins_values.present? && !@columns.all? { |column| column.to_s.include?(".") }
30-
raise ArgumentError, "You need to specify fully-qualified columns if you join a table"
31-
end
3225

3326
if relation.arel.orders.present? || relation.arel.taken.present?
3427
raise ConditionNotSupportedError
3528
end
3629

37-
@base_relation = relation.reorder(@columns.join(","))
30+
@base_relation = relation.reorder(*@columns)
3831
@reached_end = false
3932
end
4033

@@ -54,12 +47,10 @@ def position=(position)
5447

5548
def update_from_record(record)
5649
self.position = @columns.map do |column|
57-
method = column.to_s.split(".").last
58-
59-
if ActiveRecord.version >= Gem::Version.new("7.1.0.alpha") && method == "id"
50+
if ActiveRecord.version >= Gem::Version.new("7.1.0.alpha") && column.name == "id"
6051
record.id_value
6152
else
62-
record.send(method.to_sym)
53+
record.send(column.name)
6354
end
6455
end
6556
end
@@ -89,14 +80,14 @@ def conditions
8980
i = @position.size - 1
9081
column = @columns[i]
9182
conditions = if @columns.size == @position.size
92-
"#{column} > ?"
83+
column.gt(@position[i])
9384
else
94-
"#{column} >= ?"
85+
column.gteq(@position[i])
9586
end
9687
while i > 0
9788
i -= 1
9889
column = @columns[i]
99-
conditions = "#{column} > ? OR (#{column} = ? AND (#{conditions}))"
90+
conditions = column.gt(@position[i]).or(column.eq(@position[i]).and(conditions))
10091
end
10192
ret = @position.reduce([conditions]) { |params, value| params << value << value }
10293
ret.pop

lib/job-iteration/active_record_enumerator.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ def initialize(relation, columns: nil, batch_size: 100, cursor: nil)
1111
@relation = relation
1212
@batch_size = batch_size
1313
@columns = if columns
14-
Array(columns)
14+
Array(columns).map do |column|
15+
if column.is_a?(Arel::Attributes::Attribute)
16+
column
17+
else
18+
relation.arel_table[column.to_sym]
19+
end
20+
end
1521
else
16-
Array(relation.primary_key).map { |pk| "#{relation.table_name}.#{pk}" }
22+
Array(relation.primary_key).map { |pk| relation.arel_table[pk.to_sym] }
1723
end
1824
@cursor = cursor
1925
end
@@ -45,7 +51,7 @@ def size
4551

4652
def cursor_value(record)
4753
positions = @columns.map do |column|
48-
attribute_name = column.to_s.split(".").last
54+
attribute_name = column.name.to_sym
4955
column_value(record, attribute_name)
5056
end
5157
return positions.first if positions.size == 1
@@ -58,8 +64,8 @@ def finder_cursor
5864
end
5965

6066
def column_value(record, attribute)
61-
value = record.read_attribute(attribute.to_sym)
62-
case record.class.columns_hash.fetch(attribute).type
67+
value = record.read_attribute(attribute)
68+
case record.class.columns_hash.fetch(attribute.to_s).type
6369
when :datetime
6470
value.strftime(SQL_DATETIME_WITH_NSEC)
6571
else

0 commit comments

Comments
 (0)