Module: RailsPGExtras

Defined in:
lib/rails-pg-extras.rb,
lib/rails-pg-extras/version.rb,
lib/rails-pg-extras/index_info.rb,
lib/rails-pg-extras/table_info.rb,
lib/rails-pg-extras/diagnose_data.rb,
lib/rails-pg-extras/diagnose_print.rb,
lib/rails-pg-extras/index_info_print.rb,
lib/rails-pg-extras/table_info_print.rb

Defined Under Namespace

Classes: DiagnoseData, DiagnosePrint, IndexInfo, IndexInfoPrint, Railtie, TableInfo, TableInfoPrint

Constant Summary collapse

QUERIES =
RubyPGExtras::QUERIES
DEFAULT_ARGS =
RubyPGExtras::DEFAULT_ARGS
NEW_PG_STAT_STATEMENTS =
RubyPGExtras::NEW_PG_STAT_STATEMENTS
VERSION =
"3.2.6"

Class Method Summary collapse

Class Method Details

.connectionObject



93
94
95
# File 'lib/rails-pg-extras.rb', line 93

def self.connection
  ActiveRecord::Base.connection
end

.diagnose(in_format: :display_table) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rails-pg-extras.rb', line 53

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

.index_info(args: {}, in_format: :display_table) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rails-pg-extras.rb', line 65

def self.index_info(args: {}, in_format: :display_table)
  data = RailsPGExtras::IndexInfo.call(args[:table_name])

  if in_format == :display_table
    RailsPGExtras::IndexInfoPrint.call(data)
  elsif in_format == :hash
    data
  elsif in_format == :array
    data.map(&:values)
  else
    raise "Invalid 'in_format' argument!"
  end
end

.run_query(query_name:, in_format:, args: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rails-pg-extras.rb', line 27

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

.table_info(args: {}, in_format: :display_table) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails-pg-extras.rb', line 79

def self.table_info(args: {}, in_format: :display_table)
  data = RailsPGExtras::TableInfo.call(args[:table_name])

  if in_format == :display_table
    RailsPGExtras::TableInfoPrint.call(data)
  elsif in_format == :hash
    data
  elsif in_format == :array
    data.map(&:values)
  else
    raise "Invalid 'in_format' argument!"
  end
end