Module: RailsPgExtras
- Defined in:
- lib/rails-pg-extras.rb,
lib/rails_pg_extras/web.rb,
lib/rails_pg_extras/mcp_app.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/configuration.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,
lib/rails_pg_extras/missing_fk_indexes.rb,
lib/rails_pg_extras/missing_fk_constraints.rb
Defined Under Namespace
Modules: Web
Classes: Configuration, DiagnoseData, DiagnosePrint, IndexInfo, IndexInfoPrint, MCPApp, MissingFkConstraints, MissingFkIndexes, Railtie, TableInfo, TableInfoPrint
Constant Summary
collapse
- QUERIES =
::QUERIES
- DEFAULT_ARGS =
::DEFAULT_ARGS
- NEW_PG_STAT_STATEMENTS =
::NEW_PG_STAT_STATEMENTS
- PG_STAT_STATEMENTS_17 =
::PG_STAT_STATEMENTS_17
- VERSION =
"5.6.14"
- @@database_url =
nil
Class Method Summary
collapse
Class Method Details
.configuration ⇒ Object
22
23
24
|
# File 'lib/rails_pg_extras/configuration.rb', line 22
def self.configuration
@configuration ||= Configuration.new(Configuration::DEFAULT_CONFIG)
end
|
26
27
28
|
# File 'lib/rails_pg_extras/configuration.rb', line 26
def self.configure
yield(configuration)
end
|
.connection ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/rails-pg-extras.rb', line 150
def self.connection
selected_db_key = Thread.current[:rails_pg_extras_db_key]
db_url = @@database_url || ENV["RAILS_PG_EXTRAS_DATABASE_URL"]
if selected_db_key.present?
const_name = selected_db_key.classify
thread_classes = (Thread.current[:rails_pg_extras_ar_classes] ||= {})
ar_class = (thread_classes[selected_db_key] ||= begin
if const_defined?(const_name, false)
const_get(const_name, false)
else
klass = Class.new(ActiveRecord::Base)
klass.abstract_class = true
const_set(const_name, klass)
end
end)
connector = ar_class.establish_connection(selected_db_key.to_sym)
if connector.respond_to?(:connection)
connector.connection
elsif connector.respond_to?(:lease_connection)
connector.lease_connection
else
raise "Unsupported connector: #{connector.class}"
end
elsif db_url.present?
connector = ActiveRecord::Base.establish_connection(db_url)
if connector.respond_to?(:connection)
connector.connection
elsif connector.respond_to?(:lease_connection)
connector.lease_connection
else
raise "Unsupported connector: #{connector.class}"
end
else
ActiveRecord::Base.connection
end
end
|
.database_url=(value) ⇒ Object
146
147
148
|
# File 'lib/rails-pg-extras.rb', line 146
def self.database_url=(value)
@@database_url = value
end
|
.diagnose(in_format: :display_table) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
|
# File 'lib/rails-pg-extras.rb', line 41
def self.diagnose(in_format: :display_table)
data = ::DiagnoseData.call
if in_format == :display_table
::DiagnosePrint.call(data)
elsif in_format == :hash
data
else
raise "Invalid 'in_format' argument!"
end
end
|
.index_info(args: {}, in_format: :display_table) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/rails-pg-extras.rb', line 108
def self.index_info(args: {}, in_format: :display_table)
data = ::IndexInfo.call(args[:table_name])
if in_format == :display_table
::IndexInfoPrint.call(data)
elsif in_format == :hash
data
elsif in_format == :array
data.map(&:values)
else
raise "Invalid 'in_format' argument!"
end
end
|
.measure_duration(&block) ⇒ Object
53
54
55
56
57
58
|
# File 'lib/rails-pg-extras.rb', line 53
def self.measure_duration(&block)
starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
block.call
ending = Process.clock_gettime(Process::CLOCK_MONOTONIC)
(ending - starting) * 1000
end
|
.measure_queries(&block) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/rails-pg-extras.rb', line 60
def self.measure_queries(&block)
queries = {}
sql_duration = 0
method_name = if ActiveSupport::Notifications.respond_to?(:monotonic_subscribe)
:monotonic_subscribe
else
:subscribe
end
subscriber = ActiveSupport::Notifications.public_send(method_name, "sql.active_record") do |_name, start, finish, _id, payload|
unless payload[:name] =~ /SCHEMA/
key = payload[:sql]
queries[key] ||= { count: 0, total_duration: 0, min_duration: nil, max_duration: nil }
queries[key][:count] += 1
duration = (finish - start) * 1000
queries[key][:total_duration] += duration
sql_duration += duration
if queries[key][:min_duration] == nil || queries[key][:min_duration] > duration
queries[key][:min_duration] = duration.round(2)
end
if queries[key][:max_duration] == nil || queries[key][:max_duration] < duration
queries[key][:max_duration] = duration.round(2)
end
end
end
total_duration = measure_duration do
block.call
end
queries = queries.reduce({}) do |agg, val|
val[1][:avg_duration] = (val[1][:total_duration] / val[1][:count]).round(2)
val[1][:total_duration] = val[1][:total_duration].round(2)
agg.merge(val[0] => val[1])
end
ActiveSupport::Notifications.unsubscribe(subscriber)
{
count: queries.reduce(0) { |agg, val| agg + val[1].fetch(:count) },
queries: queries,
total_duration: total_duration.round(2),
sql_duration: sql_duration.round(2),
}
end
|
.missing_fk_constraints(args: {}, in_format: :display_table) ⇒ Object
141
142
143
144
|
# File 'lib/rails-pg-extras.rb', line 141
def self.missing_fk_constraints(args: {}, in_format: :display_table)
result = ::MissingFkConstraints.call(args[:table_name])
.display_result(result, title: "Missing foreign key constraints", in_format: in_format)
end
|
.missing_fk_indexes(args: {}, in_format: :display_table) ⇒ Object
136
137
138
139
|
# File 'lib/rails-pg-extras.rb', line 136
def self.missing_fk_indexes(args: {}, in_format: :display_table)
result = ::MissingFkIndexes.call(args[:table_name])
.display_result(result, title: "Missing foreign key indexes", in_format: in_format)
end
|
.run_query(query_name:, in_format:, args: {}) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/rails-pg-extras.rb', line 31
def self.run_query(query_name:, in_format:, args: {})
.run_query_base(
query_name: query_name,
conn: connection,
exec_method: :execute,
in_format: in_format,
args: args,
)
end
|
.table_info(args: {}, in_format: :display_table) ⇒ Object
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rails-pg-extras.rb', line 122
def self.table_info(args: {}, in_format: :display_table)
data = ::TableInfo.call(args[:table_name])
if in_format == :display_table
::TableInfoPrint.call(data)
elsif in_format == :hash
data
elsif in_format == :array
data.map(&:values)
else
raise "Invalid 'in_format' argument!"
end
end
|