rails-pg-extras 4.12.2 → 5.0.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: 704a88d0240b75de358954e7c24399b580b112a0d39da66ebab10b1b6c873c68
4
- data.tar.gz: 3323892eb17b871ff0980f782c92f90f7f6c482d4104d155dde4e05c94211704
3
+ metadata.gz: f1b2e490792ca16221528d9b86cc3631420e09ceeeb7162c4cdad45e520a9a03
4
+ data.tar.gz: f6e40c1f217b8d621e76420601d8f051f0cb897a46ed6cb0d5f1be86c882ef2f
5
5
  SHA512:
6
- metadata.gz: 88564f72410fa57d18c7f8a6fd7531d98ae0a3bc46dcd98873bd0fba9821c68df542b875726c0db0c7e3a285019e5627130ac514c1c434361a12cd8a03ca4c9d
7
- data.tar.gz: accc7532505e69f17382391d434cd4b610c5ee6a73a18fc8bcc40734b41c91fc5e10cd39e2c8125d41fd80ad6f4a836eb7596be67c00e56dfe0b3afc44b933d2
6
+ metadata.gz: e010da06f594274147283f6a9aba8cdf82efc9802564639ace819194897ce97108d96323049c7a718fd91a84a2e92a9270009278738414b592d7871ff3a6ff8d
7
+ data.tar.gz: fc6291563b877bcd6ad8af7bc25676c9ae5bf1843a1c551d6a57c36fa7a3571bb58c3124e931a4214a5859498ec9f6a8fb4fc9cee0a6bd23f265adcc4f657ef5
data/README.md CHANGED
@@ -51,6 +51,12 @@ You should see the similar line in the output:
51
51
  RailsPgExtras.add_extensions
52
52
  ```
53
53
 
54
+ By deafult a primary ActiveRecord database connection is used for running metadata queries, rake tasks and web UI. To connect to a different database you can specify an `ENV['RAILS_PG_EXTRAS_DATABASE_URL']` value in the following format:
55
+
56
+ ```ruby
57
+ ENV["RAILS_PG_EXTRAS_DATABASE_URL"] = "postgresql://postgres:secret@localhost:5432/database_name"
58
+ ```
59
+
54
60
  ## Usage
55
61
 
56
62
  Each command can be used as a rake task, or a directly from the Ruby code.
@@ -134,6 +140,27 @@ end
134
140
 
135
141
  ## Available methods
136
142
 
143
+ ### `queries_data`
144
+
145
+ This method allows checking query types executed when running a provided Ruby snippet. It can help debug N+1 issues and review the impact of adding eager loading:
146
+
147
+ ```ruby
148
+
149
+ User.queries_data { User.limit(10).map(&:team) }
150
+
151
+ # {:count=>11,
152
+ # :queries=>
153
+ # {"SELECT \"users\".* FROM \"users\" LIMIT $1"=>1,
154
+ # "SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" = $1 LIMIT $2"=>10}}
155
+
156
+ User.queries_data { User.limit(10).includes(:team).map(&:team) }
157
+
158
+ # {:count=>2,
159
+ # :queries=>
160
+ # {"SELECT \"users\".* FROM \"users\" LIMIT $1"=>1,
161
+ # "SELECT \"teams\".* FROM \"teams\" WHERE \"teams\".\"id\" IN ($1, $2, $3, $4, $5, $6, $7, $8)"=>1}}
162
+ ```
163
+
137
164
  ### `table_info`
138
165
 
139
166
  This method displays metadata metrics for all or a selected table. You can use it to check the table's size, its cache hit metrics, and whether it is correctly indexed. Many sequential scans or no index scans are potential indicators of misconfigured indexes. This method aggregates data provided by other methods in an easy to analyze summary format.
@@ -62,6 +62,23 @@ module RailsPgExtras
62
62
  end
63
63
  end
64
64
 
65
+ def self.queries_data(&block)
66
+ queries = Hash.new(0)
67
+ subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") do |_name, _start, _finish, _id, payload|
68
+ unless payload[:name] =~ /SCHEMA/
69
+ queries[payload[:sql]] += 1
70
+ end
71
+ end
72
+
73
+ block.call
74
+
75
+ ActiveSupport::Notifications.unsubscribe(subscriber)
76
+ {
77
+ count: queries.values.sum,
78
+ queries: queries
79
+ }
80
+ end
81
+
65
82
  def self.index_info(args: {}, in_format: :display_table)
66
83
  data = RailsPgExtras::IndexInfo.call(args[:table_name])
67
84
 
@@ -91,7 +108,11 @@ module RailsPgExtras
91
108
  end
92
109
 
93
110
  def self.connection
94
- ActiveRecord::Base.connection
111
+ if (db_url = ENV['RAILS_PG_EXTRAS_DATABASE_URL'])
112
+ ActiveRecord::Base.establish_connection(db_url).connection
113
+ else
114
+ ActiveRecord::Base.connection
115
+ end
95
116
  end
96
117
  end
97
118
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsPgExtras
4
- VERSION = "4.12.2"
4
+ VERSION = "5.0.0"
5
5
  end
data/spec/smoke_spec.rb CHANGED
@@ -28,4 +28,19 @@ describe RailsPgExtras do
28
28
  RailsPgExtras.table_info(in_format: :hash)
29
29
  end.not_to raise_error
30
30
  end
31
+
32
+ it "collecting queries data works" do
33
+ output = RailsPgExtras.queries_data { RailsPgExtras.diagnose(in_format: :hash) }
34
+ expect(output.fetch(:count)).to eq 10
35
+ end
36
+
37
+ it "supports custom RAILS_PG_EXTRAS_DATABASE_URL" do
38
+ ENV['RAILS_PG_EXTRAS_DATABASE_URL'] = ENV['DATABASE_URL']
39
+
40
+ expect do
41
+ RailsPgExtras.calls
42
+ end.not_to raise_error
43
+
44
+ ENV['RAILS_PG_EXTRAS_DATABASE_URL'] = nil
45
+ end
31
46
  end
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.12.2
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-03 00:00:00.000000000 Z
11
+ date: 2023-02-25 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.12.2
19
+ version: 5.0.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.12.2
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement