Skip to content

Commit

Permalink
fix: pass block argument in ActiveRecord find_by_sql patch (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
olepbr authored Nov 21, 2024
1 parent bf6394d commit 16bef85
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class << base
module ClassMethods
method_name = ::ActiveRecord.version >= Gem::Version.new('7.0.0') ? :_query_by_sql : :find_by_sql

define_method(method_name) do |*args, **kwargs|
define_method(method_name) do |*args, **kwargs, &block|
tracer.in_span("#{self} query") do
super(*args, **kwargs)
super(*args, **kwargs, &block)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
let(:spans) { exporter.finished_spans }

before { exporter.reset }
after do
ActiveRecord::Base.subclasses.each do |model|
model.connection.truncate(model.table_name)
end
end

describe 'query' do
it 'traces' do
Expand All @@ -28,5 +33,79 @@
_(user_find_spans.length).must_equal(2)
_(account_find_span).wont_be_nil
end

describe 'find_by_sql' do
it 'creates a span' do
Account.create!

Account.find_by_sql('SELECT * FROM accounts')

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end

describe 'given a block' do
it 'creates a span' do
account = Account.create!

record_ids = []

Account.find_by_sql('SELECT * FROM accounts') do |record|
record_ids << record.id
end

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty

_(record_ids).must_equal([account.id])
end
end
end

describe 'find_by' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

Account.find_by(id: account.id)

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end
end

describe 'find' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

Account.find(account.id)

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
end

describe 'given a block' do
it 'creates a span' do
account = Account.create!
User.create!(account: account)

record_ids = []

Account.find(account.id) do |record|
record_ids << record.id
end

account_find_span = spans.find { |s| s.name == 'Account query' }
_(account_find_span).wont_be_nil
_(account_find_span.attributes).must_be_empty
_(record_ids).must_equal([account.id])
end
end
end
end
end

0 comments on commit 16bef85

Please sign in to comment.