activerecord-analyze 0.6.0 → 0.9.1

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: ef55b677ccdab275d0b76db680c55e8c8534b7e03245dc76f622eca81cb3b244
4
- data.tar.gz: 3ee9e2dc7ce0251cab913d79ee3c5b5760b571d27bdf98b132cb8d8751c7c7da
3
+ metadata.gz: '09f3c691a676a55f45059cbfd0e12161e42f7ab87acdc834c1f496a2ceae1c1f'
4
+ data.tar.gz: c83a3d1bb42bdbbb82ca80f00acc55d420de1c7589660f8fd72f103f19aca0c4
5
5
  SHA512:
6
- metadata.gz: 5a1057165667e384853cc9fc83e98b4c98377c2af41057cbe52427cd7a25b8fff99f50321435e4bab1645be97e2d989e026fe3ba85d0d825e53992d1d373ac3b
7
- data.tar.gz: 43019fbfa8dff77b956e62eba3750f63b4925eb179876348750414a77c91c0406892a12a3eb719d51e100b8e00e7274a7b41db0054c06d9a061a8887b4be807a
6
+ metadata.gz: 96e5ba8e107b9d005f59e6ffe3ff0925b872c753b51ef9deee505919d415e3165a67d5bfe1cffa6f29dbd34eb6726e65f93f14d52389bcdd736edc636c25d0e5
7
+ data.tar.gz: 957325a0f295e28c76689b504a92a4d0aa636965f6a71326004c3662256aef769ca24bf75bd81a823d2fd3281b9f49d805e0a63dedda3bc5c392f7790cfd0cde
data/README.md CHANGED
@@ -1,4 +1,4 @@
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)
1
+ # ActiveRecord Analyze [![Gem Version](https://badge.fury.io/rb/activerecord-analyze.svg)](https://badge.fury.io/rb/activerecord-analyze) [![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
 
@@ -23,21 +23,21 @@ gem 'activerecord-analyze'
23
23
  The `analyze` method supports the following EXPLAIN query options ([PostgreSQL docs reference](https://www.postgresql.org/docs/12/sql-explain.html)):
24
24
 
25
25
  ```
26
- VERBOSE [ boolean ]
27
- COSTS [ boolean ]
28
- SETTINGS [ boolean ]
29
- BUFFERS [ boolean ]
30
- TIMING [ boolean ]
31
- SUMMARY [ boolean ]
32
- FORMAT { TEXT | XML | JSON | YAML }
26
+ buffers: [ boolean ]
27
+ verbose: [ boolean ]
28
+ costs: [ boolean ]
29
+ settings: [ boolean ]
30
+ timing: [ boolean ]
31
+ summary: [ boolean ]
32
+ format: { :text | :json | :xml | :yaml | :pretty_json }
33
33
  ```
34
34
 
35
35
  You can execute it like that:
36
36
 
37
37
  ```ruby
38
38
 
39
- User.all.analyze(
40
- format: :json,
39
+ puts User.all.analyze(
40
+ format: :pretty_json, # :pretty_json format option generates a formatted JSON output
41
41
  verbose: true,
42
42
  costs: true,
43
43
  settings: true,
@@ -6,15 +6,19 @@ module ActiveRecord
6
6
  format_sql = if fmt = opts[:format].presence
7
7
  case fmt
8
8
  when :json
9
- "FORMAT JSON,"
9
+ "FORMAT JSON, "
10
10
  when :hash
11
- "FORMAT JSON,"
11
+ "FORMAT JSON, "
12
+ when :pretty_json
13
+ "FORMAT JSON, "
12
14
  when :yaml
13
- "FORMAT YAML,"
15
+ "FORMAT YAML, "
14
16
  when :text
15
- "FORMAT TEXT,"
17
+ "FORMAT TEXT, "
16
18
  when :xml
17
- "FORMAT XML,"
19
+ "FORMAT XML, "
20
+ else
21
+ ""
18
22
  end
19
23
  end
20
24
 
@@ -48,7 +52,7 @@ module ActiveRecord
48
52
  "ANALYZE"
49
53
  end
50
54
 
51
- opts_sql = "(#{format_sql} #{analyze_sql}#{verbose_sql}#{costs_sql}#{settings_sql}#{buffers_sql}#{timing_sql}#{summary_sql})"
55
+ opts_sql = "(#{format_sql}#{analyze_sql}#{verbose_sql}#{costs_sql}#{settings_sql}#{buffers_sql}#{timing_sql}#{summary_sql})"
52
56
  .strip.gsub(/\s+/, " ")
53
57
  .gsub(/\(\s?\s?\s?,/, "(")
54
58
  .gsub(/\s,\s/, " ")
@@ -66,8 +70,8 @@ module ActiveRecord
66
70
  class Relation
67
71
  def analyze(opts = {})
68
72
  res = exec_analyze(collecting_queries_for_explain { exec_queries }, opts)
69
- if [:json, :hash].include?(opts[:format])
70
- start = res.index("[")
73
+ if [:json, :hash, :pretty_json].include?(opts[:format])
74
+ start = res.index("[\n")
71
75
  finish = res.rindex("]")
72
76
  raw_json = res.slice(start, finish - start + 1)
73
77
 
@@ -75,6 +79,8 @@ module ActiveRecord
75
79
  JSON.parse(raw_json).to_json
76
80
  elsif opts[:format] == :hash
77
81
  JSON.parse(raw_json)
82
+ elsif opts[:format] == :pretty_json
83
+ JSON.pretty_generate(JSON.parse(raw_json))
78
84
  end
79
85
  else
80
86
  res
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordAnalyze
2
- VERSION = "0.6.0"
2
+ VERSION = "0.9.1"
3
3
  end
data/spec/main_spec.rb CHANGED
@@ -40,4 +40,27 @@ describe "ActiveRecord analyze" do
40
40
  ]
41
41
  end
42
42
  end
43
+
44
+ describe "format pretty" do
45
+ it "works" do
46
+ result = User.all.analyze(format: :pretty_json)
47
+ expect(JSON.parse(result)[0].keys.sort).to eq [
48
+ "Execution Time", "Plan", "Planning Time", "Triggers"
49
+ ]
50
+ end
51
+ end
52
+
53
+ describe "supports options" do
54
+ it "works" do
55
+ result = User.all.limit(10).where.not(email: nil).analyze(
56
+ format: :hash,
57
+ costs: true,
58
+ timing: true,
59
+ summary: true
60
+ )
61
+ expect(result[0].keys.sort).to eq [
62
+ "Execution Time", "Plan", "Planning Time", "Triggers"
63
+ ]
64
+ end
65
+ end
43
66
  end
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.6.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-07 00:00:00.000000000 Z
11
+ date: 2021-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails