spring_onion 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -1
- data/README.md +3 -3
- data/lib/spring_onion.rb +1 -0
- data/lib/spring_onion/config.rb +26 -16
- data/lib/spring_onion/explainer.rb +17 -8
- data/lib/spring_onion/version.rb +1 -1
- data/spring_onion.gemspec +2 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 797e6837cbc8fb508afac284d54509ce175ac77346320bcc0697c0cd54b49a06
|
4
|
+
data.tar.gz: 487f8a88353cce686f0b259cd83f2401cb3d599a4d05e403250018b2e3ae4fdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc53955a16cd100e68ef306b798966f62937963ce8a3a67b819779379892fa50125b61c30e127cd1d1c3c0bd23cf58b9961a13a196430b15f55705b553ec9f0e
|
7
|
+
data.tar.gz: d0c482e0864d25b05d3dc002de0b36d6b6b2d5414d8af7402bf86390f7a1182dc2d0cd745afb6cd8dcfe5d4c889141c351f4d3f6414acd957c4d2bd43322af41
|
data/Gemfile.lock
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
spring_onion (0.
|
4
|
+
spring_onion (0.2.0)
|
5
5
|
activerecord
|
6
|
+
coderay
|
6
7
|
mysql2
|
7
8
|
|
8
9
|
GEM
|
@@ -24,6 +25,7 @@ GEM
|
|
24
25
|
rake
|
25
26
|
thor (>= 0.14.0)
|
26
27
|
ast (2.4.1)
|
28
|
+
coderay (1.1.3)
|
27
29
|
concurrent-ruby (1.1.6)
|
28
30
|
diff-lcs (1.4.4)
|
29
31
|
i18n (1.8.3)
|
data/README.md
CHANGED
@@ -33,14 +33,14 @@ ActiveRecord::Base.establish_connection(
|
|
33
33
|
database: 'employees',
|
34
34
|
)
|
35
35
|
|
36
|
-
SpringOnion.enabled = true
|
36
|
+
SpringOnion.enabled = true # or `SPRING_ONION_ENABLED=1`
|
37
37
|
SpringOnion.connection = ActiveRecord::Base.connection.raw_connection
|
38
38
|
SpringOnion.source_filter_re = //
|
39
39
|
|
40
40
|
class Employee < ActiveRecord::Base; end
|
41
41
|
|
42
42
|
Employee.all.to_a.count
|
43
|
-
#=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"
|
43
|
+
#=> SpringOnion INFO 2020-07-18 01:53:27 +0900 {"sql":"SELECT `employees`.* FROM `employees`","explain":[{"line":1,"select_type":"SIMPLE","table":"employees","partitions":null,"type":"ALL","possible_keys":null,"key":null,"key_len":null,"ref":null,"rows":298936,"filtered":100.0,"Extra":null}],"warnings":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
|
44
44
|
#=> 300024
|
45
45
|
```
|
46
46
|
|
@@ -65,7 +65,7 @@ Employee.all.to_a.count
|
|
65
65
|
"Extra": null
|
66
66
|
}
|
67
67
|
],
|
68
|
-
"
|
68
|
+
"warnings": {
|
69
69
|
"line 1": [
|
70
70
|
"slow_type"
|
71
71
|
]
|
data/lib/spring_onion.rb
CHANGED
data/lib/spring_onion/config.rb
CHANGED
@@ -17,7 +17,7 @@ module SpringOnion
|
|
17
17
|
'Using temporary'
|
18
18
|
)
|
19
19
|
|
20
|
-
@
|
20
|
+
@warnings = {
|
21
21
|
slow_select_type: ->(exp) { SLOW_SELECT_TYPE_RE =~ exp['select_type'] },
|
22
22
|
slow_type: ->(exp) { SLOW_TYPE_RE =~ exp['type'] },
|
23
23
|
slow_possible_keys: ->(exp) { SLOW_POSSIBLE_KEYS_RE =~ exp['possible_keys'] },
|
@@ -25,17 +25,17 @@ module SpringOnion
|
|
25
25
|
slow_extra: ->(exp) { SLOW_EXTRA_RE =~ exp['Extra'] },
|
26
26
|
}
|
27
27
|
|
28
|
-
@enabled =
|
28
|
+
@enabled = (/\A(1|true)\z/i =~ ENV['SPRING_ONION_ENABLED'])
|
29
29
|
|
30
|
-
@sql_filter_re =
|
31
|
-
|
32
|
-
|
33
|
-
Regexp.new(ENV['SPRING_ONION_SQL_FILTER_RE'])
|
34
|
-
end
|
30
|
+
@sql_filter_re = ENV['SPRING_ONION_SQL_FILTER_RE'].yield_self do |re|
|
31
|
+
re ? Regexp.new(re) : //
|
32
|
+
end
|
35
33
|
|
36
34
|
@ignore_sql_filter_re = Regexp.union(
|
37
|
-
/\binformation_schema\b
|
38
|
-
|
35
|
+
[/\binformation_schema\b/].tap do |ary|
|
36
|
+
re = ENV['SPRING_ONION_IGNORE_SQL_FILTER_RE']
|
37
|
+
ary << Regexp.new(re) if re
|
38
|
+
end
|
39
39
|
)
|
40
40
|
|
41
41
|
@sql_filter = lambda do |sql|
|
@@ -43,17 +43,23 @@ module SpringOnion
|
|
43
43
|
end
|
44
44
|
|
45
45
|
@source_filter_re = Regexp.union(
|
46
|
-
%r{/app/}
|
47
|
-
|
46
|
+
[%r{/app/}].tap do |ary|
|
47
|
+
re = ENV['SPRING_ONION_SOURCE_FILTER_RE']
|
48
|
+
ary << Regexp.new(re) if re
|
49
|
+
end
|
48
50
|
)
|
49
51
|
|
50
52
|
@ignore_source_filter_re = Regexp.union(
|
51
|
-
|
52
|
-
|
53
|
+
[RbConfig::TOPDIR, *Gem.path, '/.rbenv/versions/'].tap do |ary|
|
54
|
+
re = ENV['SPRING_ONION_IGNORE_SOURCE_FILTER_RE']
|
55
|
+
ary << Regexp.new(re) if re
|
56
|
+
end
|
53
57
|
)
|
54
58
|
|
55
59
|
@source_filter = lambda do |backtrace_lines|
|
56
|
-
backtrace_lines.grep_v(@ignore_source_filter_re)
|
60
|
+
backtrace_lines = backtrace_lines.grep_v(@ignore_source_filter_re)
|
61
|
+
idx = backtrace_lines.index { |l| @source_filter_re =~ l }
|
62
|
+
idx ? backtrace_lines.slice(idx..-1) : []
|
57
63
|
end
|
58
64
|
|
59
65
|
@logger = Logger.new($stdout).tap do |logger|
|
@@ -63,14 +69,18 @@ module SpringOnion
|
|
63
69
|
end
|
64
70
|
|
65
71
|
@trace_len = 3
|
72
|
+
@json_pretty = (/\A(1|true)\z/i =~ ENV['SPRING_ONION_JSON_PRETTY'])
|
73
|
+
@color = /\A(1|true)\z/i =~ ENV.fetch('SPRING_ONION_COLOR', $stdout.tty?.to_s)
|
66
74
|
|
67
75
|
class << self
|
68
76
|
attr_accessor :enabled,
|
69
77
|
:connection,
|
70
|
-
:
|
78
|
+
:warnings,
|
71
79
|
:sql_filter_re, :ignore_sql_filter_re, :sql_filter,
|
72
80
|
:source_filter_re, :ignore_source_filter_re, :source_filter,
|
73
81
|
:logger,
|
74
|
-
:trace_len
|
82
|
+
:trace_len,
|
83
|
+
:json_pretty,
|
84
|
+
:color
|
75
85
|
end
|
76
86
|
end
|
@@ -30,25 +30,34 @@ module SpringOnion
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def _validate(exp, sql, trace)
|
33
|
-
|
34
|
-
|
33
|
+
warnings = SpringOnion.warnings
|
34
|
+
warning_names_by_line = {}
|
35
35
|
|
36
36
|
exp.each_with_index do |row, i|
|
37
|
-
|
37
|
+
warning_names = warnings.select do |_name, validator|
|
38
38
|
validator.call(row)
|
39
39
|
end.keys
|
40
40
|
|
41
|
-
|
41
|
+
warning_names_by_line["line #{i + 1}"] = warning_names unless warning_names.empty?
|
42
42
|
end
|
43
43
|
|
44
|
-
return if
|
44
|
+
return if warning_names_by_line.empty?
|
45
45
|
|
46
|
-
|
46
|
+
h = {
|
47
47
|
sql: sql,
|
48
48
|
explain: exp.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
|
49
|
-
|
49
|
+
warnings: warning_names_by_line,
|
50
50
|
backtrace: trace.slice(0, SpringOnion.trace_len),
|
51
|
-
}
|
51
|
+
}
|
52
|
+
|
53
|
+
line = if SpringOnion.json_pretty
|
54
|
+
JSON.pretty_generate(h)
|
55
|
+
else
|
56
|
+
JSON.dump(h)
|
57
|
+
end
|
58
|
+
|
59
|
+
line = CodeRay.scan(line, :json).terminal if SpringOnion.color
|
60
|
+
SpringOnion.logger.info(line)
|
52
61
|
end
|
53
62
|
end
|
54
63
|
end
|
data/lib/spring_onion/version.rb
CHANGED
data/spring_onion.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = 'Log MySQL queries with EXPLAIN that may be slow.'
|
13
13
|
spec.homepage = 'https://github.com/winebarrel/spring_onion'
|
14
14
|
spec.license = 'MIT'
|
15
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
16
16
|
|
17
17
|
# Specify which files should be added to the gem when it is released.
|
18
18
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -24,6 +24,7 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.require_paths = ['lib']
|
25
25
|
|
26
26
|
spec.add_dependency 'activerecord'
|
27
|
+
spec.add_dependency 'coderay'
|
27
28
|
spec.add_dependency 'mysql2'
|
28
29
|
spec.add_development_dependency 'appraisal'
|
29
30
|
spec.add_development_dependency 'bundler'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spring_onion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- winebarrel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coderay
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: mysql2
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
142
156
|
requirements:
|
143
157
|
- - ">="
|
144
158
|
- !ruby/object:Gem::Version
|
145
|
-
version: 2.
|
159
|
+
version: 2.5.0
|
146
160
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
161
|
requirements:
|
148
162
|
- - ">="
|