Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
クエリに発行元のコメントを含める処理の簡易版
# 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