sql_footprint 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/README.md +35 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/sql_footprint/version.rb +3 -0
- data/lib/sql_footprint.rb +82 -0
- data/sql_footprint.gemspec +31 -0
- metadata +168 -0
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
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
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
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,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:
|