Sha256: cf941cffc49d28ea5dbd5f461cbca02a1cf4bb46f226d60c27b50395d3f44d9d
Contents?: true
Size: 1.87 KB
Versions: 2
Compression:
Stored size: 1.87 KB
Contents
# frozen_string_literal: true require 'terminal-table' require 'ruby-pg-extras' require 'rails-pg-extras/diagnose_data' require 'rails-pg-extras/diagnose_print' module RailsPGExtras QUERIES = RubyPGExtras::QUERIES DEFAULT_ARGS = RubyPGExtras::DEFAULT_ARGS NEW_PG_STAT_STATEMENTS = RubyPGExtras::NEW_PG_STAT_STATEMENTS QUERIES.each do |query_name| define_singleton_method query_name do |options = {}| run_query( query_name: query_name, in_format: options.fetch(:in_format, :display_table), args: options.fetch(:args, {}) ) end end def self.run_query(query_name:, in_format:, args: {}) if %i(calls outliers).include?(query_name) pg_stat_statements_ver = RailsPGExtras.connection.execute("select installed_version from pg_available_extensions where name='pg_stat_statements'") .to_a[0].fetch("installed_version", nil) if pg_stat_statements_ver != nil if Gem::Version.new(pg_stat_statements_ver) < Gem::Version.new(NEW_PG_STAT_STATEMENTS) query_name = "#{query_name}_legacy".to_sym end end end sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {} RubyPGExtras.sql_for(query_name: query_name) % custom_args else RubyPGExtras.sql_for(query_name: query_name) end result = connection.execute(sql) RubyPGExtras.display_result( result, title: RubyPGExtras.description_for(query_name: query_name), in_format: in_format ) end def self.diagnose(in_format: :display_table) data = RailsPGExtras::DiagnoseData.call if in_format == :display_table RailsPGExtras::DiagnosePrint.call(data) elsif in_format == :hash data else raise "Invalid 'in_format' argument!" end end def self.connection ActiveRecord::Base.connection end end require 'rails-pg-extras/railtie' if defined?(Rails)
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
rails-pg-extras-3.1.0 | lib/rails-pg-extras.rb |
rails-pg-extras-3.0.6 | lib/rails-pg-extras.rb |