Sha256: 0eaa353326e28366c03ae1593cc388b51320d413e7485ec84680dc66093fd340

Contents?: true

Size: 1.08 KB

Versions: 63

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

require "active_support/notifications"
require "active_record/explain_registry"

module ActiveRecord
  class ExplainSubscriber # :nodoc:
    def start(name, id, payload)
      # unused
    end

    def finish(name, id, payload)
      if ExplainRegistry.collect? && !ignore_payload?(payload)
        ExplainRegistry.queries << payload.values_at(:sql, :binds)
      end
    end

    # SCHEMA queries cannot be EXPLAINed, also we do not want to run EXPLAIN on
    # our own EXPLAINs no matter how loopingly beautiful that would be.
    #
    # On the other hand, we want to monitor the performance of our real database
    # queries, not the performance of the access to the query cache.
    IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN)
    EXPLAINED_SQLS = /\A\s*(with|select|update|delete|insert)\b/i
    def ignore_payload?(payload)
      payload[:exception] ||
        payload[:cached] ||
        IGNORED_PAYLOADS.include?(payload[:name]) ||
        !payload[:sql].match?(EXPLAINED_SQLS)
    end

    ActiveSupport::Notifications.subscribe("sql.active_record", new)
  end
end

Version data entries

63 entries across 59 versions & 5 rubygems

Version Path
activerecord-6.1.7.10 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.9 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.8 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.7 lib/active_record/explain_subscriber.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/explain_subscriber.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/explain_subscriber.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/explain_subscriber.rb
activerecord-6.1.7.6 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.5 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.4 lib/active_record/explain_subscriber.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.2.3/lib/active_record/explain_subscriber.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-7.0.3.1/lib/active_record/explain_subscriber.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/explain_subscriber.rb
rubypitaya-3.12.5 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/explain_subscriber.rb
activerecord-7.0.4.3 lib/active_record/explain_subscriber.rb
activerecord-6.1.7.3 lib/active_record/explain_subscriber.rb
rubypitaya-3.12.4 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/explain_subscriber.rb
activerecord-6.1.7.2 lib/active_record/explain_subscriber.rb
activerecord-7.0.4.2 lib/active_record/explain_subscriber.rb
rubypitaya-3.12.3 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activerecord-7.0.4/lib/active_record/explain_subscriber.rb