Skip to content

Commit 731c7bb

Browse files
committed
Fixes reidmorrison#121 Handle nil bound key
1 parent 618e094 commit 731c7bb

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

Gemfile

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ gem "minitest"
1111
gem "rake"
1212
gem "sprockets", "< 4.0"
1313

14-
gem "rails", "~> 5.0.0"
15-
gem "activerecord-jdbcsqlite3-adapter", "~> 50.0", platform: :jruby
16-
gem "sqlite3", "~> 1.3.0", platform: :ruby
17-
gem "jdbc-sqlite3", platform: :jruby
14+
gem "rails", "~> 6.1.0"
15+
gem "sqlite3", "~> 1.4.0", platform: :ruby
1816

1917
group :development do
2018
gem "rubocop"

lib/rails_semantic_logger/active_record/log_subscriber.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def sql(event)
5353

5454
# When multiple values are received for a single bound field, it is converted into an array
5555
def add_bind_value(binds, key, value)
56-
key = key.downcase.to_sym
56+
key = key.downcase.to_sym unless key.nil?
5757
value = (Array(binds[key]) << value) if binds.key?(key)
5858
binds[key] = value
5959
end
@@ -116,6 +116,16 @@ def bind_values_v5_1_5(payload)
116116
binds
117117
end
118118

119+
def bind_values_v6_1(payload)
120+
binds = {}
121+
casted_params = type_casted_binds(payload[:type_casted_binds])
122+
payload[:binds].each_with_index do |attr, i|
123+
attr_name, value = render_bind(attr, casted_params[i])
124+
add_bind_value(binds, attr_name, value)
125+
end
126+
binds
127+
end
128+
119129
def render_bind_v4_2(column, value)
120130
if column
121131
if column.binary?
@@ -155,6 +165,21 @@ def render_bind_v5_0_3(attr, value)
155165
[attr&.name, value]
156166
end
157167

168+
def render_bind_v6_1(attr, value)
169+
case attr
170+
when ActiveModel::Attribute
171+
if attr.type.binary? && attr.value
172+
value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
173+
end
174+
when Array
175+
attr = attr.first
176+
else
177+
attr = nil
178+
end
179+
180+
[attr&.name, value]
181+
end
182+
158183
def type_casted_binds_v5_0_3(binds, casted_binds)
159184
casted_binds || ::ActiveRecord::Base.connection.type_casted_binds(binds)
160185
end
@@ -172,6 +197,10 @@ def type_casted_binds_v5_1_5(casted_binds)
172197
alias bind_values bind_values_v5_0_3
173198
alias render_bind render_bind_v5_0_3
174199
alias type_casted_binds type_casted_binds_v5_0_3
200+
elsif Rails::VERSION::MAJOR == 6 && Rails::VERSION::MINOR > 0 # ~> 6.1.0
201+
alias bind_values bind_values_v6_1
202+
alias render_bind render_bind_v6_1
203+
alias type_casted_binds type_casted_binds_v5_1_5
175204
elsif Rails::VERSION::MAJOR >= 5 # ~> 5.1.5 && ~> 5.0.7 && 6.x.x
176205
alias bind_values bind_values_v5_1_5
177206
alias render_bind render_bind_v5_0_3

test/active_record_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,28 @@ class ActiveRecordTest < Minitest::Test
7676
assert_equal 1, binds[:limit], -> { actual.ai }
7777
end
7878
end
79+
80+
it "works with an IN clause" do
81+
Sample.where(age: [2,3]).first
82+
83+
SemanticLogger.flush
84+
actual = @mock_logger.message
85+
assert payload = actual[:payload], -> { actual.ai }
86+
assert payload[:sql], -> { actual.ai }
87+
88+
if Rails.version.to_f >= 5.0
89+
assert binds = payload[:binds], -> { actual.ai }
90+
if Rails.version.to_f >= 6.1
91+
# Rails 6.1 dropped the bound column name
92+
# Can be removed once this PR is fixed: https://github.com/rails/rails/pull/41068
93+
assert_equal [2, 3], binds[nil], -> { actual.ai }
94+
else
95+
assert_equal [2, 3], binds[:age], -> { actual.ai }
96+
end
97+
98+
assert_equal 1, binds[:limit], -> { actual.ai }
99+
end
100+
end
79101
end
80102
end
81103
end

0 commit comments

Comments
 (0)