sql_spy 0.3.0 → 0.5.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 +1 -1
- data/README.md +15 -6
- data/lib/sql_spy.rb +8 -3
- data/{sql-spy.gemspec → sql_spy.gemspec} +2 -2
- metadata +4 -6
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 287963cc1eff0c73997b9d8ef4fbcefebce45b2799a6406041e1ec100961cabf
|
4
|
+
data.tar.gz: a19370274527d317538a587055e6b9dd1924d3c19f1e9434e0ce7a48161fabd0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be06e6c37a51d3e8d60634a6a7be338ea4bcf66d01108ee84bc7d2c2f31995b4dc1d49eef51b9908733282dad7c3168eb0d38c643164570e941c80baec2ad2c7
|
7
|
+
data.tar.gz: df4712e2a6dc8f202d272f9faa3121a17935ace86ffb6d0c21b5f8c14cde2abee8cfec7aff6f934098146e17732f737f8c30b82bfe4410cbde22aa1e00676872
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -4,10 +4,19 @@
|
|
4
4
|
|
5
5
|
A gem to track all SQL queries performed inside a block of code.
|
6
6
|
|
7
|
-
|
7
|
+
With **SqlSpy** you can test:
|
8
|
+
|
9
|
+
- The total amount of queries performed by a piece of code
|
10
|
+
- The amount of queries performed on a specific table
|
11
|
+
- The type of these queries: *SELECT*, *INSERT*, *UPDATE*, *DELETE*
|
12
|
+
- The query duration
|
13
|
+
- The SQL produced by your *ActiveRecord* code
|
14
|
+
- N+1 queries
|
8
15
|
|
9
16
|
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
17
|
|
18
|
+
Check out [my blog post](https://lipanski.com/posts/activerecord-eager-loading#preventing-n1-regressions-with-tests) about writing controller tests with **SqlSpy** in order to prevent N+1 regressions.
|
19
|
+
|
11
20
|
## Usage
|
12
21
|
|
13
22
|
Add the gem to your `Gemfile`:
|
@@ -16,7 +25,7 @@ Add the gem to your `Gemfile`:
|
|
16
25
|
gem "sql_spy"
|
17
26
|
```
|
18
27
|
|
19
|
-
|
28
|
+
Install it:
|
20
29
|
|
21
30
|
```sh
|
22
31
|
bundle install
|
@@ -42,7 +51,7 @@ The `SqlSpy.track` method will return **an array containing all the queries perf
|
|
42
51
|
- `#update?`: Is this an *UPDATE* query?
|
43
52
|
- `#delete?`: Is this a *DELETE* query?
|
44
53
|
|
45
|
-
Here are some **ideas**
|
54
|
+
Here are some **ideas** of how you could use this:
|
46
55
|
|
47
56
|
```ruby
|
48
57
|
# Expect less than 5 queries
|
@@ -57,7 +66,7 @@ assert_equal 2, queries.select { |query| query.model_name == "Post" }.size
|
|
57
66
|
# None of the queries should be slower than 100ms
|
58
67
|
assert queries.none? { |query| query.duration > 100 }
|
59
68
|
|
60
|
-
# Fail on N+1 queries: expect
|
69
|
+
# Fail on N+1 queries: expect at most 1 query per table
|
61
70
|
queries_by_model = queries.group_by(&:model_name)
|
62
71
|
assert queries_by_model.none? { |model_name, queries| queries.count > 1 }
|
63
72
|
```
|
@@ -69,7 +78,7 @@ The gem is tested with Postgres 9.6 and SQLite 3.
|
|
69
78
|
To run tests with SQLite:
|
70
79
|
|
71
80
|
```sh
|
72
|
-
|
81
|
+
rake
|
73
82
|
```
|
74
83
|
|
75
84
|
To run tests with Postgres:
|
@@ -78,7 +87,7 @@ To run tests with Postgres:
|
|
78
87
|
# Create a test database
|
79
88
|
cretedb sql_spy_test
|
80
89
|
|
81
|
-
DATABASE_URL=postgres:///sql_spy_test
|
90
|
+
DATABASE_URL=postgres:///sql_spy_test rake
|
82
91
|
```
|
83
92
|
|
84
93
|
## License
|
data/lib/sql_spy.rb
CHANGED
@@ -37,11 +37,16 @@ module SqlSpy
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.track
|
40
|
+
|
40
41
|
tracker = Tracker.new
|
41
42
|
|
42
|
-
ActiveSupport::Notifications.subscribe("sql.active_record", tracker)
|
43
|
-
|
44
|
-
|
43
|
+
subscriber = ActiveSupport::Notifications.subscribe("sql.active_record", tracker)
|
44
|
+
|
45
|
+
begin
|
46
|
+
yield
|
47
|
+
ensure
|
48
|
+
ActiveSupport::Notifications.unsubscribe(subscriber)
|
49
|
+
end
|
45
50
|
|
46
51
|
tracker.queries
|
47
52
|
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "sql_spy"
|
3
|
-
spec.version = "0.
|
3
|
+
spec.version = "0.5.0"
|
4
4
|
spec.authors = ["Florin Lipan"]
|
5
5
|
spec.email = ["florinlipan@gmail.com"]
|
6
6
|
|
7
|
-
spec.summary = %q{A gem to track SQL queries
|
7
|
+
spec.summary = %q{A gem to track all SQL queries performed inside a block of code}
|
8
8
|
spec.homepage = "https://github.com/lipanski/sql-spy"
|
9
9
|
spec.license = "MIT"
|
10
10
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sql_spy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florin Lipan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -24,10 +24,8 @@ files:
|
|
24
24
|
- LICENSE.txt
|
25
25
|
- README.md
|
26
26
|
- Rakefile
|
27
|
-
- bin/console
|
28
|
-
- bin/setup
|
29
27
|
- lib/sql_spy.rb
|
30
|
-
-
|
28
|
+
- sql_spy.gemspec
|
31
29
|
homepage: https://github.com/lipanski/sql-spy
|
32
30
|
licenses:
|
33
31
|
- MIT
|
@@ -53,5 +51,5 @@ requirements: []
|
|
53
51
|
rubygems_version: 3.1.2
|
54
52
|
signing_key:
|
55
53
|
specification_version: 4
|
56
|
-
summary: A gem to track SQL queries
|
54
|
+
summary: A gem to track all SQL queries performed inside a block of code
|
57
55
|
test_files: []
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
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__)
|