Skip to content

Commit

Permalink
Adding rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Joyce committed Mar 19, 2016
1 parent 47ae3c7 commit a7534fa
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 46 deletions.
10 changes: 10 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require: rubocop-rspec

Style/MethodDefParentheses:
EnforcedStyle: require_no_parentheses

Metrics/LineLength:
Max: 100

Documentation:
Enabled: false
6 changes: 3 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "sql_footprint"
require 'bundler/setup'
require 'sql_footprint'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +10,5 @@ require "sql_footprint"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
37 changes: 19 additions & 18 deletions lib/sql_footprint.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "sql_footprint/version"
require 'sql_footprint/version'

module SqlFootprint
def self.start
Expand All @@ -24,58 +24,59 @@ def initialize
attr_reader :logs

def error param
fail param
raise param
end

def debug text
if sql? text
sql = format_sql(text)
logs << sql if !logs.include?(sql)
logs << sql unless logs.include?(sql)
logs.sort!
end
end

def debug?
true
end

private

def sql? text
/SQL/.match(text) ||
/Load\s\(/.match(text)
end

def format_sql text
strip_values(text).split("\e\[0m")
.select { |t| !t.include?('SQL') }
.select { |t| !/Load\s\(/.match(t) }
.first
.gsub(/\e\[1m/, '')
.strip
.select { |t| !t.include?('SQL') }
.find { |t| !/Load\s\(/.match(t) }
.gsub(/\e\[1m/, '')
.strip
end

def strip_values text
text = text.gsub(/\[\[.*\]\]/, '')
text = strip_string_values(text)
text = strip_integer_values(text)
text = strip_in_clause_values(text)
strip_in_clause_values(text)
end

def strip_in_clause_values text
text.gsub(/\sIN\s\((.*)\)/) do |match|
" IN (values-redacted)"
text.gsub(/\sIN\s\((.*)\)/) do |_match|
' IN (values-redacted)'
end
end

def strip_integer_values text
text.gsub(/\s\=\s([0-9]+)/) do |match|
" = number-redacted"
text.gsub(/\s\=\s([0-9]+)/) do |_match|
' = number-redacted'
end
end

def strip_string_values text
text.gsub(/\s\=\s\'(.*)\'/) do |match|
text.gsub(/\s\=\s\'(.*)\'/) do |_match|
" = 'value-redacted'"
end
end

def debug?
true
end
end
end
2 changes: 1 addition & 1 deletion lib/sql_footprint/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SqlFootprint
VERSION = "0.1.0"
VERSION = '0.1.0'.freeze
end
4 changes: 4 additions & 0 deletions spec/models.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class Widget < ActiveRecord::Base
has_many :cogs
end

class Cog < ActiveRecord::Base
end
10 changes: 9 additions & 1 deletion spec/schema.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
ActiveRecord::Schema.define do
self.verbose = false

create_table :widgets, :force => true do |t|
create_table :widgets, force: true do |t|
t.string :name
t.integer :quantity

t.timestamps
end

create_table :cogs, force: true do |t|
t.string :name
t.integer :quantity
t.references :widget

t.timestamps
end
end
20 changes: 15 additions & 5 deletions spec/sql_footprint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@

it 'formats inserts' do
Widget.create!
expect(sql).to eq "INSERT INTO \"widgets\" (\"created_at\", \"updated_at\") VALUES (?, ?)"

expect(sql).to eq 'INSERT INTO "widgets" ("created_at", "updated_at") VALUES (?, ?)'
end

it 'formats selects' do
Widget.where(name: SecureRandom.uuid, quantity: 1).last
expect(sql).to eq "SELECT \"widgets\".* FROM \"widgets\" WHERE \"widgets\".\"name\" = 'value-redacted' AND \"widgets\".\"quantity\" = number-redacted ORDER BY \"widgets\".\"id\" DESC LIMIT 1"
expect(sql).to eq 'SELECT "widgets".* FROM "widgets" WHERE "widgets"."name" = '\
"'value-redacted' AND \"widgets\".\"quantity\" = number-redacted ORDER BY \"widgets"\
'"."id" DESC LIMIT 1'
end

it 'formats IN clauses' do
Widget.where(name: [SecureRandom.uuid, SecureRandom.uuid]).last
expect(sql).to eq "SELECT \"widgets\".* FROM \"widgets\" WHERE \"widgets\".\"name\" IN (values-redacted) ORDER BY \"widgets\".\"id\" DESC LIMIT 1"
expect(sql).to eq 'SELECT "widgets".* FROM "widgets" WHERE "widgets"."name" '\
'IN (values-redacted) ORDER BY "widgets"."id" DESC LIMIT 1'
end

it 'dedupes the same sql' do
Expand All @@ -41,6 +43,14 @@
Widget.create!
expect(logger.logs.first).to include('INSERT INTO')
end

it 'works with joins' do
Widget.joins(:cogs).where(name: SecureRandom.uuid).load
expected = 'SELECT "widgets".* FROM "widgets" INNER JOIN "'\
'cogs" ON "cogs"."widget_id" = "widgets"."id" WHERE '\
"\"widgets\".\"name\" = 'value-redacted'"
expect(logger.logs.first).to eq expected
end
end

describe '.stop' do
Expand All @@ -60,7 +70,7 @@
Widget.last
described_class.stop
log = File.read('footprint.sql')
expect(log).to_not include('INSERT INTO')
expect(log).not_to include('INSERT INTO')
end
end
end
34 changes: 19 additions & 15 deletions sql_footprint.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'sql_footprint/version'

Gem::Specification.new do |spec|
spec.name = "sql_footprint"
spec.name = 'sql_footprint'
spec.version = SqlFootprint::VERSION
spec.authors = ["Brandon Joyce"]
spec.email = ["[email protected]"]
spec.authors = ['Brandon Joyce']
spec.email = ['[email protected]']

spec.summary = %q{Keeps your DB guy happy.}
spec.description = %q{Check your footprint file into source control}
spec.homepage = "https://github.com/covermymeds/sql_footprint"
spec.summary = 'Keeps your DB guy happy.'
spec.description = 'Check your footprint file into source control'
spec.homepage = 'https://github.com/covermymeds/sql_footprint'

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
spec.bindir = "exe"
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "activerecord", "~> 4.0.0"
spec.add_development_dependency "pry"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency 'bundler', '~> 1.10'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'activerecord', '~> 4.0.0'
spec.add_development_dependency 'pry'
spec.add_development_dependency 'sqlite3'
spec.add_development_dependency 'rubocop', '~> 0.37.0'
spec.add_development_dependency 'rubocop-rspec'
end

0 comments on commit a7534fa

Please sign in to comment.