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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfc3f0d5560a94e94ae9e4c0e4801b9a9a02e4fd9b5bb74b6b59d9569a6d8613
4
- data.tar.gz: 1d12d7b4b1e984eb1b9c0383b0a82a49f79ee5d47e5ccb12b342a0182dda572d
3
+ metadata.gz: 797e6837cbc8fb508afac284d54509ce175ac77346320bcc0697c0cd54b49a06
4
+ data.tar.gz: 487f8a88353cce686f0b259cd83f2401cb3d599a4d05e403250018b2e3ae4fdc
5
5
  SHA512:
6
- metadata.gz: 0eff35cb7949993b9161dae0d4d89b3714d513e8b6a80272c214e8a19d4b56381382fc52640773c324d896e0f7f2d9ea50ff992306cf6272506334d3f0ab5dbe
7
- data.tar.gz: 1fcd2d6d57328bc7272cdff88626fd9396837faad035b6c5f1377687a97d502ea357798889b6011d6dcb68e59ad03b112ee08bb27ea7a3aceb2ce313fd074872
6
+ metadata.gz: bc53955a16cd100e68ef306b798966f62937963ce8a3a67b819779379892fa50125b61c30e127cd1d1c3c0bd23cf58b9961a13a196430b15f55705b553ec9f0e
7
+ data.tar.gz: d0c482e0864d25b05d3dc002de0b36d6b6b2d5414d8af7402bf86390f7a1182dc2d0cd745afb6cd8dcfe5d4c889141c351f4d3f6414acd957c4d2bd43322af41
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- spring_onion (0.1.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}],"violations":{"line 1":["slow_type"]},"backtrace":["/foo/bar/zoo/baz.rb:18:in `\u003ctop (required)\u003e'"]}
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
- "violations": {
68
+ "warnings": {
69
69
  "line 1": [
70
70
  "slow_type"
71
71
  ]
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'logger'
4
+ require 'coderay'
4
5
  require 'active_support'
5
6
 
6
7
  require 'spring_onion/config'
@@ -17,7 +17,7 @@ module SpringOnion
17
17
  'Using temporary'
18
18
  )
19
19
 
20
- @violations = {
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 = ENV.fetch('SPRING_ONION_ENABLED', '') =~ /\A(1|true)\z/i
28
+ @enabled = (/\A(1|true)\z/i =~ ENV['SPRING_ONION_ENABLED'])
29
29
 
30
- @sql_filter_re = if ENV.fetch('SPRING_ONION_SQL_FILTER_RE', '').empty?
31
- //
32
- else
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
- *(ENV.fetch('SPRING_ONION_IGNORE_SQL_FILTER_RE', '').empty? ? nil : Regexp.new(ENV['SPRING_ONION_IGNORE_SQL_FILTER_RE']))
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
- *(ENV.fetch('SPRING_ONION_SOURCE_FILTER_RE', '').empty? ? nil : Regexp.new(ENV['SPRING_ONION_SOURCE_FILTER_RE']))
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
- Regexp.union(RbConfig::TOPDIR, *Gem.path),
52
- *(ENV.fetch('SPRING_ONION_IGNORE_SOURCE_FILTER_RE', '').empty? ? nil : Regexp.new(ENV['SPRING_ONION_IGNORE_SOURCE_FILTER_RE']))
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).grep(@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
- :violations,
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
- violations = SpringOnion.violations
34
- violation_names_by_line = {}
33
+ warnings = SpringOnion.warnings
34
+ warning_names_by_line = {}
35
35
 
36
36
  exp.each_with_index do |row, i|
37
- violation_names = violations.select do |_name, validator|
37
+ warning_names = warnings.select do |_name, validator|
38
38
  validator.call(row)
39
39
  end.keys
40
40
 
41
- violation_names_by_line["line #{i + 1}"] = violation_names unless violation_names.empty?
41
+ warning_names_by_line["line #{i + 1}"] = warning_names unless warning_names.empty?
42
42
  end
43
43
 
44
- return if violation_names_by_line.empty?
44
+ return if warning_names_by_line.empty?
45
45
 
46
- SpringOnion.logger.info({
46
+ h = {
47
47
  sql: sql,
48
48
  explain: exp.each_with_index.map { |r, i| { line: i + 1 }.merge(r) },
49
- violations: violation_names_by_line,
49
+ warnings: warning_names_by_line,
50
50
  backtrace: trace.slice(0, SpringOnion.trace_len),
51
- }.to_json)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SpringOnion
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -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.3.0')
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.1.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-17 00:00:00.000000000 Z
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.3.0
159
+ version: 2.5.0
146
160
  required_rubygems_version: !ruby/object:Gem::Requirement
147
161
  requirements:
148
162
  - - ">="