Skip to content

Commit e163b03

Browse files
authored
Merge pull request reidmorrison#92 from alachaum/rails6-activerecord-logger
Fix logging of SQL queries when query cache is in use
2 parents f5f56e4 + fb32c94 commit e163b03

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/rails_semantic_logger/active_record/log_subscriber.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ def type_casted_binds_v5_1_5(casted_binds)
165165
if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR.zero? && Rails::VERSION::TINY <= 2 # 5.0.0 - 5.0.2
166166
alias bind_values bind_values_v5_0_0
167167
alias render_bind render_bind_v5_0_0
168-
elsif Rails::VERSION::MAJOR >= 5 &&
168+
elsif Rails::VERSION::MAJOR == 5 &&
169169
((Rails::VERSION::MINOR.zero? && Rails::VERSION::TINY <= 6) ||
170170
(Rails::VERSION::MINOR == 1 && Rails::VERSION::TINY <= 4)) # 5.0.3 - 5.0.6 && 5.1.0 - 5.1.4
171171
alias bind_values bind_values_v5_0_3
172172
alias render_bind render_bind_v5_0_3
173173
alias type_casted_binds type_casted_binds_v5_0_3
174-
elsif Rails::VERSION::MAJOR >= 5 # ~> 5.1.5 && ~> 5.0.7
174+
elsif Rails::VERSION::MAJOR >= 5 # ~> 5.1.5 && ~> 5.0.7 && 6.x.x
175175
alias bind_values bind_values_v5_1_5
176176
alias render_bind render_bind_v5_0_3
177177
alias type_casted_binds type_casted_binds_v5_1_5

test/active_record_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ class ActiveRecordTest < Minitest::Test
3030
assert actual[:payload][:sql], actual[:payload]
3131
end
3232

33+
it 'sql with query cache' do
34+
Sample.cache { 2.times { Sample.where(name: 'foo').first } }
35+
36+
SemanticLogger.flush
37+
actual = @mock_logger.message
38+
39+
if Rails.version.to_f >= 5.1
40+
assert actual[:message].include?('Sample'), actual[:message]
41+
else
42+
assert actual[:message].include?('CACHE'), actual[:message]
43+
end
44+
45+
assert actual[:payload], actual
46+
assert actual[:payload][:sql], actual[:payload]
47+
end
48+
3349
it 'single bind value' do
3450
Sample.where(name: 'Jack').first
3551

0 commit comments

Comments
 (0)