Sha256: 53b8198b1f610508670cc76a61d1707c0b5065537d1f884a899a1a1f66d8fd7e

Contents?: true

Size: 760 Bytes

Versions: 13

Compression:

Stored size: 760 Bytes

Contents

module PgHero
  module Methods
    module Explain
      def explain(sql)
        sql = squish(sql)
        explanation = nil

        # use transaction for safety
        with_transaction(statement_timeout: (explain_timeout_sec * 1000).round, rollback: true) do
          if (sql.sub(/;\z/, "").include?(";") || sql.upcase.include?("COMMIT")) && !explain_safe?
            raise ActiveRecord::StatementInvalid, "Unsafe statement"
          end
          explanation = select_all("EXPLAIN #{sql}").map { |v| v[:"QUERY PLAN"] }.join("\n")
        end

        explanation
      end

      private

      def explain_safe?
        select_all("SELECT 1; SELECT 1")
        false
      rescue ActiveRecord::StatementInvalid
        true
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
pghero-3.0.1 lib/pghero/methods/explain.rb
pghero-3.0.0 lib/pghero/methods/explain.rb
pghero-2.8.3 lib/pghero/methods/explain.rb
pghero-2.8.2 lib/pghero/methods/explain.rb
pghero-2.8.1 lib/pghero/methods/explain.rb
pghero-2.8.0 lib/pghero/methods/explain.rb
pghero-2.7.4 lib/pghero/methods/explain.rb
pghero_fork-2.7.3 lib/pghero/methods/explain.rb
pghero-2.7.3 lib/pghero/methods/explain.rb
pghero-2.7.2 lib/pghero/methods/explain.rb
pghero-2.7.1 lib/pghero/methods/explain.rb
pghero-2.7.0 lib/pghero/methods/explain.rb
pghero-2.6.0 lib/pghero/methods/explain.rb