ar-query-matchers 0.5.1 → 0.5.2.pre.5
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2aaeaac19a8660c885b5ac75c32077ab6b0f835c53aaefa1bb3d3b7bab701fd4
|
4
|
+
data.tar.gz: 3746c66186e7d067fc82895a8d7bb90dc230eb7ddef7553ad2bb9631bd69d097
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d43af021387d6a1ea871bf13d876a512e19cf98eb702f85f9ff0446970c8f6bfbf40356d1eb0c1417664fd8b47ab4e8aab115785f06f8f22f547ab284dfa809
|
7
|
+
data.tar.gz: f8785fb4accfb4f7a2573ced84211769793cc72bb6784de3884665f39a63346ebcc83046e737e6fca3e56a9fd1dbafc563805800b49df32ee77951137c76f8aa
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,10 @@ 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
|
10
|
+
### Changed
|
11
|
+
- Removes 'SELECT' from MODEL_SQL_PATTERN to allow for more granular SQL statement matching
|
12
|
+
|
9
13
|
## [0.5.1] - 2020-11-19
|
10
14
|
### Changed
|
11
15
|
- Removes zero count expectations from hash before comparing
|
@@ -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
|
@@ -21,18 +21,21 @@ module ArQueryMatchers
|
|
21
21
|
|
22
22
|
# Matches unnamed SQL operations like the following:
|
23
23
|
# "SELECT COUNT(*) FROM `users` ..."
|
24
|
-
MODEL_SQL_PATTERN = /
|
24
|
+
MODEL_SQL_PATTERN = /FROM [`"](?<table_name>[^`"]+)[`"]/.freeze
|
25
25
|
|
26
|
-
def filter_map(
|
26
|
+
def filter_map(_name, sql)
|
27
27
|
# First check for a `SELECT * FROM` query that ActiveRecord has
|
28
28
|
# helpfully named for us in the payload
|
29
|
-
|
30
|
-
|
29
|
+
#
|
30
|
+
# NOTE: This misses possible subqueries and prevents us from getting
|
31
|
+
# to the below matcher
|
32
|
+
# match = name.match(MODEL_LOAD_PATTERN)
|
33
|
+
# return [ModelName.new(match[:model_name])] if match
|
31
34
|
|
32
35
|
# Fall back to pattern-matching on the table name in a COUNT and looking
|
33
36
|
# up the table name from ActiveRecord's loaded descendants.
|
34
|
-
|
35
|
-
TableName.new(
|
37
|
+
selects_from_table = sql.scan(MODEL_SQL_PATTERN)
|
38
|
+
selects_from_table.map { |(table_name)| TableName.new(table_name) } unless selects_from_table.empty?
|
36
39
|
end
|
37
40
|
end
|
38
41
|
end
|
@@ -81,9 +81,13 @@ 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
|
-
|
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
|
85
90
|
|
86
|
-
if model_name
|
87
91
|
comment = payload[:sql].match(MARGINALIA_SQL_COMMENT_PATTERN)
|
88
92
|
queries[model_name][:lines] << comment[:line] if comment
|
89
93
|
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.
|
4
|
+
version: 0.5.2.pre.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matan Zruya
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -186,11 +186,11 @@ 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:
|
191
|
+
version: 1.3.1
|
192
192
|
requirements: []
|
193
|
-
rubygems_version: 3.1
|
193
|
+
rubygems_version: 3.0.3.1
|
194
194
|
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: Ruby test matchers for instrumenting ActiveRecord query counts
|