sql_spy 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +44 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sql_spy.rb +48 -0
- data/sql-spy.gemspec +19 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2c4bb9268e7009228c6ac7528e90bfed389d8024d7d96998eda16e3b7e5f8f4
|
4
|
+
data.tar.gz: 90f4a5afca785eccfcb6a1223c560692f6437a66010233ab554f13eaace0c33f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e0564cf944ffaf168e3b9a074eec8ffcc6c04a5e6ee00be00e5eedef4c59c28c9ee052f4decf9c546dc886adea97afa30b0008fdd6fcd07884567fd4144b0cd6
|
7
|
+
data.tar.gz: d7c9ef7a7566bb9e605e921f28b2ae5c6b9907746efd2c030345b3d5c2e629086adc2e7e3d1ff3dd970262cb24b9f6a3cb110f4bb0cc8541beecffdb4c7046a2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
sql_spy (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (6.0.3.3)
|
10
|
+
activesupport (= 6.0.3.3)
|
11
|
+
activerecord (6.0.3.3)
|
12
|
+
activemodel (= 6.0.3.3)
|
13
|
+
activesupport (= 6.0.3.3)
|
14
|
+
activesupport (6.0.3.3)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 0.7, < 2)
|
17
|
+
minitest (~> 5.1)
|
18
|
+
tzinfo (~> 1.1)
|
19
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
20
|
+
byebug (11.1.3)
|
21
|
+
concurrent-ruby (1.1.7)
|
22
|
+
i18n (1.8.5)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
minitest (5.14.2)
|
25
|
+
rake (12.3.3)
|
26
|
+
sqlite3 (1.4.2)
|
27
|
+
thread_safe (0.3.6)
|
28
|
+
tzinfo (1.2.7)
|
29
|
+
thread_safe (~> 0.1)
|
30
|
+
zeitwerk (2.4.0)
|
31
|
+
|
32
|
+
PLATFORMS
|
33
|
+
ruby
|
34
|
+
|
35
|
+
DEPENDENCIES
|
36
|
+
activerecord (~> 6.0)
|
37
|
+
byebug
|
38
|
+
minitest (~> 5.0)
|
39
|
+
rake (~> 12.0)
|
40
|
+
sql_spy!
|
41
|
+
sqlite3
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Florin Lipan
|
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,71 @@
|
|
1
|
+
# SqlSpy
|
2
|
+
|
3
|
+
<a href="https://travis-ci.com/github/lipanski/sql-spy"><img src="https://api.travis-ci.com/lipanski/sql-spy.svg?branch=master"></a>
|
4
|
+
|
5
|
+
A gem to track all SQL queries performed inside a given block.
|
6
|
+
|
7
|
+
You can use **SqlSpy** to test various things, like the total amount of queries performed by a piece of code, the amount of queries per table, inserts vs. updates or even N+1 queries. You can also use it to validate and debug your SQL.
|
8
|
+
|
9
|
+
The implementation is inspired by how [ActiveRecord is tested](https://github.com/rails/rails/blob/6-0-stable/activerecord/test/cases/test_case.rb).
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Add the gem to your `Gemfile`:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "sql_spy"
|
17
|
+
```
|
18
|
+
|
19
|
+
...and install it with:
|
20
|
+
|
21
|
+
```sh
|
22
|
+
bundle install
|
23
|
+
```
|
24
|
+
|
25
|
+
Wrap the code you'd like to track inside `SqlSpy.track {}`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require "sql_spy"
|
29
|
+
|
30
|
+
queries = SqlSpy.track do
|
31
|
+
User.create(name: "Mario")
|
32
|
+
users = User.limit(5).to_a
|
33
|
+
users.each { |user| user.posts.to_a }
|
34
|
+
end
|
35
|
+
```
|
36
|
+
|
37
|
+
The return value of this block is **an Array containing all the queries performed inside the block**.
|
38
|
+
|
39
|
+
Every query inside the Array exposes the following **methods**:
|
40
|
+
|
41
|
+
- `#model_name`: The model name (e.g. "User").
|
42
|
+
- `#sql`: The SQL query that was performed.
|
43
|
+
- `#duration`: The duration of the query in milliseconds.
|
44
|
+
- `#select?`: Is this a *SELECT* query?
|
45
|
+
- `#insert?`: Is this an *INSERT* query?
|
46
|
+
- `#update?`: Is this an *UPDATE* query?
|
47
|
+
- `#delete?`: Is this a *DELETE* query?
|
48
|
+
|
49
|
+
Here are some **ideas** of how you could use this:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
# Expect less than 5 queries
|
53
|
+
assert queries.count < 5
|
54
|
+
|
55
|
+
# Expect 1 INSERT query
|
56
|
+
assert_equal 1, queries.select(&:insert?).size
|
57
|
+
|
58
|
+
# Expect 2 queries to the posts table
|
59
|
+
assert_equal 2, queries.select { |query| query.model_name == "Post" }.size
|
60
|
+
|
61
|
+
# None of the queries should be slower than 100ms
|
62
|
+
assert queries.none? { |query| query.duration > 100 }
|
63
|
+
|
64
|
+
# Fail on N+1 queries: expect no more than 1 query per table
|
65
|
+
queries_by_model = queries.group_by(&:model_name)
|
66
|
+
assert queries_by_model.none? { |model_name, queries| queries.count > 1 }
|
67
|
+
```
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sql/spy"
|
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
data/lib/sql_spy.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module SqlSpy
|
2
|
+
Query = Struct.new(:name, :sql, :duration) do
|
3
|
+
def model_name
|
4
|
+
name.split.first
|
5
|
+
end
|
6
|
+
|
7
|
+
def select?
|
8
|
+
sql.starts_with?("SELECT")
|
9
|
+
end
|
10
|
+
|
11
|
+
def insert?
|
12
|
+
sql.starts_with?("INSERT")
|
13
|
+
end
|
14
|
+
|
15
|
+
def update?
|
16
|
+
sql.starts_with?("UPDATE")
|
17
|
+
end
|
18
|
+
|
19
|
+
def delete?
|
20
|
+
sql.starts_with?("DELETE")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Tracker
|
25
|
+
IGNORED_NAMES = %w(SCHEMA)
|
26
|
+
|
27
|
+
def queries
|
28
|
+
@queries ||= []
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(_name, start, finish, _message_id, values)
|
32
|
+
return if values[:name].nil? || IGNORED_NAMES.include?(values[:name])
|
33
|
+
return if values[:cached]
|
34
|
+
|
35
|
+
queries << Query.new(values[:name], values[:sql], (finish - start) * 1000)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.track
|
40
|
+
tracker = Tracker.new
|
41
|
+
|
42
|
+
ActiveSupport::Notifications.subscribe("sql.active_record", tracker)
|
43
|
+
yield
|
44
|
+
ActiveSupport::Notifications.unsubscribe(tracker)
|
45
|
+
|
46
|
+
tracker.queries
|
47
|
+
end
|
48
|
+
end
|
data/sql-spy.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "sql_spy"
|
3
|
+
spec.version = "0.2.0"
|
4
|
+
spec.authors = ["Florin Lipan"]
|
5
|
+
spec.email = ["florinlipan@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = %q{A gem to track SQL queries triggered by a particular block of code}
|
8
|
+
spec.homepage = "https://github.com/lipanski/sql-spy"
|
9
|
+
spec.license = "MIT"
|
10
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
11
|
+
|
12
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
13
|
+
spec.metadata["source_code_uri"] = "https://github.com/lipanski/sql-spy"
|
14
|
+
spec.metadata["changelog_uri"] = "https://github.com/lipanski/sql-spy/releases"
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sql_spy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florin Lipan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- florinlipan@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".travis.yml"
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/sql_spy.rb
|
30
|
+
- sql-spy.gemspec
|
31
|
+
homepage: https://github.com/lipanski/sql-spy
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
metadata:
|
35
|
+
homepage_uri: https://github.com/lipanski/sql-spy
|
36
|
+
source_code_uri: https://github.com/lipanski/sql-spy
|
37
|
+
changelog_uri: https://github.com/lipanski/sql-spy/releases
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.3.0
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.1.2
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: A gem to track SQL queries triggered by a particular block of code
|
57
|
+
test_files: []
|