Skip to content

Commit

Permalink
Use rufo
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Feb 25, 2024
1 parent dd9df17 commit 8d56654
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
source 'https://rubygems.org'
source "https://rubygems.org"

gemspec
25 changes: 13 additions & 12 deletions activerecord-analyze.gemspec
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# -*- encoding: utf-8 -*-
lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'activerecord-analyze/version'
require "activerecord-analyze/version"

Gem::Specification.new do |gem|
gem.name = "activerecord-analyze"
gem.version = ActiveRecordAnalyze::VERSION
gem.authors = ["pawurb"]
gem.email = ["p.urbanek89@gmail.com"]
gem.summary = %q{ Add EXPLAIN ANALYZE to Active Record query objects }
gem.description = %q{ Gem adds an "analyze" method to all Active Record query objects. Compatible with PostgreSQL database. }
gem.homepage = "http://github.com/pawurb/activerecord-analyze"
gem.files = `git ls-files`.split("\n")
gem.test_files = gem.files.grep(%r{^(spec)/})
gem.name = "activerecord-analyze"
gem.version = ActiveRecordAnalyze::VERSION
gem.authors = ["pawurb"]
gem.email = ["p.urbanek89@gmail.com"]
gem.summary = %q{ Add EXPLAIN ANALYZE to Active Record query objects }
gem.description = %q{ Gem adds an "analyze" method to all Active Record query objects. Compatible with PostgreSQL database. }
gem.homepage = "http://github.com/pawurb/activerecord-analyze"
gem.files = `git ls-files`.split("\n")
gem.test_files = gem.files.grep(%r{^(spec)/})
gem.require_paths = ["lib"]
gem.license = "MIT"
gem.license = "MIT"
gem.add_dependency "activerecord"
gem.add_dependency "railties"
gem.add_dependency "rufo"
gem.add_development_dependency "rake"
gem.add_development_dependency "pg"
gem.add_development_dependency "rspec"
Expand Down
69 changes: 34 additions & 35 deletions lib/activerecord-analyze.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'activerecord-analyze/main'
require "activerecord-analyze/main"

module ActiveRecordAnalyze
def self.analyze_sql(raw_sql, opts = {})
Expand All @@ -11,15 +11,14 @@ def self.analyze_sql(raw_sql, opts = {})
costs: true,
buffers: true,
timing: true,
summary: true
summary: true,
}
end

prefix = "EXPLAIN #{build_prefix(opts)}"

result = ActiveRecord::Base.connection.execute("#{prefix} #{raw_sql}").to_a


if [:json, :hash, :pretty_json].include?(opts[:format])
raw_json = result[0].fetch("QUERY PLAN")
if opts[:format] == :json
Expand All @@ -38,53 +37,53 @@ def self.analyze_sql(raw_sql, opts = {})

def self.build_prefix(opts = {})
format_sql = if fmt = opts[:format].presence
case fmt
when :json
"FORMAT JSON, "
when :hash
"FORMAT JSON, "
when :pretty_json
"FORMAT JSON, "
when :yaml
"FORMAT YAML, "
when :text
"FORMAT TEXT, "
when :xml
"FORMAT XML, "
else
""
case fmt
when :json
"FORMAT JSON, "
when :hash
"FORMAT JSON, "
when :pretty_json
"FORMAT JSON, "
when :yaml
"FORMAT YAML, "
when :text
"FORMAT TEXT, "
when :xml
"FORMAT XML, "
else
""
end
end
end

verbose_sql = if opts[:verbose] == true
", VERBOSE"
end
", VERBOSE"
end

costs_sql = if opts[:costs] == true
", COSTS"
end
", COSTS"
end

settings_sql = if opts[:settings] == true
", SETTINGS"
end
", SETTINGS"
end

buffers_sql = if opts[:buffers] == true
", BUFFERS"
end
", BUFFERS"
end

timing_sql = if opts[:timing] == true
", TIMING"
end
", TIMING"
end

summary_sql = if opts[:summary] == true
", SUMMARY"
end
", SUMMARY"
end

analyze_sql = if opts[:analyze] == false
""
else
"ANALYZE"
end
""
else
"ANALYZE"
end

opts_sql = "(#{format_sql}#{analyze_sql}#{verbose_sql}#{costs_sql}#{settings_sql}#{buffers_sql}#{timing_sql}#{summary_sql})"
.strip.gsub(/\s+/, " ")
Expand Down
11 changes: 5 additions & 6 deletions lib/activerecord-analyze/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def analyze(opts = {})
costs: true,
buffers: true,
timing: true,
summary: true
summary: true,
}
end

Expand Down Expand Up @@ -52,10 +52,10 @@ module Explain
def exec_analyze(queries, opts = {}) # :nodoc:
str = queries.map do |sql, binds|
analyze_msg = if opts[:analyze] == false
""
else
" ANALYZE"
end
""
else
" ANALYZE"
end

msg = "EXPLAIN#{analyze_msg} for: #{sql}".dup
unless binds.empty?
Expand All @@ -75,4 +75,3 @@ def str.inspect
end
end
end

26 changes: 13 additions & 13 deletions spec/analyze_sql_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'spec_helper'
require "spec_helper"

describe "ActiveRecord analyze" do
let(:raw_sql) do
Expand Down Expand Up @@ -31,8 +31,8 @@
it "works" do
puts result
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -43,8 +43,8 @@

it "works" do
expect(result[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -55,8 +55,8 @@

it "works" do
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -70,14 +70,14 @@
format: :hash,
costs: true,
timing: true,
summary: true
summary: true,
}
end

it "works" do
expect(result[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -88,15 +88,15 @@

let(:opts) do
{
full_debug: true
full_debug: true,
}
end

it "works" do
puts result
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end
end
24 changes: 12 additions & 12 deletions spec/main_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require 'spec_helper'
require "spec_helper"
require "migrations/create_users_migration.rb"

class User < ActiveRecord::Base; end
Expand All @@ -27,26 +27,26 @@ class User < ActiveRecord::Base; end
it "works" do
result = User.all.analyze(format: :json)
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

describe "format hash" do
it "works" do
result = User.all.analyze(format: :hash)
expect(result[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

describe "format pretty" do
it "works" do
result = User.all.analyze(format: :pretty_json)
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -61,8 +61,8 @@ class User < ActiveRecord::Base; end
it "works" do
result = User.all.analyze(full_debug: true)
expect(JSON.parse(result)[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end

Expand All @@ -72,11 +72,11 @@ class User < ActiveRecord::Base; end
format: :hash,
costs: true,
timing: true,
summary: true
summary: true,
)
expect(result[0].keys.sort).to eq [
"Execution Time", "Plan", "Planning Time", "Triggers"
]
"Execution Time", "Plan", "Planning Time", "Triggers",
]
end
end
end
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# frozen_string_literal: true

require 'rubygems'
require "rubygems"
require "active_record/railtie"
require 'bundler/setup'
require "bundler/setup"
require "migrations/create_users_migration.rb"
require_relative '../lib/activerecord-analyze'
require_relative "../lib/activerecord-analyze"

ENV["DATABASE_URL"] ||= "postgresql://postgres:secret@localhost:5432/activerecord-analyze-test"

Expand Down

0 comments on commit 8d56654

Please sign in to comment.