Skip to content

Commit c870ecb

Browse files
Allow safe sql values to avoid dangerous query method error (#1)
Support plucking custom Arel columns --------- Co-authored-by: fatkodima <[email protected]>
1 parent c0209d9 commit c870ecb

File tree

5 files changed

+24
-3
lines changed

5 files changed

+24
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## master (unreleased)
22

3+
- Support plucking custom Arel columns
4+
35
## 0.2.0 (2023-07-24)
46

57
- Support specifying per cursor column ordering when batching

lib/pluck_in_batches/iterator.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ def each_batch(*columns, start: nil, finish: nil, batch_size: 1000, error_on_ign
3535
raise ArgumentError, ":order must be :asc or :desc or an array consisting of :asc or :desc, got #{order.inspect}"
3636
end
3737

38-
pluck_columns = columns.map(&:to_s)
38+
pluck_columns = columns.map do |column|
39+
if Arel.arel_node?(column)
40+
column
41+
else
42+
column.to_s
43+
end
44+
end
45+
3946
cursor_columns = Array(cursor_column).map(&:to_s)
4047
cursor_column_indexes = cursor_column_indexes(pluck_columns, cursor_columns)
4148
missing_cursor_columns = cursor_column_indexes.count(&:nil?)

test/pluck_in_batches_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def test_pluck_in_batches_should_return_batches
4848
end
4949
end
5050

51+
def test_pluck_in_batches_should_return_batches_for_arel_columns
52+
ids_and_ranks = User.order(:id).pluck(:id, Arel.sql("json_extract(users.metadata, '$.rank')"))
53+
54+
assert_queries(User.count + 1) do
55+
User.pluck_in_batches(:id, Arel.sql("json_extract(users.metadata, '$.rank')"), batch_size: 1).with_index do |batch, index|
56+
assert_kind_of Array, batch
57+
assert_equal ids_and_ranks[index], batch.first
58+
end
59+
end
60+
end
61+
5162
def pluck_in_batches_desc_order
5263
ids = User.order(id: :desc).ids
5364
assert_queries(User.count + 1) do

test/support/schema.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
ActiveRecord::Schema.define do
44
create_table :users, force: true do |t|
55
t.string :name
6+
t.json :metadata, null: false, default: {}
67
end
78

89
create_table :subscribers, id: false, primary_key: :nick, force: true do |t|

test/test_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def prepare_database
2323
# Create users
2424
values = 20.times.map do |i|
2525
id = i + 1
26-
"(#{id}, 'User-#{id}')"
26+
"(#{id}, 'User-#{id}', json('{\"rank\": #{i}}'))"
2727
end.join(", ")
28-
ActiveRecord::Base.connection.execute("INSERT INTO users (id, name) VALUES #{values}")
28+
ActiveRecord::Base.connection.execute("INSERT INTO users (id, name, metadata) VALUES #{values}")
2929

3030
# Create subscribers
3131
values = 10.times.map do |i|

0 commit comments

Comments
 (0)