sql_spy 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -1
- data/.travis.yml +8 -3
- data/Gemfile +1 -0
- data/Gemfile.lock +3 -1
- data/README.md +24 -9
- data/lib/sql_spy.rb +2 -2
- data/sql-spy.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98d9ae168c842d388db04918e4abf010ab0ece285bc4cad77d44b6606305c916
|
4
|
+
data.tar.gz: acfa349aeec0b8f003dd21b36286e923fb697709b7b07d4ca57abd6c1ff80823
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e464cc84c62c58935b5d3e8769b776a668edbfef35d51a7d0d0fc0f7de99b0545fcacc3a247dfc913ae4b2319c3bd5ce65856a581de3641a3173837f190aaf5a
|
7
|
+
data.tar.gz: 547c8b53ae652c54be10b3d9e77b6c4dd9a882957d64e11c3b7903df988eb5229d6e48599f88f1497d5a9c5d6cbd9fe5590d73a6e7b371a8cad5167cd9d194c5
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
|
1
|
+
dist: bionic
|
2
2
|
language: ruby
|
3
|
+
addons:
|
4
|
+
postgresql: "9.6"
|
5
|
+
rvm: 2.7.1
|
3
6
|
cache: bundler
|
4
|
-
rvm:
|
5
|
-
- 2.7.1
|
6
7
|
before_install: gem install bundler -v 2.1.4
|
8
|
+
before_script: createdb sql_spy_test
|
9
|
+
env:
|
10
|
+
- DATABASE_URL=sqlite3:///tmp/sql_spy_test.sqlite
|
11
|
+
- DATABASE_URL=postgres:///sql_spy_test
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sql_spy (0.
|
4
|
+
sql_spy (0.2.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -22,6 +22,7 @@ GEM
|
|
22
22
|
i18n (1.8.5)
|
23
23
|
concurrent-ruby (~> 1.0)
|
24
24
|
minitest (5.14.2)
|
25
|
+
pg (1.2.3)
|
25
26
|
rake (12.3.3)
|
26
27
|
sqlite3 (1.4.2)
|
27
28
|
thread_safe (0.3.6)
|
@@ -36,6 +37,7 @@ DEPENDENCIES
|
|
36
37
|
activerecord (~> 6.0)
|
37
38
|
byebug
|
38
39
|
minitest (~> 5.0)
|
40
|
+
pg
|
39
41
|
rake (~> 12.0)
|
40
42
|
sql_spy!
|
41
43
|
sqlite3
|
data/README.md
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
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
4
|
|
5
|
-
A gem to track all SQL queries performed inside a
|
5
|
+
A gem to track all SQL queries performed inside a block of code.
|
6
6
|
|
7
|
-
You can use **SqlSpy** to test
|
7
|
+
You can use **SqlSpy** to test the total amount of queries performed by a piece of code, the amount of queries per table, selects vs. inserts vs. updates vs. deletes, or query duration. You can also use it to validate and debug your SQL or to prevent N+1 queries.
|
8
8
|
|
9
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
10
|
|
@@ -28,15 +28,11 @@ Wrap the code you'd like to track inside `SqlSpy.track {}`:
|
|
28
28
|
require "sql_spy"
|
29
29
|
|
30
30
|
queries = SqlSpy.track do
|
31
|
-
|
32
|
-
users = User.limit(5).to_a
|
33
|
-
users.each { |user| user.posts.to_a }
|
31
|
+
# Some code that triggers ActiveRecord queries
|
34
32
|
end
|
35
33
|
```
|
36
34
|
|
37
|
-
The
|
38
|
-
|
39
|
-
Every query inside the Array exposes the following **methods**:
|
35
|
+
The `SqlSpy.track` method will return **an array containing all the queries performed inside the block**. Every object inside this array exposes the following **methods**:
|
40
36
|
|
41
37
|
- `#model_name`: The model name (e.g. "User").
|
42
38
|
- `#sql`: The SQL query that was performed.
|
@@ -46,7 +42,7 @@ Every query inside the Array exposes the following **methods**:
|
|
46
42
|
- `#update?`: Is this an *UPDATE* query?
|
47
43
|
- `#delete?`: Is this a *DELETE* query?
|
48
44
|
|
49
|
-
Here are some **ideas**
|
45
|
+
Here are some **ideas** for how you could use this:
|
50
46
|
|
51
47
|
```ruby
|
52
48
|
# Expect less than 5 queries
|
@@ -66,6 +62,25 @@ queries_by_model = queries.group_by(&:model_name)
|
|
66
62
|
assert queries_by_model.none? { |model_name, queries| queries.count > 1 }
|
67
63
|
```
|
68
64
|
|
65
|
+
## Development
|
66
|
+
|
67
|
+
The gem is tested with Postgres 9.6 and SQLite 3.
|
68
|
+
|
69
|
+
To run tests with SQLite:
|
70
|
+
|
71
|
+
```sh
|
72
|
+
bundle exec rake
|
73
|
+
```
|
74
|
+
|
75
|
+
To run tests with Postgres:
|
76
|
+
|
77
|
+
```sh
|
78
|
+
# Create a test database
|
79
|
+
cretedb sql_spy_test
|
80
|
+
|
81
|
+
DATABASE_URL=postgres:///sql_spy_test bundle exec rake
|
82
|
+
```
|
83
|
+
|
69
84
|
## License
|
70
85
|
|
71
86
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/sql_spy.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module SqlSpy
|
2
2
|
Query = Struct.new(:name, :sql, :duration) do
|
3
3
|
def model_name
|
4
|
-
name.split.first
|
4
|
+
String(name).split.first
|
5
5
|
end
|
6
6
|
|
7
7
|
def select?
|
@@ -29,7 +29,7 @@ module SqlSpy
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def call(_name, start, finish, _message_id, values)
|
32
|
-
return if
|
32
|
+
return if IGNORED_NAMES.include?(values[:name])
|
33
33
|
return if values[:cached]
|
34
34
|
|
35
35
|
queries << Query.new(values[:name], values[:sql], (finish - start) * 1000)
|
data/sql-spy.gemspec
CHANGED
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.3.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: 2020-10-
|
11
|
+
date: 2020-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|