Sha256: 061dccf74a307097d46fcd6eb9d03e285c5c5871fb4d88889ce77d8a6131efee

Contents?: true

Size: 1.07 KB

Versions: 72

Compression:

Stored size: 1.07 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] !~ EXPLAINED_SQLS
    end

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

Version data entries

72 entries across 72 versions & 6 rubygems

Version Path
activerecord-5.2.4.1 lib/active_record/explain_subscriber.rb
activerecord-6.0.2 lib/active_record/explain_subscriber.rb
activerecord-6.0.2.rc2 lib/active_record/explain_subscriber.rb
activerecord-5.2.4 lib/active_record/explain_subscriber.rb
activerecord-6.0.2.rc1 lib/active_record/explain_subscriber.rb
activerecord-5.2.4.rc1 lib/active_record/explain_subscriber.rb
activerecord-6.0.1 lib/active_record/explain_subscriber.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/explain_subscriber.rb
activerecord-6.0.1.rc1 lib/active_record/explain_subscriber.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activerecord-6.0.0/lib/active_record/explain_subscriber.rb
activerecord-6.0.0 lib/active_record/explain_subscriber.rb
spiral_form-0.1.1 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/explain_subscriber.rb
spiral_form-0.1.0 vendor/bundle/gems/activerecord-5.2.3/lib/active_record/explain_subscriber.rb
activerecord-6.0.0.rc2 lib/active_record/explain_subscriber.rb
activerecord-6.0.0.rc1 lib/active_record/explain_subscriber.rb
activerecord-5.2.3 lib/active_record/explain_subscriber.rb
activerecord-5.2.3.rc1 lib/active_record/explain_subscriber.rb
activerecord-6.0.0.beta3 lib/active_record/explain_subscriber.rb
activerecord-5.2.2.1 lib/active_record/explain_subscriber.rb
activerecord-6.0.0.beta2 lib/active_record/explain_subscriber.rb