Skip to content

Commit 1198efe

Browse files
committed
Add custom database role and poc to avoid comparing integer ids as strings
1 parent 922ee56 commit 1198efe

2 files changed

Lines changed: 24 additions & 11 deletions

File tree

lib/job-iteration/active_record_cursor.rb

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,37 @@ def position=(position)
5555
def update_from_record(record)
5656
self.position = @columns.map do |column|
5757
method = column.to_s.split(".").last
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
58+
record.send(method.to_sym)
6459
end
6560
end
6661

67-
def next_batch(batch_size)
62+
def next_batch(batch_size, database_role: nil)
6863
return if @reached_end
6964

7065
relation = @base_relation.limit(batch_size)
7166

7267
if (conditions = self.conditions).any?
73-
relation = relation.where(*conditions)
68+
column = @columns.first.split(".").first
69+
model = column.gsub(/_/, " ").split.map(&:capitalize).join("::").singularize.constantize
70+
71+
attribute = @columns.first.split(".").second.to_sym
72+
73+
relation = relation.where(model.arel_table[attribute].gt(@position.first))
74+
# model.id > id
75+
76+
# relation = relation.where(*conditions)
77+
# model.id > 'id'
78+
7479
end
7580

7681
records = relation.uncached do
77-
relation.to_a
82+
if database_role.present?
83+
ActiveRecord::Base.connected_to(role: database_role) do
84+
relation.to_a
85+
end
86+
else
87+
relation.to_a
88+
end
7889
end
7990

8091
update_from_record(records.last) unless records.empty?
@@ -93,6 +104,7 @@ def conditions
93104
else
94105
"#{column} >= ?"
95106
end
107+
96108
while i > 0
97109
i -= 1
98110
column = @columns[i]

lib/job-iteration/active_record_enumerator.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ module JobIteration
77
class ActiveRecordEnumerator
88
SQL_DATETIME_WITH_NSEC = "%Y-%m-%d %H:%M:%S.%N"
99

10-
def initialize(relation, columns: nil, batch_size: 100, cursor: nil)
10+
def initialize(relation, columns: nil, batch_size: 100, cursor: nil, database_role: nil)
1111
@relation = relation
1212
@batch_size = batch_size
13+
@database_role = database_role
1314
@columns = if columns
1415
Array(columns)
1516
else
@@ -31,7 +32,7 @@ def records
3132
def batches
3233
cursor = finder_cursor
3334
Enumerator.new(method(:size)) do |yielder|
34-
while (records = cursor.next_batch(@batch_size))
35+
while (records = cursor.next_batch(@batch_size, database_role: @database_role))
3536
yielder.yield(records, cursor_value(records.last)) if records.any?
3637
end
3738
end

0 commit comments

Comments
 (0)