prosopite 0.2.1 → 1.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5c95516b64f760903487cb161f0e507cd960a21770bd2729d39fd219526ce99
4
- data.tar.gz: 890f117df692947849ffb243dff96ddfb1b808b0728bd5a5e613969eb2b11838
3
+ metadata.gz: faaeeb7c0908320a1eade29fde52c959c756f30d390bf3251427aca0d5737ea4
4
+ data.tar.gz: 9843d2c7d5991d992299197b5121a68adc8cd8bc0ba7f4878f89e6eba1c00565
5
5
  SHA512:
6
- metadata.gz: 991261e0e5cb2002e2031e4d2a53c620732752425a77dee6d3fd63b06acb779cdba11aebd61531922beafc67492cc41eaa222f33a53c843e01660fe1a7b268f2
7
- data.tar.gz: 6a09687449111aabcea1cc7d58a64b325e8115504b557b41fd8bf313666eb8098713eda6aaa9d37f83716159c62cfcdce78444ed422dc6a43e40c8568e8237a8
6
+ metadata.gz: a6e47da136f7e071d1d5466ee2e60fc6f571e8c129ca3f4331a200398b824891ec4dbdadcc7e244f2f51957df386aa3b20bf2b5b7e84a77510e64a3cba6fd465
7
+ data.tar.gz: b53e5014b44e8fbef56088cb15e4a6cbf1fc4e596316425cc1c5ce0a09eda29b9aa98ea94900eb3be3c2772bb0c77b7a4433f2650119ef20e4125718a3b8e812
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ source "https://rubygems.org"
5
5
  gemspec
6
6
 
7
7
  gem "rake", "~> 13.0"
8
+ gem 'pg_query'
data/Gemfile.lock CHANGED
@@ -1,8 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prosopite (0.2.0)
5
- pg_query (~> 1.3)
4
+ prosopite (1.0.3)
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
@@ -188,6 +194,14 @@ Prosopite.scan
188
194
  Prosopite.finish
189
195
  ```
190
196
 
197
+ or
198
+
199
+ ```ruby
200
+ Prosopite.scan do
201
+ <code to scan>
202
+ end
203
+ ```
204
+
191
205
  ## Contributing
192
206
 
193
207
  Bug reports and pull requests are welcome on GitHub at https://github.com/charkost/prosopite.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prosopite
4
- VERSION = "0.2.1"
4
+ VERSION = "1.0.3"
5
5
  end
data/lib/prosopite.rb CHANGED
@@ -1,6 +1,7 @@
1
- require 'pg_query'
2
1
 
3
2
  module Prosopite
3
+ DEFAULT_ALLOW_LIST = %w(active_record/associations/preloader active_record/validations/uniqueness)
4
+
4
5
  class NPlusOneQueriesError < StandardError; end
5
6
  class << self
6
7
  attr_writer :raise,
@@ -22,6 +23,15 @@ module Prosopite
22
23
  @allow_list ||= []
23
24
 
24
25
  tc[:prosopite_scan] = true
26
+
27
+ if block_given?
28
+ begin
29
+ yield
30
+ finish
31
+ ensure
32
+ tc[:prosopite_scan] = false
33
+ end
34
+ end
25
35
  end
26
36
 
27
37
  def tc
@@ -56,12 +66,10 @@ module Prosopite
56
66
 
57
67
  kaller = tc[:prosopite_query_caller][location_key]
58
68
 
59
- if fingerprints.uniq.size == 1 && !kaller.any? { |f| @allow_list.any? { |s| f.include?(s) } }
69
+ is_allowed = kaller.any? { |f| @allow_list.concat(DEFAULT_ALLOW_LIST).any? { |s| f.include?(s) } }
70
+ if fingerprints.uniq.size == 1 && !is_allowed
60
71
  queries = tc[:prosopite_query_holder][location_key]
61
-
62
- unless kaller.any? { |f| f.include?('active_record/validations/uniqueness') }
63
- tc[:prosopite_notifications][queries] = kaller
64
- end
72
+ tc[:prosopite_notifications][queries] = kaller
65
73
  end
66
74
  end
67
75
  end
@@ -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.xb+-]*/, "?")
117
+ query.gsub!(/[0-9+-][0-9a-f.x+-]*/, "?")
104
118
  query.gsub!(/[xb.+-]\?/, "?")
105
119
 
106
120
  query.strip!
@@ -161,9 +175,9 @@ module Prosopite
161
175
  return if @subscribed
162
176
 
163
177
  ActiveSupport::Notifications.subscribe 'sql.active_record' do |_, _, _, _, data|
164
- sql = data[:sql]
178
+ sql, name = data[:sql], data[:name]
165
179
 
166
- if scan? && sql.include?('SELECT') && data[:cached].nil?
180
+ if scan? && name != "SCHEMA" && sql.include?('SELECT') && data[:cached].nil?
167
181
  location_key = Digest::SHA1.hexdigest(caller.join)
168
182
 
169
183
  tc[:prosopite_query_counter][location_key] += 1
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.2.1
4
+ version: 1.0.3
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-03-10 00:00:00.000000000 Z
11
+ date: 2021-09-25 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