n_1_finder 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Readme.md +35 -24
- data/lib/n_1_finder/version.rb +1 -1
- data/spec/.env.example +7 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/orm_helpers.rb +23 -41
- metadata +2 -2
- data/spec/secrets.yml.example +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab7427d8a228787f6c0ec69a45911a03c2db8d88
|
4
|
+
data.tar.gz: e2466e6c7fe33f5e262938fd74e465eda7e91c6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c45009c7542f01b595f26450c890e85b3904b1d4635d3df4043b66388f145dc2423c1226ea045cadeed2034b922a772775a937fedc0a676237c5bee510acbae6
|
7
|
+
data.tar.gz: d3037ad0f27c81ff23bfbc9458708cc8a28d9cba4d0e54384f581135d7f83a3492c93dc1d4a7ff9d26cb745013dd476772006860efedd610b5a4814203bab3f3
|
data/Readme.md
CHANGED
@@ -1,20 +1,22 @@
|
|
1
1
|
# N+1 Finder
|
2
|
-
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/n_1_finder.svg)](https://badge.fury.io/rb/n_1_finder)
|
3
|
+
[![Build Status](https://api.travis-ci.org/aglushkov/n1_finder.svg?branch=master)](https://travis-ci.org/aglushkov/n1_finder)
|
4
|
+
[![Inline docs](http://inch-ci.org/github/aglushkov/n1_finder.svg?branch=master)](http://inch-ci.org/github/aglushkov/n1_finder)
|
5
|
+
|
6
|
+
This gem helps to find N+1 queries.
|
3
7
|
|
4
8
|
It works with `ActiveRecord` and `Sequel`.
|
5
9
|
|
6
10
|
And tested with `postgresql`, `mysql` and `sqlite`.
|
7
11
|
|
8
12
|
## Installation
|
9
|
-
|
10
|
-
|
11
|
-
Without Gemfile: `gem install n1_finder`
|
13
|
+
`gem 'n_1_finder', group: :development`
|
12
14
|
|
13
15
|
## Configuration
|
14
16
|
### Logger
|
15
17
|
Default is `Logger.new(STDOUT)`
|
16
18
|
|
17
|
-
|
19
|
+
Logger can be any instance of `Logger` class.
|
18
20
|
|
19
21
|
```ruby
|
20
22
|
N1Finder.logger = Logger.new('log/n1.log')
|
@@ -25,14 +27,29 @@ Default is `:active_record` if you have activerecord gem installed.
|
|
25
27
|
|
26
28
|
Default is `:sequel` if you have sequel gem installed.
|
27
29
|
|
28
|
-
|
30
|
+
Allowed values are `:active_record` and `:sequel`
|
31
|
+
|
29
32
|
```ruby
|
30
33
|
N1Finder.orm = :active_record
|
31
34
|
```
|
32
35
|
|
33
36
|
## Using
|
37
|
+
Include middleware to your Rack app:
|
38
|
+
```ruby
|
39
|
+
# Rails
|
40
|
+
class Application < Rails::Application
|
41
|
+
config.middleware.use(N1Finder::Middleware)
|
42
|
+
...
|
43
|
+
# Grape
|
44
|
+
class YourAPI < Grape::API
|
45
|
+
use N1Finder::Middleware
|
46
|
+
...
|
47
|
+
# Padrino
|
48
|
+
class YourAPP < Padrino::Application
|
49
|
+
use N1Finder::Middleware
|
50
|
+
```
|
34
51
|
|
35
|
-
|
52
|
+
Or use it directly:
|
36
53
|
```ruby
|
37
54
|
N1Finder.find do
|
38
55
|
User.all.map { |user| user.comments.to_a }
|
@@ -51,24 +68,18 @@ N+1 QUERY DETECTED:
|
|
51
68
|
SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = $1, user_id = 947
|
52
69
|
```
|
53
70
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
```ruby
|
59
|
-
class Application < Rails::Application
|
60
|
-
config.middleware.use(N1Finder::Middleware)
|
71
|
+
## Running tests
|
72
|
+
- Copy `spec/.env.example` file to `spec/.env` and set database credentials.
|
73
|
+
```bash
|
74
|
+
cp spec/.env.example spec/.env
|
61
75
|
```
|
62
76
|
|
63
|
-
|
64
|
-
```
|
65
|
-
|
66
|
-
use N1Finder::Middleware
|
67
|
-
```
|
68
|
-
|
69
|
-
#### Padrino
|
70
|
-
```ruby
|
71
|
-
class YourAPP < Padrino::Application
|
72
|
-
use N1Finder::Middleware
|
77
|
+
- Load this variables into your system.
|
78
|
+
```bash
|
79
|
+
source spec/.env
|
73
80
|
```
|
74
81
|
|
82
|
+
- Run tests.
|
83
|
+
```bash
|
84
|
+
rspec
|
85
|
+
```
|
data/lib/n_1_finder/version.rb
CHANGED
data/spec/.env.example
ADDED
data/spec/spec_helper.rb
CHANGED
data/spec/support/orm_helpers.rb
CHANGED
@@ -1,58 +1,40 @@
|
|
1
|
-
|
1
|
+
# rubocop:disable Metrics/LineLength
|
2
2
|
|
3
3
|
module ORMHelpers
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
database: ':memory:'
|
11
|
-
},
|
12
|
-
pg: {
|
13
|
-
adapter: 'postgresql',
|
14
|
-
host: 'localhost',
|
15
|
-
username: SECRETS[:pg][:username].to_s,
|
16
|
-
password: SECRETS[:pg][:password].to_s,
|
17
|
-
database: SECRETS[:pg][:database].to_s
|
4
|
+
DB_PARAMS = Hash.new { |hash, _key| hash['mri'] }.update(
|
5
|
+
'mri' => {
|
6
|
+
active_record: {
|
7
|
+
sqlite: 'sqlite3::memory:',
|
8
|
+
pg: "postgres://#{ENV['PG_USERNAME']}:#{ENV['PG_PASSWORD']}@localhost/#{ENV['PG_DATABASE']}",
|
9
|
+
mysql: "mysql2://#{ENV['MYSQL_USERNAME']}:#{ENV['MYSQL_PASSWORD']}@localhost/#{ENV['MYSQL_DATABASE']}"
|
18
10
|
},
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
password: SECRETS[:mysql][:password].to_s,
|
24
|
-
database: SECRETS[:mysql][:database].to_s
|
11
|
+
sequel: {
|
12
|
+
sqlite: 'sqlite::memory:',
|
13
|
+
pg: "postgres://#{ENV['PG_USERNAME']}:#{ENV['PG_PASSWORD']}@localhost/#{ENV['PG_DATABASE']}",
|
14
|
+
mysql: "mysql2://#{ENV['MYSQL_USERNAME']}:#{ENV['MYSQL_PASSWORD']}@localhost/#{ENV['MYSQL_DATABASE']}"
|
25
15
|
}
|
26
16
|
},
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
pg: {
|
33
|
-
adapter: 'postgres',
|
34
|
-
host: 'localhost',
|
35
|
-
username: SECRETS[:pg][:username].to_s,
|
36
|
-
password: SECRETS[:pg][:password].to_s,
|
37
|
-
database: SECRETS[:pg][:database].to_s
|
17
|
+
'java' => {
|
18
|
+
active_record: {
|
19
|
+
sqlite: 'sqlite3::memory:',
|
20
|
+
pg: "postgresql://#{ENV['PG_USERNAME']}:#{ENV['PG_PASSWORD']}@localhost/#{ENV['PG_DATABASE']}",
|
21
|
+
mysql: "mysql2://#{ENV['MYSQL_USERNAME']}:#{ENV['MYSQL_PASSWORD']}@localhost/#{ENV['MYSQL_DATABASE']}"
|
38
22
|
},
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
password: SECRETS[:mysql][:password].to_s,
|
44
|
-
database: SECRETS[:mysql][:database].to_s
|
23
|
+
sequel: {
|
24
|
+
sqlite: 'jdbc:sqlite::memory:',
|
25
|
+
pg: "jdbc:postgresql://localhost/#{ENV['PG_DATABASE']}?user=#{ENV['PG_USERNAME']}&password=#{ENV['PG_PASSWORD']}",
|
26
|
+
mysql: "jdbc:mysql://localhost/#{ENV['MYSQL_DATABASE']}?user=#{ENV['MYSQL_USERNAME']}&password=#{ENV['MYSQL_PASSWORD']}"
|
45
27
|
}
|
46
28
|
}
|
47
|
-
|
29
|
+
)
|
48
30
|
|
49
31
|
def connect_sequel(adapter)
|
50
|
-
Sequel::Model.db = Sequel.connect(DB_PARAMS[:sequel][adapter])
|
32
|
+
Sequel::Model.db = Sequel.connect(DB_PARAMS[RUBY_PLATFORM][:sequel][adapter])
|
51
33
|
TestSequelMigration.up
|
52
34
|
end
|
53
35
|
|
54
36
|
def connect_active_record(adapter)
|
55
|
-
ActiveRecord::Base.establish_connection(DB_PARAMS[:active_record][adapter])
|
37
|
+
ActiveRecord::Base.establish_connection(DB_PARAMS[RUBY_PLATFORM][:active_record][adapter])
|
56
38
|
silence_stream(STDOUT) { TestActiveRecordMigration.up }
|
57
39
|
end
|
58
40
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: n_1_finder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrey Glushkov
|
@@ -28,6 +28,7 @@ files:
|
|
28
28
|
- lib/n_1_finder/query.rb
|
29
29
|
- lib/n_1_finder/storage.rb
|
30
30
|
- lib/n_1_finder/version.rb
|
31
|
+
- spec/.env.example
|
31
32
|
- spec/n_1_finder/adapters/active_record_adapter_spec.rb
|
32
33
|
- spec/n_1_finder/adapters/null_adapter_spec.rb
|
33
34
|
- spec/n_1_finder/adapters/sequel_adapter_spec.rb
|
@@ -43,7 +44,6 @@ files:
|
|
43
44
|
- spec/n_1_finder/query_spec.rb
|
44
45
|
- spec/n_1_finder/storage_spec.rb
|
45
46
|
- spec/n_1_finder_spec.rb
|
46
|
-
- spec/secrets.yml.example
|
47
47
|
- spec/shared_examples/adapter.rb
|
48
48
|
- spec/spec_helper.rb
|
49
49
|
- spec/support/n_1_helpers.rb
|