Class: Debugbar::ActiveRecordEventSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/debugbar/subscribers/active_record.rb

Class Method Summary collapse

Class Method Details

.query_source_locationObject



43
44
45
46
# File 'lib/debugbar/subscribers/active_record.rb', line 43

def query_source_location
  str = backtrace_cleaner.clean(caller(3))[0]
  str&.split(":in")&.map { |s| s.delete_prefix("#{Rails.root}/").strip.tr("`'", '' ) } # Duplicated in Debugbar.msg
end

.sql(event) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/debugbar/subscribers/active_record.rb', line 8

def sql(event)
  return if Debugbar::Current.ignore?

  payload = event.payload

  return if payload[:name]&.starts_with? "SCHEMA"

  title = if payload[:async]
    "ASYNC #{payload[:name]} (#{payload[:lock_wait].round(1)}ms) (db time #{event.duration.round(1)}ms)"
  else
    "#{payload[:name] || "Unnamed"} (#{event.duration.round(1)}ms)"
  end
  title = "CACHE #{title}" if payload[:cached]

  sql = payload[:sql]&.gsub(/\/\*.*\*\//, "") # remove comments

  binds = nil
  # if payload[:binds]&.any?
  #   TODO: Restore binds when I can figure out how to get something in poayload[:binds]
  # end

  Debugbar::Tracker.add_query({
    id: event.transaction_id,
    title: title,
    name: payload[:name],
    sql: sql,
    cached: payload[:cached].present?,
    async: payload[:async],
    duration: event.duration.round(1),
    lock_wait: payload[:lock_wait]&.round(1),
    binds: binds,
    source: query_source_location
  })
end