Skip to content

Commit

Permalink
select_all's cast_values option is careful with AR>=6.1 codepath
Browse files Browse the repository at this point in the history
  • Loading branch information
pda committed Jul 13, 2023
1 parent ca8b828 commit f1c9bbd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/pghero/methods/basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ def select_all(sql, conn: nil, query_columns: [], cast_values: false)
# squish for logs
retries = 0
begin
result = conn.select_all(add_source(squish(sql)))
result = result.cast_values.map { |row| result.columns.zip(row).to_h } if cast_values
result = uncast_result = conn.select_all(add_source(squish(sql)))
if cast_values
# ActiveRecord::Result#cast_values turns PostgreSQL arrays into Ruby arrays, etc.
# But it turns ActiveRecord::Result into Array of results, which is why we keep
# an `uncast_result` copy of `result`.
result = result.cast_values.map { |row| result.columns.zip(row).to_h }
end
if ActiveRecord::VERSION::STRING.to_f >= 6.1
result = result.map(&:symbolize_keys)
else
result = result.map { |row| row.to_h { |col, val| [col.to_sym, result.column_types[col].send(:cast_value, val)] } }
result = result.map { |row| row.to_h { |col, val| [col.to_sym, uncast_result.column_types[col].send(:cast_value, val)] } }
end
if filter_data
query_columns.each do |column|
Expand Down

0 comments on commit f1c9bbd

Please sign in to comment.