prosopite 0.1.6 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -0
- data/Gemfile.lock +2 -2
- data/README.md +22 -6
- data/lib/prosopite.rb +25 -7
- data/lib/prosopite/version.rb +1 -1
- data/prosopite.gemspec +0 -2
- metadata +2 -16
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f4f17a47aa9ab87c866f3fb9c493d3b5baae8248781346e497b90b3dc23f488d
|
|
4
|
+
data.tar.gz: b9e0e29a7774adb0f8600d8fedeb2de8f9a8149468d216e1ac2e05e587445939
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c143ec186f20ee7e770619074a532f1bec494f2149e572be083eea05ee569ded551611043e22acfbe36e016285081cb43502b6f79e786491eaab4d04900fd47c
|
|
7
|
+
data.tar.gz: 1e5331db9ff3ce75299cd10468ab48341a54d8f451a2487d08646f757a801a193d930c4512991d7b2fec6be3766a34cb80a081cfddef620df6a31dd82c58f25b
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
prosopite (0.
|
|
5
|
-
pg_query (~> 1.3)
|
|
4
|
+
prosopite (1.0.0)
|
|
6
5
|
|
|
7
6
|
GEM
|
|
8
7
|
remote: https://rubygems.org/
|
|
@@ -44,6 +43,7 @@ DEPENDENCIES
|
|
|
44
43
|
activerecord
|
|
45
44
|
factory_bot
|
|
46
45
|
minitest
|
|
46
|
+
pg_query
|
|
47
47
|
prosopite!
|
|
48
48
|
pry
|
|
49
49
|
rake (~> 13.0)
|
data/README.md
CHANGED
|
@@ -97,6 +97,12 @@ Add this line to your application's Gemfile:
|
|
|
97
97
|
gem 'prosopite'
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
If you're **not** using MySQL/MariaDB, you should also add:
|
|
101
|
+
|
|
102
|
+
```ruby
|
|
103
|
+
gem 'pg_query'
|
|
104
|
+
```
|
|
105
|
+
|
|
100
106
|
And then execute:
|
|
101
107
|
|
|
102
108
|
$ bundle install
|
|
@@ -120,12 +126,14 @@ Prosopite auto-detection can be enabled on all controllers:
|
|
|
120
126
|
|
|
121
127
|
```ruby
|
|
122
128
|
class ApplicationController < ActionController::Base
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
+
unless Rails.env.production?
|
|
130
|
+
before_action do
|
|
131
|
+
Prosopite.scan
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
after_action do
|
|
135
|
+
Prosopite.finish
|
|
136
|
+
end
|
|
129
137
|
end
|
|
130
138
|
end
|
|
131
139
|
```
|
|
@@ -186,6 +194,14 @@ Prosopite.scan
|
|
|
186
194
|
Prosopite.finish
|
|
187
195
|
```
|
|
188
196
|
|
|
197
|
+
or
|
|
198
|
+
|
|
199
|
+
```ruby
|
|
200
|
+
Prosopite.scan do
|
|
201
|
+
<code to scan>
|
|
202
|
+
end
|
|
203
|
+
```
|
|
204
|
+
|
|
189
205
|
## Contributing
|
|
190
206
|
|
|
191
207
|
Bug reports and pull requests are welcome on GitHub at https://github.com/charkost/prosopite.
|
data/lib/prosopite.rb
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
require 'pg_query'
|
|
2
1
|
|
|
3
2
|
module Prosopite
|
|
4
3
|
class NPlusOneQueriesError < StandardError; end
|
|
@@ -22,6 +21,15 @@ module Prosopite
|
|
|
22
21
|
@allow_list ||= []
|
|
23
22
|
|
|
24
23
|
tc[:prosopite_scan] = true
|
|
24
|
+
|
|
25
|
+
if block_given?
|
|
26
|
+
begin
|
|
27
|
+
yield
|
|
28
|
+
finish
|
|
29
|
+
ensure
|
|
30
|
+
tc[:prosopite_scan] = false
|
|
31
|
+
end
|
|
32
|
+
end
|
|
25
33
|
end
|
|
26
34
|
|
|
27
35
|
def tc
|
|
@@ -71,6 +79,12 @@ module Prosopite
|
|
|
71
79
|
if ActiveRecord::Base.connection.adapter_name.downcase.include?('mysql')
|
|
72
80
|
mysql_fingerprint(query)
|
|
73
81
|
else
|
|
82
|
+
begin
|
|
83
|
+
require 'pg_query'
|
|
84
|
+
rescue LoadError => e
|
|
85
|
+
msg = "Could not load the 'pg_query' gem. Add `gem 'pg_query'` to your Gemfile"
|
|
86
|
+
raise LoadError, msg, e.backtrace
|
|
87
|
+
end
|
|
74
88
|
PgQuery.fingerprint(query)
|
|
75
89
|
end
|
|
76
90
|
end
|
|
@@ -100,7 +114,7 @@ module Prosopite
|
|
|
100
114
|
|
|
101
115
|
query.gsub!(/\btrue\b|\bfalse\b/i, "?")
|
|
102
116
|
|
|
103
|
-
query.gsub!(/[0-9+-][0-9a-f.
|
|
117
|
+
query.gsub!(/[0-9+-][0-9a-f.x+-]*/, "?")
|
|
104
118
|
query.gsub!(/[xb.+-]\?/, "?")
|
|
105
119
|
|
|
106
120
|
query.strip!
|
|
@@ -140,8 +154,8 @@ module Prosopite
|
|
|
140
154
|
notifications_str << "\n"
|
|
141
155
|
end
|
|
142
156
|
|
|
143
|
-
Rails.logger.warn(notifications_str) if @rails_logger
|
|
144
|
-
$stderr.puts(notifications_str) if @stderr_logger
|
|
157
|
+
Rails.logger.warn(red(notifications_str)) if @rails_logger
|
|
158
|
+
$stderr.puts(red(notifications_str)) if @stderr_logger
|
|
145
159
|
|
|
146
160
|
if @prosopite_logger
|
|
147
161
|
File.open(File.join(Rails.root, 'log', 'prosopite.log'), 'a') do |f|
|
|
@@ -152,9 +166,13 @@ module Prosopite
|
|
|
152
166
|
raise NPlusOneQueriesError.new(notifications_str) if @raise
|
|
153
167
|
end
|
|
154
168
|
|
|
169
|
+
def red(str)
|
|
170
|
+
str.split("\n").map { |line| "\e[91m#{line}\e[0m" }.join("\n")
|
|
171
|
+
end
|
|
172
|
+
|
|
155
173
|
def subscribe
|
|
156
|
-
|
|
157
|
-
return if
|
|
174
|
+
@subscribed ||= false
|
|
175
|
+
return if @subscribed
|
|
158
176
|
|
|
159
177
|
ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, _, _, _, data|
|
|
160
178
|
sql = data[:sql]
|
|
@@ -171,7 +189,7 @@ module Prosopite
|
|
|
171
189
|
end
|
|
172
190
|
end
|
|
173
191
|
|
|
174
|
-
|
|
192
|
+
@subscribed = true
|
|
175
193
|
end
|
|
176
194
|
end
|
|
177
195
|
end
|
data/lib/prosopite/version.rb
CHANGED
data/prosopite.gemspec
CHANGED
|
@@ -24,8 +24,6 @@ Gem::Specification.new do |spec|
|
|
|
24
24
|
end
|
|
25
25
|
spec.require_paths = ["lib"]
|
|
26
26
|
|
|
27
|
-
spec.add_runtime_dependency 'pg_query', '~> 1.3'
|
|
28
|
-
|
|
29
27
|
spec.add_development_dependency "pry"
|
|
30
28
|
spec.add_development_dependency "minitest"
|
|
31
29
|
spec.add_development_dependency "factory_bot"
|
metadata
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: prosopite
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1
|
|
4
|
+
version: 1.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mpampis Kostas
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-
|
|
11
|
+
date: 2021-04-07 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: pg_query
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - "~>"
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '1.3'
|
|
20
|
-
type: :runtime
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - "~>"
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '1.3'
|
|
27
13
|
- !ruby/object:Gem::Dependency
|
|
28
14
|
name: pry
|
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|