Module: RubyPGExtras
- Defined in:
- lib/ruby-pg-extras.rb,
lib/ruby-pg-extras/version.rb
Constant Summary collapse
- QUERIES =
%i( bloat blocking cache_hit calls extensions table_cache_hit index_cache_hit index_size index_usage null_indexes locks all_locks long_running_queries mandelbrot outliers records_rank seq_scans table_indexes_size table_size total_index_size total_table_size unused_indexes vacuum_stats kill_all )
- DEFAULT_ARGS =
Hash.new({}).merge({ calls: { limit: 10 }, long_running_queries: { threshold: "500 milliseconds" }, outliers: { limit: 10 }, unused_indexes: { min_scans: 50 } })
- VERSION =
"1.5.0"- @@database_url =
nil
Class Method Summary collapse
- .connection ⇒ Object
- .database_url ⇒ Object
- .database_url=(value) ⇒ Object
- .description_for(query_name:) ⇒ Object
- .display_result(result, title:, in_format:) ⇒ Object
- .run_query(query_name:, in_format:, args: {}) ⇒ Object
- .sql_for(query_name:) ⇒ Object
- .sql_path_for(query_name:) ⇒ Object
Class Method Details
.connection ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ruby-pg-extras.rb', line 95 def self.connection database_uri = URI.parse(database_url) @_connection ||= PG.connect( dbname: database_uri.path[1..-1], host: database_uri.host, port: database_uri.port, user: database_uri.user, password: database_uri.password ) end |
.database_url ⇒ Object
111 112 113 |
# File 'lib/ruby-pg-extras.rb', line 111 def self.database_url @@database_url || ENV.fetch("DATABASE_URL") end |
.database_url=(value) ⇒ Object
107 108 109 |
# File 'lib/ruby-pg-extras.rb', line 107 def self.database_url=(value) @@database_url = value end |
.description_for(query_name:) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/ruby-pg-extras.rb', line 77 def self.description_for(query_name:) first_line = File.open( sql_path_for(query_name: query_name) ) { |f| f.readline } first_line[/\/\*(.*?)\*\//m, 1].strip end |
.display_result(result, title:, in_format:) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ruby-pg-extras.rb', line 52 def self.display_result(result, title:, in_format:) case in_format when :array result.values when :hash result.to_a when :raw result when :display_table headings = if result.count > 0 result[0].keys else ["No results"] end puts Terminal::Table.new( title: title, headings: headings, rows: result.values ) else raise "Invalid in_format option" end end |
.run_query(query_name:, in_format:, args: {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ruby-pg-extras.rb', line 37 def self.run_query(query_name:, in_format:, args: {}) sql = if (custom_args = DEFAULT_ARGS[query_name].merge(args)) != {} sql_for(query_name: query_name) % custom_args else sql_for(query_name: query_name) end result = connection.exec(sql) display_result( result, title: description_for(query_name: query_name), in_format: in_format ) end |
.sql_for(query_name:) ⇒ Object
85 86 87 88 89 |
# File 'lib/ruby-pg-extras.rb', line 85 def self.sql_for(query_name:) File.read( sql_path_for(query_name: query_name) ) end |
.sql_path_for(query_name:) ⇒ Object
91 92 93 |
# File 'lib/ruby-pg-extras.rb', line 91 def self.sql_path_for(query_name:) File.join(File.dirname(__FILE__), "/ruby-pg-extras/queries/#{query_name}.sql") end |