Created
May 23, 2021 13:05
-
-
Save alpaca-tc/97b875d63e0e798537e0e3d5c234e0b5 to your computer and use it in GitHub Desktop.
クエリに発行元のコメントを含める処理の簡易版
This file contains 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
# 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