debian-mirror-gitlab/spec/support/helpers/query_recorder.rb

44 lines
1 KiB
Ruby
Raw Normal View History

2018-10-15 14:42:47 +05:30
module ActiveRecord
class QueryRecorder
2018-11-08 19:23:39 +05:30
attr_reader :log, :skip_cached, :cached
2018-10-15 14:42:47 +05:30
2018-11-08 19:23:39 +05:30
def initialize(skip_cached: true, &block)
2018-10-15 14:42:47 +05:30
@log = []
@cached = []
2018-11-08 19:23:39 +05:30
@skip_cached = skip_cached
2018-10-15 14:42:47 +05:30
ActiveSupport::Notifications.subscribed(method(:callback), 'sql.active_record', &block)
end
def show_backtrace(values)
Rails.logger.debug("QueryRecorder SQL: #{values[:sql]}")
2019-09-04 21:01:54 +05:30
Gitlab::Profiler.clean_backtrace(caller).each { |line| Rails.logger.debug(" --> #{line}") }
2018-10-15 14:42:47 +05:30
end
def callback(name, start, finish, message_id, values)
show_backtrace(values) if ENV['QUERY_RECORDER_DEBUG']
2019-07-31 22:56:46 +05:30
if values[:cached] && skip_cached
2018-10-15 14:42:47 +05:30
@cached << values[:sql]
elsif !values[:name]&.include?("SCHEMA")
@log << values[:sql]
end
end
def count
@log.count
end
def cached_count
@cached.count
end
def log_message
@log.join("\n\n")
end
2019-09-30 21:07:59 +05:30
def occurrences
@log.group_by(&:to_s).transform_values(&:count)
end
2018-10-15 14:42:47 +05:30
end
end