ar-query-matchers 0.5.2.pre.6 → 0.5.2

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: a03e79681fb7644c916a5f3c019a5e9484f8b7176e21b6c91beae9bd2d9eb520
4
- data.tar.gz: 1a1d6f97aab26efa604ae32dd2d4b56164e4616ff87b2ffbfb84812e87e57a75
3
+ metadata.gz: ab18e9a530a6577ba47fbc78341913bc83ec6c2976dfd3404be8819d346fcab8
4
+ data.tar.gz: 9801846663ecc57468f79a66a7c12f170f9ddb2fef79d35aa33c36910c8d9a8a
5
5
  SHA512:
6
- metadata.gz: 960fb79a70f3875f606d67364227ad6b0af6440b18fed4e885a8a44614d61f484ad7cdc33e6040b114b568d320edcc045a212a9db0c570577a039bb7a04b09c6
7
- data.tar.gz: 379ef8320c724772bcf9fc38dda46a86fa8a24a8690a9547a13973065dcebef9fae17f77a636e059463f91afbba2aa341164ddadbe900e58fb625d3f1b64a5c4
6
+ metadata.gz: aa331847dc1130257f6af7cc567e636ccedc8c7cecbd90383606f4c17050ab7c05546b51aa3cea040f1416082e45d98c04182341a13fddcfbbb66148183b9c43
7
+ data.tar.gz: 4becc7972b419b4e55d0f5cf55292ec8dfad6d6a1804bf9315c4368e842ec8a6c98506028b1ae7df41d2736f8a80b67fb745f1c9bb9e98328092db745baf8619
data/CHANGELOG.md CHANGED
@@ -6,9 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [0.5.2] - 2021-05-18
9
+ ## [0.5.2] - 2021-05-26
10
10
  ### Changed
11
- - Removes 'SELECT' from MODEL_SQL_PATTERN to allow for more granular SQL statement matching
11
+ - Update MODEL_SQL_PATTERN to allow for more granular matches; remove MODEL_LOAD_PATTERN
12
12
 
13
13
  ## [0.5.1] - 2020-11-19
14
14
  ### Changed
@@ -23,7 +23,7 @@ module ArQueryMatchers
23
23
  # for inserts, name is always 'SQL', we have to rely on pattern matching the query string.
24
24
  select_from_table = sql.match(TABLE_NAME_SQL_PATTERN)
25
25
 
26
- [TableName.new(select_from_table[:table_name])] if select_from_table
26
+ TableName.new(select_from_table[:table_name]) if select_from_table
27
27
  end
28
28
  end
29
29
  end
@@ -15,15 +15,24 @@ module ArQueryMatchers
15
15
  end
16
16
 
17
17
  class LoadQueryFilter < Queries::QueryFilter
18
+ # Matches named SQL operations like the following:
19
+ # 'User Load'
20
+ MODEL_LOAD_PATTERN = /\A(?<model_name>[\w:]+) (Load|Exists)\Z/.freeze
21
+
18
22
  # Matches unnamed SQL operations like the following:
19
- # "SELECT * FROM `users` ..."
23
+ # "SELECT COUNT(*) FROM `users` ..."
20
24
  MODEL_SQL_PATTERN = /SELECT (?:(?!SELECT).)* FROM [`"](?<table_name>[^`"]+)[`"]/.freeze
21
25
 
22
- def filter_map(_name, sql)
23
- # Pattern-matching on the table name in a SELECT ... FROM and looking
26
+ def filter_map(name, sql)
27
+ # First check for a `SELECT * FROM` query that ActiveRecord has
28
+ # helpfully named for us in the payload
29
+ match = name.match(MODEL_LOAD_PATTERN)
30
+ return ModelName.new(match[:model_name]) if match
31
+
32
+ # Fall back to pattern-matching on the table name in a COUNT and looking
24
33
  # up the table name from ActiveRecord's loaded descendants.
25
- selects_from_table = sql.scan(MODEL_SQL_PATTERN)
26
- selects_from_table.map { |(table_name)| TableName.new(table_name) } unless selects_from_table.empty?
34
+ select_from_table = sql.match(MODEL_SQL_PATTERN)
35
+ TableName.new(select_from_table[:table_name]) if select_from_table
27
36
  end
28
37
  end
29
38
  end
@@ -81,13 +81,9 @@ module ArQueryMatchers
81
81
  # Given a `sql.active_record` event, figure out which model is being
82
82
  # accessed. Some of the simpler queries have a :name key that makes this
83
83
  # really easy. Others require parsing the SQL by hand.
84
- results = @query_filter.filter_map(payload[:name] || '', payload[:sql] || '')
85
-
86
- # Round to microseconds
87
- results&.each do |result|
88
- model_name = result.model_name
89
- next unless model_name
84
+ model_name = @query_filter.filter_map(payload[:name] || '', payload[:sql] || '')&.model_name
90
85
 
86
+ if model_name
91
87
  comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
92
88
  queries[model_name][:lines] << comment[:line] if comment
93
89
  queries[model_name][:count] += 1
@@ -22,7 +22,7 @@ module ArQueryMatchers
22
22
  def filter_map(_name, sql)
23
23
  # for updates, name is always 'SQL', we have to rely on pattern matching on the query string instead.
24
24
  select_from_table = sql.match(TABLE_NAME_SQL_PATTERN)
25
- [TableName.new(select_from_table[:table_name])] if select_from_table
25
+ TableName.new(select_from_table[:table_name]) if select_from_table
26
26
  end
27
27
  end
28
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-query-matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2.pre.6
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matan Zruya
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-26 00:00:00.000000000 Z
11
+ date: 2021-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -186,9 +186,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
186
186
  version: '0'
187
187
  required_rubygems_version: !ruby/object:Gem::Requirement
188
188
  requirements:
189
- - - ">"
189
+ - - ">="
190
190
  - !ruby/object:Gem::Version
191
- version: 1.3.1
191
+ version: '0'
192
192
  requirements: []
193
193
  rubygems_version: 3.0.3.1
194
194
  signing_key: