sql_footprint 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ccd5321b9997aec9b2f17886396d7deaf6b90d82
4
+ data.tar.gz: 5fe7904a9e24961e73a4c8bd356987db3a6b8ff5
5
+ SHA512:
6
+ metadata.gz: 6d6f53ce634d8d0dbb070b33344797c041bd5524a13aa89f577be4be60a6c965c7234f0f1389e25d2130660e969c0968384289f99c896da427e9928e78f74093
7
+ data.tar.gz: 2b7f935a0a5ebae662634541cfbcc1829a14e1ddca9bee909644e553e2f6b0dde4a9ad210b9736e32592c4c6e1bebe7e1b4ef16aedb1b263296a984b5c613917
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ footprint.sql
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ require: rubocop-rspec
2
+
3
+ Style/MethodDefParentheses:
4
+ EnforcedStyle: require_no_parentheses
5
+
6
+ Metrics/LineLength:
7
+ Max: 100
8
+
9
+ Documentation:
10
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ script:
3
+ - bundle exec rspec
4
+ - bundle exec rubocop --display-cop-names
5
+ sudo: false
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0
9
+ - 2.1
10
+ - 2.2
11
+ - rbx-2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sql_footprint.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # SqlFootprint [![Build Status](https://travis-ci.org/covermymeds/sql_footprint.svg?branch=master)](https://travis-ci.org/covermymeds/sql_footprint)
2
+
3
+ This gem allows you to keep a "footprint" of the sql queries that your application runs.
4
+ It's like logging all the sql you're executing except that we remove all the value parameters
5
+ and dedupe similar queries. This footprint should be valuable in determining if changes you've
6
+ made will significantly change the way you're querying the database.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'sql_footprint', group: [:development, :test]
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ ## Usage
21
+
22
+ Typically, you would want to run this while you're running your specs.
23
+ For example w/ RSpec:
24
+ ```
25
+ RSpec.configure do |config|
26
+ config.before(:suite) { SqlFootprint.start }
27
+ config.after(:suite) { SqlFootprint.stop }
28
+ end
29
+ ```
30
+
31
+ After running your specs you'll find a 'footprint.sql' file in your project.
32
+ Adding this to your Git repository can be very useful so you can include the diff of the footprint
33
+ as part of your code review.
34
+
35
+ DO NOT run SqlFootprint in production!
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'sql_footprint'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module SqlFootprint
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,82 @@
1
+ require 'sql_footprint/version'
2
+
3
+ module SqlFootprint
4
+ def self.start
5
+ @original_logger = ActiveRecord::Base.logger
6
+ @logger = Logger.new
7
+ ActiveRecord::Base.logger = @logger
8
+ end
9
+
10
+ def self.stop
11
+ ActiveRecord::Base.logger = @original_logger
12
+ File.open('footprint.sql', 'w') do |f|
13
+ @logger.logs.each do |log|
14
+ f.puts log
15
+ end
16
+ end
17
+ end
18
+
19
+ class Logger
20
+ def initialize
21
+ @logs = []
22
+ end
23
+
24
+ attr_reader :logs
25
+
26
+ def error param
27
+ raise param
28
+ end
29
+
30
+ def debug text
31
+ if sql? text
32
+ sql = format_sql(text)
33
+ logs << sql unless logs.include?(sql)
34
+ logs.sort!
35
+ end
36
+ end
37
+
38
+ def debug?
39
+ true
40
+ end
41
+
42
+ private
43
+
44
+ def sql? text
45
+ /SQL/.match(text) ||
46
+ /Load\s\(/.match(text)
47
+ end
48
+
49
+ def format_sql text
50
+ strip_values(text).split("\e\[0m")
51
+ .select { |t| !t.include?('SQL') }
52
+ .find { |t| !/Load\s\(/.match(t) }
53
+ .gsub(/\e\[1m/, '')
54
+ .strip
55
+ end
56
+
57
+ def strip_values text
58
+ text = text.gsub(/\[\[.*\]\]/, '')
59
+ text = strip_string_values(text)
60
+ text = strip_integer_values(text)
61
+ strip_in_clause_values(text)
62
+ end
63
+
64
+ def strip_in_clause_values text
65
+ text.gsub(/\sIN\s\((.*)\)/) do |_match|
66
+ ' IN (values-redacted)'
67
+ end
68
+ end
69
+
70
+ def strip_integer_values text
71
+ text.gsub(/\s\=\s([0-9]+)/) do |_match|
72
+ ' = number-redacted'
73
+ end
74
+ end
75
+
76
+ def strip_string_values text
77
+ text.gsub(/\s\=\s\'(.*)\'/) do |_match|
78
+ " = 'value-redacted'"
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sql_footprint/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sql_footprint'
8
+ spec.version = SqlFootprint::VERSION
9
+ spec.authors = ['Brandon Joyce']
10
+ spec.email = ['bjoyce@covermymeds.com']
11
+
12
+ spec.summary = 'Keeps your DB guy happy.'
13
+ spec.description = 'Check your footprint file into source control'
14
+ spec.homepage = 'https://github.com/covermymeds/sql_footprint'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'activerecord', '~> 4.0.0'
27
+ spec.add_development_dependency 'pry'
28
+ spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'rubocop', '~> 0.37.0'
30
+ spec.add_development_dependency 'rubocop-rspec'
31
+ end
metadata ADDED
@@ -0,0 +1,168 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_footprint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Brandon Joyce
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 0.37.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.37.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Check your footprint file into source control
126
+ email:
127
+ - bjoyce@covermymeds.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".rubocop.yml"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/sql_footprint.rb
142
+ - lib/sql_footprint/version.rb
143
+ - sql_footprint.gemspec
144
+ homepage: https://github.com/covermymeds/sql_footprint
145
+ licenses: []
146
+ metadata: {}
147
+ post_install_message:
148
+ rdoc_options: []
149
+ require_paths:
150
+ - lib
151
+ required_ruby_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ requirements: []
162
+ rubyforge_project:
163
+ rubygems_version: 2.4.8
164
+ signing_key:
165
+ specification_version: 4
166
+ summary: Keeps your DB guy happy.
167
+ test_files: []
168
+ has_rdoc: