rails-pg-extras 4.0.1 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 111d02ff1b373270351607d9adde9475dfde67e687583eb76ce8b51664e330f0
4
- data.tar.gz: bd0a8c6dab34c2200ca97325487076dc1f6945f347b1b545db5f268990ab5e5a
3
+ metadata.gz: 0a0259a817155746ae3dda5a13fd1e6c8e0421dad029dd2c6100222379dad76f
4
+ data.tar.gz: 5ffdc7d923d458bab991439a65adf91ff3e752e8e90280c6e3faae57d36762a7
5
5
  SHA512:
6
- metadata.gz: 115828ab093b55f55a688d6414750f5e0ef62caaed85a731b5dd2d19e88c142d910a6fa316073a0df825571c04a440b8a9a168d9a1ded6a3b9ae3e254571210f
7
- data.tar.gz: 8b119ef84bf4e01a1b4b2cc51eba7cf63cdcb6cc8d9254aacd5a39813d18337e32f6b0b56b0f595afb847b33a90e2e9996a631a7094f3b5b75a13acc5bf2fd19
6
+ metadata.gz: a6a55fcb7e8cc126859f1f45f4ba38f9562f1bded1e4aa6287e3728926cc3eb598552d4cdcc311104e811b513f20a71f52d29964dd6f0137df68a2699e9ef7ab
7
+ data.tar.gz: 305dc8c57cace4845795b051afca373103765181a1121203d9963261a0be18b50d6335355a60988be745d16d19ecd9f30ac506f4cff15c5265c38cdca039316a
data/README.md CHANGED
@@ -10,7 +10,7 @@ You can read this blog post for detailed step by step tutorial on how to [optimi
10
10
 
11
11
  Optionally you can enable a visual interface:
12
12
 
13
- ![Web interface](https://github.com/pawurb/rails-pg-extras/raw/master/rails-pg-extras-web.png)
13
+ ![Web interface](https://github.com/pawurb/rails-pg-extras/raw/master/pg-extras-ui.png)
14
14
 
15
15
  Alternative versions:
16
16
 
@@ -29,7 +29,7 @@ Alternative versions:
29
29
  In your Gemfile
30
30
 
31
31
  ```ruby
32
- gem "rails-pg-extras", require: "rails_pg_extras"
32
+ gem "rails-pg-extras"
33
33
  ```
34
34
 
35
35
  `calls` and `outliers` queries require [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) extension.
@@ -119,7 +119,18 @@ You can enable UI using a Rails engine by adding the following code in `config/r
119
119
  mount RailsPgExtras::Web::Engine, at: 'pg_extras'
120
120
  ```
121
121
 
122
- On production environment you can enable HTTP basic auth by specifying `RAILS_PG_EXTRAS_USER` and `RAILS_PG_EXTRAS_PASSWORD` variables.
122
+ You can enable HTTP basic auth by specifying `RAILS_PG_EXTRAS_USER` and `RAILS_PG_EXTRAS_PASSWORD` variables.
123
+
124
+ You can configure available web actions in `config/initializers/rails_pg_extras.rb`:
125
+
126
+ ```ruby
127
+ RailsPgExtras.configure do |config|
128
+ # Rails-pg-extras does not enable all the web actions by default. You can check all available actions via `RailsPgExtras::Web::ACTIONS`.
129
+ # For example, you may want to enable the dangerous `kill_all` action.
130
+
131
+ config.enabled_web_actions = %i[kill_all pg_stat_statements_reset enable_extensions]
132
+ end
133
+ ```
123
134
 
124
135
  ## Available methods
125
136
 
@@ -1,5 +1,7 @@
1
1
  module RailsPgExtras::Web
2
2
  class ActionsController < RailsPgExtras::Web::ApplicationController
3
+ before_action :validate_action!
4
+
3
5
  def kill_all
4
6
  run(:kill_all)
5
7
  end
@@ -14,6 +16,12 @@ module RailsPgExtras::Web
14
16
 
15
17
  private
16
18
 
19
+ def validate_action!
20
+ unless RailsPgExtras::Web.action_enabled?(action_name)
21
+ render plain: "Action '#{action_name}' is not enabled!", status: :forbidden
22
+ end
23
+ end
24
+
17
25
  def run(action)
18
26
  begin
19
27
  RailsPgExtras.run_query(query_name: action, in_format: :raw)
@@ -1,4 +1,4 @@
1
- require "rails_pg_extras"
1
+ require "rails-pg-extras"
2
2
  require "rails_pg_extras/version"
3
3
 
4
4
  module RailsPgExtras::Web
@@ -13,7 +13,7 @@ module RailsPgExtras::Web
13
13
 
14
14
  ACTIONS = %i[kill_all pg_stat_statements_reset add_extensions]
15
15
 
16
- if Rails.env.production? && ENV['RAILS_PG_EXTRAS_USER'].present? && ENV['RAILS_PG_EXTRAS_PASSWORD'].present?
16
+ if ENV['RAILS_PG_EXTRAS_USER'].present? && ENV['RAILS_PG_EXTRAS_PASSWORD'].present?
17
17
  http_basic_authenticate_with name: ENV['RAILS_PG_EXTRAS_USER'], password: ENV['RAILS_PG_EXTRAS_PASSWORD']
18
18
  end
19
19
  end
@@ -5,9 +5,11 @@
5
5
  <% end %>
6
6
  </div>
7
7
 
8
- <%= link_to "Enable extensions", add_extensions_action_path,
9
- method: "post",
10
- data: {
11
- confirm: "This command will enable following extensions: #{unavailable_extensions.keys.join(', ')}. Do you want to proceeed?"
12
- }, class: 'border p-3 bg-green-500 text-white hover:bg-green-600 font-bold rounded' %>
8
+ <% if RailsPgExtras::Web.action_enabled?(:enable_extensions) %>
9
+ <%= link_to "Enable extensions", add_extensions_action_path,
10
+ method: "post",
11
+ data: {
12
+ confirm: "This command will enable following extensions: #{unavailable_extensions.keys.join(', ')}. Do you want to proceeed?"
13
+ }, class: 'border p-3 bg-green-500 text-white hover:bg-green-600 font-bold rounded' %>
14
+ <% end %>
13
15
 
@@ -5,17 +5,21 @@
5
5
 
6
6
  <h1 class="font-bold text-xl my-5">Actions</h1>
7
7
 
8
- <%= link_to "kill_all", kill_all_action_path,
9
- method: "post",
10
- data: {
11
- confirm: "This commands kills all the currently active connections to the database. Do you want to proceed?"
12
- },
13
- class: 'border p-3 bg-red-500 text-white hover:bg-red-600 font-bold rounded'
14
- %>
8
+ <% if RailsPgExtras::Web.action_enabled?(:kill_all) %>
9
+ <%= link_to "kill_all", kill_all_action_path,
10
+ method: "post",
11
+ data: {
12
+ confirm: "This commands kills all the currently active connections to the database. Do you want to proceed?"
13
+ },
14
+ class: 'border p-3 bg-red-500 text-white hover:bg-red-600 font-bold rounded'
15
+ %>
16
+ <% end %>
15
17
 
16
- <%= link_to "pg_stat_statements_reset", pg_stat_statements_reset_action_path,
17
- method: "post",
18
- data: {
19
- confirm: "This command discards all statistics gathered so far by pg_stat_statements. Do you want to proceed?"
20
- }, class: 'border p-3 bg-blue-500 text-white hover:bg-blue-600 font-bold rounded'
21
- %>
18
+ <% if RailsPgExtras::Web.action_enabled?(:pg_stat_statements_reset) %>
19
+ <%= link_to "pg_stat_statements_reset", pg_stat_statements_reset_action_path,
20
+ method: "post",
21
+ data: {
22
+ confirm: "This command discards all statistics gathered so far by pg_stat_statements. Do you want to proceed?"
23
+ }, class: 'border p-3 bg-blue-500 text-white hover:bg-blue-600 font-bold rounded'
24
+ %>
25
+ <% end %>
@@ -1,14 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'terminal-table'
4
- require 'ruby_pg_extras'
4
+ require 'ruby-pg-extras'
5
5
  require 'rails_pg_extras/diagnose_data'
6
6
  require 'rails_pg_extras/diagnose_print'
7
7
  require 'rails_pg_extras/index_info'
8
8
  require 'rails_pg_extras/index_info_print'
9
9
  require 'rails_pg_extras/table_info'
10
10
  require 'rails_pg_extras/table_info_print'
11
- require 'rails_pg_extras/web'
12
11
 
13
12
  module RailsPgExtras
14
13
  QUERIES = RubyPgExtras::QUERIES
@@ -96,4 +95,6 @@ module RailsPgExtras
96
95
  end
97
96
  end
98
97
 
98
+ require 'rails_pg_extras/web'
99
+ require 'rails_pg_extras/configuration'
99
100
  require 'rails_pg_extras/railtie' if defined?(Rails)
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_pg_extras/web"
4
+
5
+ module RailsPgExtras
6
+ class Configuration
7
+ DEFAULT_CONFIG = { enabled_web_actions: Web::ACTIONS - [:kill_all] }
8
+
9
+ attr_reader :enabled_web_actions
10
+
11
+ def initialize(attrs)
12
+ self.enabled_web_actions = attrs[:enabled_web_actions]
13
+ end
14
+
15
+ def enabled_web_actions=(*actions)
16
+ @enabled_web_actions = actions.flatten.map(&:to_sym)
17
+ end
18
+ end
19
+
20
+ def self.configuration
21
+ @configuration ||= Configuration.new(Configuration::DEFAULT_CONFIG)
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'rails_pg_extras'
3
+ require 'rails-pg-extras'
4
4
 
5
5
  namespace :pg_extras do
6
6
  task :establish_connection do
@@ -8,7 +8,7 @@ namespace :pg_extras do
8
8
  ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
9
9
  else
10
10
  db_config_file = File.read('config/database.yml')
11
- db_config = YAML::load(ERB.new(db_config_file).result)
11
+ db_config = YAML::load(ERB.new(db_config_file).result, aliases: true)
12
12
  ActiveRecord::Base.establish_connection(db_config[Rails.env])
13
13
  end
14
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPgExtras
4
- VERSION = "4.0.1"
4
+ VERSION = "4.4.0"
5
5
  end
@@ -2,5 +2,10 @@ require "rails_pg_extras/web/engine"
2
2
 
3
3
  module RailsPgExtras
4
4
  module Web
5
+ ACTIONS = %i[kill_all pg_stat_statements_reset enable_extensions].freeze
6
+
7
+ def self.action_enabled?(action_name)
8
+ RailsPgExtras.configuration.enabled_web_actions.include?(action_name.to_sym)
9
+ end
5
10
  end
6
11
  end
data/pg-extras-ui.png ADDED
Binary file
data/spec/smoke_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
- require 'rails_pg_extras'
4
+ require 'rails-pg-extras'
5
5
 
6
6
  describe RailsPgExtras do
7
7
  RailsPgExtras::QUERIES.each do |query_name|
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'bundler/setup'
5
5
  require 'active_record'
6
- require_relative '../lib/rails_pg_extras'
6
+ require_relative '../lib/rails-pg-extras'
7
7
 
8
8
  pg_version = ENV["PG_VERSION"]
9
9
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-pg-extras
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.1
4
+ version: 4.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-10 00:00:00.000000000 Z
11
+ date: 2022-03-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-pg-extras
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.0.1
19
+ version: 4.4.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.0.1
26
+ version: 4.4.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -93,7 +93,8 @@ files:
93
93
  - app/views/rails_pg_extras/web/shared/_queries_selector.html.erb
94
94
  - config/routes.rb
95
95
  - docker-compose.yml.sample
96
- - lib/rails_pg_extras.rb
96
+ - lib/rails-pg-extras.rb
97
+ - lib/rails_pg_extras/configuration.rb
97
98
  - lib/rails_pg_extras/diagnose_data.rb
98
99
  - lib/rails_pg_extras/diagnose_print.rb
99
100
  - lib/rails_pg_extras/index_info.rb
@@ -105,8 +106,8 @@ files:
105
106
  - lib/rails_pg_extras/version.rb
106
107
  - lib/rails_pg_extras/web.rb
107
108
  - lib/rails_pg_extras/web/engine.rb
109
+ - pg-extras-ui.png
108
110
  - rails-pg-extras-diagnose.png
109
- - rails-pg-extras-web.png
110
111
  - rails-pg-extras.gemspec
111
112
  - spec/smoke_spec.rb
112
113
  - spec/spec_helper.rb
Binary file