Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created May 23, 2021 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpaca-tc/97b875d63e0e798537e0e3d5c234e0b5 to your computer and use it in GitHub Desktop.
Save alpaca-tc/97b875d63e0e798537e0e3d5c234e0b5 to your computer and use it in GitHub Desktop.
クエリに発行元のコメントを含める処理の簡易版
# frozen_string_literal: true
dowsing = Module.new do
def execute(sql, *)
source_location = query_source_location(caller.lazy)
sql = "#{sql} /* #{source_location} */" if source_location
super
end
private
def query_source_location(locations)
source_location = ActiveRecord::LogSubscriber.backtrace_cleaner.clean(locations).first
source_location&.remove!('*')
source_location
end
end
ActiveSupport.on_load(:active_record) do
ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(dowsing)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment