activerecord-analyze 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +30 -0
- data/README.md +5 -3
- data/activerecord-analyze.gemspec +3 -0
- data/docker-compose.yml.sample +11 -0
- data/lib/activerecord-analyze/main.rb +3 -1
- data/lib/activerecord-analyze/version.rb +1 -1
- data/query-plan.png +0 -0
- data/spec/main_spec.rb +43 -0
- data/spec/migrations/create_users_migration.rb +10 -0
- data/spec/spec_helper.rb +8 -0
- metadata +54 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef55b677ccdab275d0b76db680c55e8c8534b7e03245dc76f622eca81cb3b244
|
4
|
+
data.tar.gz: 3ee9e2dc7ce0251cab913d79ee3c5b5760b571d27bdf98b132cb8d8751c7c7da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a1057165667e384853cc9fc83e98b4c98377c2af41057cbe52427cd7a25b8fff99f50321435e4bab1645be97e2d989e026fe3ba85d0d825e53992d1d373ac3b
|
7
|
+
data.tar.gz: 43019fbfa8dff77b956e62eba3750f63b4925eb179876348750414a77c91c0406892a12a3eb719d51e100b8e00e7274a7b41db0054c06d9a061a8887b4be807a
|
@@ -0,0 +1,30 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
test:
|
4
|
+
docker:
|
5
|
+
- image: circleci/ruby:2.6.5
|
6
|
+
environment:
|
7
|
+
DATABASE_URL: postgresql://postgres:secret@localhost:5432/activerecord-analyze-test
|
8
|
+
- image: circleci/postgres:11.5
|
9
|
+
environment:
|
10
|
+
POSTGRES_USER: postgres
|
11
|
+
POSTGRES_DB: activerecord-analyze-test
|
12
|
+
POSTGRES_PASSWORD: secret
|
13
|
+
parallelism: 1
|
14
|
+
steps:
|
15
|
+
- checkout
|
16
|
+
- run: gem update --system
|
17
|
+
- run: gem install bundler
|
18
|
+
- run: bundle install --path vendor/bundle
|
19
|
+
- run: sudo apt-get update
|
20
|
+
- run: sudo apt install postgresql-client-11
|
21
|
+
- run: dockerize -wait tcp://localhost:5432 -timeout 1m
|
22
|
+
- run:
|
23
|
+
name: Run specs
|
24
|
+
command: |
|
25
|
+
bundle exec rspec spec/
|
26
|
+
workflows:
|
27
|
+
version: 2
|
28
|
+
test:
|
29
|
+
jobs:
|
30
|
+
- test
|
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
-
# ActiveRecord Analyze
|
1
|
+
# ActiveRecord Analyze [![Gem Version](https://badge.fury.io/rb/activerecord-analyze.svg)](https://badge.fury.io/rb/ruby-pg-extras) [![CircleCI](https://circleci.com/gh/pawurb/activerecord-analyze.svg?style=svg)](https://circleci.com/gh/pawurb/activerecord-analyze)
|
2
2
|
|
3
3
|
This gem adds an `analyze` method to Active Record query objects. It executes `EXPLAIN ANALYZE` on a query SQL.
|
4
4
|
|
5
5
|
You can check out this blog post for more info on how to [debug and fix slow queries in Rails apps](https://pawelurbanek.com/slow-rails-queries).
|
6
6
|
|
7
|
+
The following `format` options are supported `:json, :hash, :yaml, :text, :xml`. Especially the `:json` format is useful because it let's you visualize a query plan using [a visualizer tool](https://tatiyants.com/pev/#/plans/new).
|
8
|
+
|
9
|
+
![PG Query visualizer plan](https://raw.githubusercontent.com/pawurb/activerecord-analyze/master/query-plan.png)
|
10
|
+
|
7
11
|
## Installation
|
8
12
|
|
9
13
|
In your Gemfile:
|
@@ -92,8 +96,6 @@ User.all.analyze(
|
|
92
96
|
|
93
97
|
```
|
94
98
|
|
95
|
-
The following `format` options are supported `:json, :hash, :yaml, :text, :xml`. Especially the `:json` format is useful because it allows you to visualize a query plan using [a visualizer tool](https://tatiyants.com/pev/#/plans/new).
|
96
|
-
|
97
99
|
Optionally you can disable running the `ANALYZE` query and only generate the plan:
|
98
100
|
|
99
101
|
```ruby
|
@@ -67,7 +67,9 @@ module ActiveRecord
|
|
67
67
|
def analyze(opts = {})
|
68
68
|
res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
|
69
69
|
if [:json, :hash].include?(opts[:format])
|
70
|
-
|
70
|
+
start = res.index("[")
|
71
|
+
finish = res.rindex("]")
|
72
|
+
raw_json = res.slice(start, finish - start + 1)
|
71
73
|
|
72
74
|
if opts[:format] == :json
|
73
75
|
JSON.parse(raw_json).to_json
|
data/query-plan.png
ADDED
Binary file
|
data/spec/main_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require "migrations/create_users_migration.rb"
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base; end
|
7
|
+
|
8
|
+
describe "ActiveRecord analyze" do
|
9
|
+
before(:all) do
|
10
|
+
ActiveRecord::Base.establish_connection(
|
11
|
+
ENV.fetch("DATABASE_URL")
|
12
|
+
)
|
13
|
+
|
14
|
+
@schema_migration = ActiveRecord::Base.connection.schema_migration
|
15
|
+
ActiveRecord::Migrator.new(:up, [CreateUsers.new], @schema_migration).migrate
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "no opts" do
|
19
|
+
it "works" do
|
20
|
+
expect do
|
21
|
+
User.all.analyze
|
22
|
+
end.not_to raise_error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "format json" do
|
27
|
+
it "works" do
|
28
|
+
result = User.all.analyze(format: :json)
|
29
|
+
expect(JSON.parse(result)[0].keys.sort).to eq [
|
30
|
+
"Execution Time", "Plan", "Planning Time", "Triggers"
|
31
|
+
]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "format hash" do
|
36
|
+
it "works" do
|
37
|
+
result = User.all.analyze(format: :hash)
|
38
|
+
expect(result[0].keys.sort).to eq [
|
39
|
+
"Execution Time", "Plan", "Planning Time", "Triggers"
|
40
|
+
]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-analyze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,48 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 5.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
27
69
|
description: ' Gem adds an "analyze" method to all Active Record query objects. Compatible
|
28
70
|
with PostgreSQL database. '
|
29
71
|
email:
|
@@ -32,15 +74,21 @@ executables: []
|
|
32
74
|
extensions: []
|
33
75
|
extra_rdoc_files: []
|
34
76
|
files:
|
77
|
+
- ".circleci/config.yml"
|
35
78
|
- ".gitignore"
|
36
79
|
- Gemfile
|
37
80
|
- LICENSE.txt
|
38
81
|
- README.md
|
39
82
|
- Rakefile
|
40
83
|
- activerecord-analyze.gemspec
|
84
|
+
- docker-compose.yml.sample
|
41
85
|
- lib/activerecord-analyze.rb
|
42
86
|
- lib/activerecord-analyze/main.rb
|
43
87
|
- lib/activerecord-analyze/version.rb
|
88
|
+
- query-plan.png
|
89
|
+
- spec/main_spec.rb
|
90
|
+
- spec/migrations/create_users_migration.rb
|
91
|
+
- spec/spec_helper.rb
|
44
92
|
homepage: http://github.com/pawurb/activerecord-analyze
|
45
93
|
licenses:
|
46
94
|
- MIT
|
@@ -64,4 +112,7 @@ rubygems_version: 3.1.4
|
|
64
112
|
signing_key:
|
65
113
|
specification_version: 4
|
66
114
|
summary: Add EXPLAIN ANALYZE to Active Record query objects
|
67
|
-
test_files:
|
115
|
+
test_files:
|
116
|
+
- spec/main_spec.rb
|
117
|
+
- spec/migrations/create_users_migration.rb
|
118
|
+
- spec/spec_helper.rb
|