activerecord-explainer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +38 -0
- data/Rakefile +6 -0
- data/activerecord-explainer.gemspec +35 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/activerecord-explainer.rb +3 -0
- data/lib/activerecord/explainer.rb +25 -0
- data/lib/activerecord/explainer/config.rb +15 -0
- data/lib/activerecord/explainer/railtie.rb +16 -0
- data/lib/activerecord/explainer/subscriber.rb +51 -0
- data/lib/activerecord/explainer/version.rb +5 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4e63c897891645df93cbd00ad44dad995b6cf7ee1420c815959eefefcc91a861
|
4
|
+
data.tar.gz: 3f9578671205fa47e3f3eb7ed12ca61dcfbd67843610c162f87240f8799e2ce1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9370f82fdfab38ddfcab8de3464fed927801af45fca0c93ca274cc115212c8abe7afb3f7a7a67aff19a6ec26acd747ba32629c8c7747fa9cb0ba1346bf3c400
|
7
|
+
data.tar.gz: 47aa0283bf3f5ed443c681dd28085b20b3eb50473751c0fde419cd01ab37490b9680849078dfbaa47bf96f1635e44b8279fe4920a7e2d607b3a84d957f8b5a86
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
2
|
+
# configuration file. It makes it possible to enable/disable
|
3
|
+
# certain cops (checks) and to alter their behavior if they accept
|
4
|
+
# any parameters. The file can be placed either in your home
|
5
|
+
# directory or in some project directory.
|
6
|
+
#
|
7
|
+
# RuboCop will start looking for the configuration file in the directory
|
8
|
+
# where the inspected file is and continue its way up to the root directory.
|
9
|
+
#
|
10
|
+
# See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
|
11
|
+
AllCops:
|
12
|
+
TargetRubyVersion: 2.2
|
13
|
+
|
14
|
+
Metrics:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Naming/FileName:
|
18
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
activerecord-explainer (0.1.0)
|
5
|
+
activerecord (>= 5.2)
|
6
|
+
activesupport (>= 5.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (5.2.3)
|
12
|
+
activesupport (= 5.2.3)
|
13
|
+
activerecord (5.2.3)
|
14
|
+
activemodel (= 5.2.3)
|
15
|
+
activesupport (= 5.2.3)
|
16
|
+
arel (>= 9.0)
|
17
|
+
activesupport (5.2.3)
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
+
i18n (>= 0.7, < 2)
|
20
|
+
minitest (~> 5.1)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
arel (9.0.0)
|
23
|
+
ast (2.4.0)
|
24
|
+
concurrent-ruby (1.1.5)
|
25
|
+
diff-lcs (1.3)
|
26
|
+
i18n (1.6.0)
|
27
|
+
concurrent-ruby (~> 1.0)
|
28
|
+
jaro_winkler (1.5.2)
|
29
|
+
minitest (5.11.3)
|
30
|
+
parallel (1.17.0)
|
31
|
+
parser (2.6.3.0)
|
32
|
+
ast (~> 2.4.0)
|
33
|
+
rainbow (3.0.0)
|
34
|
+
rake (10.5.0)
|
35
|
+
rspec (3.8.0)
|
36
|
+
rspec-core (~> 3.8.0)
|
37
|
+
rspec-expectations (~> 3.8.0)
|
38
|
+
rspec-mocks (~> 3.8.0)
|
39
|
+
rspec-core (3.8.0)
|
40
|
+
rspec-support (~> 3.8.0)
|
41
|
+
rspec-expectations (3.8.3)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.8.0)
|
44
|
+
rspec-mocks (3.8.0)
|
45
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
46
|
+
rspec-support (~> 3.8.0)
|
47
|
+
rspec-support (3.8.0)
|
48
|
+
rubocop (0.68.1)
|
49
|
+
jaro_winkler (~> 1.5.1)
|
50
|
+
parallel (~> 1.10)
|
51
|
+
parser (>= 2.5, != 2.5.1.1)
|
52
|
+
rainbow (>= 2.2.2, < 4.0)
|
53
|
+
ruby-progressbar (~> 1.7)
|
54
|
+
unicode-display_width (>= 1.4.0, < 1.6)
|
55
|
+
ruby-progressbar (1.10.0)
|
56
|
+
thread_safe (0.3.6)
|
57
|
+
tzinfo (1.2.5)
|
58
|
+
thread_safe (~> 0.1)
|
59
|
+
unicode-display_width (1.5.0)
|
60
|
+
|
61
|
+
PLATFORMS
|
62
|
+
ruby
|
63
|
+
|
64
|
+
DEPENDENCIES
|
65
|
+
activerecord-explainer!
|
66
|
+
bundler (~> 2.0)
|
67
|
+
rake (~> 10.0)
|
68
|
+
rspec (~> 3.0)
|
69
|
+
rubocop
|
70
|
+
|
71
|
+
BUNDLED WITH
|
72
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Yoshiyuki Hirano
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# ActiveRecord::Explainer
|
2
|
+
|
3
|
+
activerecord-explainer puts `SQL EXPLAIN` of every queries to **development** log.
|
4
|
+
|
5
|
+
You can check every SQL EXPLAIN from development log, without :hand: executions.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
It's no configuration, and you can puts SQL EXPLAIN of every queries to logs.
|
10
|
+
|
11
|
+
![](https://github.com/yhirano55/activerecord-explainer/blob/master/images/capture.png?raw=true)
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'activerecord-explainer', group: :development
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install activerecord-explainer
|
28
|
+
|
29
|
+
## How it work?
|
30
|
+
|
31
|
+
- This gem subscribes `sql.active_record` event provided by the [Active Support instrumentation API](https://guides.rubyonrails.org/active_support_instrumentation.html).
|
32
|
+
- Subscriber handles payload, and execute EXPLAIN from `ActiveRecord::Base`.
|
33
|
+
|
34
|
+
Please check the [implementation](https://github.com/yhirano55/activerecord-explainer/blob/master/lib/activerecord/explainer/subscriber.rb), if you interested in.
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
[MIT License](https://opensource.org/licenses/MIT)
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('./lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'activerecord/explainer/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'activerecord-explainer'
|
9
|
+
s.version = ActiveRecord::Explainer::VERSION
|
10
|
+
s.authors = ['Yoshiyuki Hirano']
|
11
|
+
s.email = ['yhirano@me.com']
|
12
|
+
s.homepage = 'https://github.com/yhirano55/activerecord-explainer'
|
13
|
+
s.summary = %(activerecord-explainer puts SQL EXPLAIN of every queries to development log)
|
14
|
+
s.description = s.summary
|
15
|
+
s.license = 'MIT'
|
16
|
+
s.files = Dir.chdir(File.expand_path('.', __dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features|images)/})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
s.bindir = 'exe'
|
23
|
+
s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
s.require_paths = ['lib']
|
25
|
+
|
26
|
+
s.required_ruby_version = '>= 2.2.2'
|
27
|
+
s.required_rubygems_version = '>= 1.8.11'
|
28
|
+
|
29
|
+
s.add_dependency 'activerecord', '>= 5.2'
|
30
|
+
s.add_dependency 'activesupport', '>= 5.2'
|
31
|
+
|
32
|
+
s.add_development_dependency 'bundler', '~> 2.0'
|
33
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
34
|
+
s.add_development_dependency 'rspec', '~> 3.0'
|
35
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'activerecord/explainer'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_record'
|
4
|
+
|
5
|
+
require_relative 'explainer/config'
|
6
|
+
require_relative 'explainer/version'
|
7
|
+
|
8
|
+
begin
|
9
|
+
require 'rails'
|
10
|
+
require_relative 'explainer/railtie'
|
11
|
+
rescue LoadError
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
|
15
|
+
module ActiveRecord
|
16
|
+
module Explainer # :nodoc:
|
17
|
+
def self.configure
|
18
|
+
yield config
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config
|
22
|
+
@config ||= Config.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Explainer
|
5
|
+
class Railtie < ::Rails::Railtie # :nodoc:
|
6
|
+
config.after_initialize do
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
require_relative 'subscriber'
|
9
|
+
|
10
|
+
ActiveRecord::Explainer.config.logger = Rails.logger
|
11
|
+
ActiveRecord::Explainer::Subscriber.attach_to :active_record
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'active_support/subscriber'
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module Explainer
|
5
|
+
class Subscriber < ActiveSupport::Subscriber # :nodoc:
|
6
|
+
def sql(event)
|
7
|
+
payload = event.payload
|
8
|
+
return if ignore_payload?(payload) || !ActiveRecord::Base.connection.supports_explain?
|
9
|
+
|
10
|
+
logger.debug exec_explain(sql: payload[:sql], binds: payload[:binds])
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
IGNORED_PAYLOADS = %w[SCHEMA EXPLAIN].freeze
|
16
|
+
EXPLAINED_SQLS = /\A\s*(with|select|update|delete|insert)\b/i.freeze
|
17
|
+
|
18
|
+
def ignore_payload?(payload)
|
19
|
+
payload[:cached] ||
|
20
|
+
IGNORED_PAYLOADS.include?(payload[:name]) ||
|
21
|
+
payload[:sql] !~ EXPLAINED_SQLS
|
22
|
+
end
|
23
|
+
|
24
|
+
def exec_explain(sql:, binds:)
|
25
|
+
msg = +"EXPLAIN for: #{sql}"
|
26
|
+
unless binds.empty?
|
27
|
+
msg << ' '
|
28
|
+
msg << binds.map { |attr| render_bind(attr) }.inspect
|
29
|
+
end
|
30
|
+
msg << "\n"
|
31
|
+
msg << ActiveRecord::Base.connection.explain(sql, binds)
|
32
|
+
msg
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_bind(attr)
|
36
|
+
value =
|
37
|
+
if attr.type.binary? && attr.value
|
38
|
+
"<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
|
39
|
+
else
|
40
|
+
ActiveRecord::Base.connection.type_cast(attr.value_for_database)
|
41
|
+
end
|
42
|
+
|
43
|
+
[attr.name, value]
|
44
|
+
end
|
45
|
+
|
46
|
+
def logger
|
47
|
+
ActiveRecord::Explainer.config.logger
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-explainer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshiyuki Hirano
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: activerecord-explainer puts SQL EXPLAIN of every queries to development
|
84
|
+
log
|
85
|
+
email:
|
86
|
+
- yhirano@me.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".rspec"
|
93
|
+
- ".rubocop.yml"
|
94
|
+
- ".travis.yml"
|
95
|
+
- Gemfile
|
96
|
+
- Gemfile.lock
|
97
|
+
- LICENSE.txt
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- activerecord-explainer.gemspec
|
101
|
+
- bin/console
|
102
|
+
- bin/setup
|
103
|
+
- lib/activerecord-explainer.rb
|
104
|
+
- lib/activerecord/explainer.rb
|
105
|
+
- lib/activerecord/explainer/config.rb
|
106
|
+
- lib/activerecord/explainer/railtie.rb
|
107
|
+
- lib/activerecord/explainer/subscriber.rb
|
108
|
+
- lib/activerecord/explainer/version.rb
|
109
|
+
homepage: https://github.com/yhirano55/activerecord-explainer
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 2.2.2
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: 1.8.11
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.0.3
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: activerecord-explainer puts SQL EXPLAIN of every queries to development log
|
132
|
+
test_files: []
|