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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2c4bb9268e7009228c6ac7528e90bfed389d8024d7d96998eda16e3b7e5f8f4
4
- data.tar.gz: 90f4a5afca785eccfcb6a1223c560692f6437a66010233ab554f13eaace0c33f
3
+ metadata.gz: 98d9ae168c842d388db04918e4abf010ab0ece285bc4cad77d44b6606305c916
4
+ data.tar.gz: acfa349aeec0b8f003dd21b36286e923fb697709b7b07d4ca57abd6c1ff80823
5
5
  SHA512:
6
- metadata.gz: e0564cf944ffaf168e3b9a074eec8ffcc6c04a5e6ee00be00e5eedef4c59c28c9ee052f4decf9c546dc886adea97afa30b0008fdd6fcd07884567fd4144b0cd6
7
- data.tar.gz: d7c9ef7a7566bb9e605e921f28b2ae5c6b9907746efd2c030345b3d5c2e629086adc2e7e3d1ff3dd970262cb24b9f6a3cb110f4bb0cc8541beecffdb4c7046a2
6
+ metadata.gz: e464cc84c62c58935b5d3e8769b776a668edbfef35d51a7d0d0fc0f7de99b0545fcacc3a247dfc913ae4b2319c3bd5ce65856a581de3641a3173837f190aaf5a
7
+ data.tar.gz: 547c8b53ae652c54be10b3d9e77b6c4dd9a882957d64e11c3b7903df988eb5229d6e48599f88f1497d5a9c5d6cbd9fe5590d73a6e7b371a8cad5167cd9d194c5
data/.gitignore CHANGED
@@ -8,5 +8,5 @@
8
8
  /tmp/
9
9
 
10
10
  .byebug_history
11
- test.sqlite3
11
+ test.sqlite
12
12
 
@@ -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
@@ -5,5 +5,6 @@ gemspec
5
5
  gem "rake", "~> 12.0"
6
6
  gem "minitest", "~> 5.0"
7
7
  gem "activerecord", "~> 6.0"
8
+ gem "pg"
8
9
  gem "sqlite3"
9
10
  gem "byebug"
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sql_spy (0.1.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 given block.
5
+ A gem to track all SQL queries performed inside a block of code.
6
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.
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
- User.create(name: "Mario")
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 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**:
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** of how you could use this:
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).
@@ -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 values[:name].nil? || IGNORED_NAMES.include?(values[:name])
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)
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "sql_spy"
3
- spec.version = "0.2.0"
3
+ spec.version = "0.3.0"
4
4
  spec.authors = ["Florin Lipan"]
5
5
  spec.email = ["florinlipan@gmail.com"]
6
6
 
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.2.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-05 00:00:00.000000000 Z
11
+ date: 2020-10-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: