Created
July 6, 2018 08:24
-
-
Save davidpdrsn/dd31f0a3f4723d1ab7a570a354b6243e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class QueryTracer | |
def disallow_queries | |
return yield unless Rails.env.test? | |
ActiveSupport::Notifications.unsubscribe("sql.active_record") | |
subscriber = ActiveSupport::Notifications.subscribe( | |
"sql.active_record" | |
) do |_name, _start, _finish, _id, payload| | |
if payload[:sql].match?(/select/i) && !payload[:sql].include?("pg_") | |
raise <<-EOS.strip_heredoc | |
Queries are disallowed currently by QueryTracer#disallow_queries" | |
Query: | |
#{payload[:sql]} | |
EOS | |
end | |
end | |
result = yield | |
ActiveSupport::Notifications.unsubscribe(subscriber) | |
result | |
end | |
end | |
# ----------------------- | |
users = User.some.set.of.scopes.includes(...).load | |
QueryTracer.new.disallow_queries do | |
users.each do |user| | |
some_method(user) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment