sql_origin 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@sql_origin --create
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ gem 'rails', '~> 3.2'
4
+
5
+ group :development do
6
+ gem 'yard'
7
+ gem 'redcarpet'
8
+
9
+ gem 'bundler'
10
+ gem 'jeweler'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,97 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.6)
5
+ actionpack (= 3.2.6)
6
+ mail (~> 2.4.4)
7
+ actionpack (3.2.6)
8
+ activemodel (= 3.2.6)
9
+ activesupport (= 3.2.6)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.1)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.2)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.3)
17
+ activemodel (3.2.6)
18
+ activesupport (= 3.2.6)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.6)
21
+ activemodel (= 3.2.6)
22
+ activesupport (= 3.2.6)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.6)
26
+ activemodel (= 3.2.6)
27
+ activesupport (= 3.2.6)
28
+ activesupport (3.2.6)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.0)
33
+ erubis (2.7.0)
34
+ git (1.2.5)
35
+ hike (1.2.1)
36
+ i18n (0.6.0)
37
+ jeweler (1.8.3)
38
+ bundler (~> 1.0)
39
+ git (>= 1.2.5)
40
+ rake
41
+ rdoc
42
+ journey (1.0.3)
43
+ json (1.7.3)
44
+ mail (2.4.4)
45
+ i18n (>= 0.4.0)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ mime-types (1.18)
49
+ multi_json (1.3.6)
50
+ polyglot (0.3.3)
51
+ rack (1.4.1)
52
+ rack-cache (1.2)
53
+ rack (>= 0.4)
54
+ rack-ssl (1.3.2)
55
+ rack
56
+ rack-test (0.6.1)
57
+ rack (>= 1.0)
58
+ rails (3.2.6)
59
+ actionmailer (= 3.2.6)
60
+ actionpack (= 3.2.6)
61
+ activerecord (= 3.2.6)
62
+ activeresource (= 3.2.6)
63
+ activesupport (= 3.2.6)
64
+ bundler (~> 1.0)
65
+ railties (= 3.2.6)
66
+ railties (3.2.6)
67
+ actionpack (= 3.2.6)
68
+ activesupport (= 3.2.6)
69
+ rack-ssl (~> 1.3.2)
70
+ rake (>= 0.8.7)
71
+ rdoc (~> 3.4)
72
+ thor (>= 0.14.6, < 2.0)
73
+ rake (0.9.2.2)
74
+ rdoc (3.12)
75
+ json (~> 1.4)
76
+ redcarpet (2.1.1)
77
+ sprockets (2.1.3)
78
+ hike (~> 1.2)
79
+ rack (~> 1.0)
80
+ tilt (~> 1.1, != 1.3.0)
81
+ thor (0.15.2)
82
+ tilt (1.3.3)
83
+ treetop (1.4.10)
84
+ polyglot
85
+ polyglot (>= 0.3.1)
86
+ tzinfo (0.3.33)
87
+ yard (0.8.2.1)
88
+
89
+ PLATFORMS
90
+ ruby
91
+
92
+ DEPENDENCIES
93
+ bundler
94
+ jeweler
95
+ rails (~> 3.2)
96
+ redcarpet
97
+ yard
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Tim Morgan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ SQL:Origin
2
+ ==========
3
+
4
+ Adds backtraces to your SQL queries and query logs, so you know where your
5
+ queries are coming from. This only works with Rails, and is only tested with
6
+ Rails 3.2.
7
+
8
+ Why do I want this?
9
+ -------------------
10
+
11
+ Simple. To turn this
12
+
13
+ ![Without backtrace logging](https://img.skitch.com/20120614-b5bw4bqxsitde6ej6776was6tx.png)
14
+
15
+ into this.
16
+
17
+ ![With backtrace logging](https://img.skitch.com/20120614-kcisbjqiaxfwbq82wbjm4iumfa.png)
18
+
19
+ So now, you needn't wonder where that odd-looking or broken SQL query is coming
20
+ from.
21
+
22
+ It can also turn this
23
+
24
+ ````
25
+ Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
26
+ Count: 1 Time=4.32s (4s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
27
+ SELECT "events".* FROM "events" WHERE "events"."bug_id" = ?
28
+
29
+ Count: 3 Time=2.53s (7s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
30
+ SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ?
31
+
32
+ Count: 3 Time=2.13s (6s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
33
+ SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1
34
+ ````
35
+
36
+ into this.
37
+
38
+ ````
39
+ Reading mysql slow query log from /usr/local/mysql/data/mysqld51-apple-slow.log
40
+ Count: 1 Time=4.32s (4s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
41
+ SELECT "events".* FROM "events" WHERE "events"."bug_id" = ? /* app/models/project.rb:125:in `_callback_after_617' */
42
+
43
+ Count: 3 Time=2.53s (7s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
44
+ SELECT "deploys".* FROM "deploys" WHERE "deploys"."id" = ? /* app/controllers/projects_controller.rb:359:in `require_or_load' */
45
+
46
+ Count: 3 Time=2.13s (6s) Lock=0.00s (0s) Rows=0.0 (0), root[root]@localhost
47
+ SELECT * FROM "slugs" WHERE (LOWER("slugs"."slug") = LOWER(?) AND "slugs"."scope" IS NULL AND "slugs"."sluggable_type" = ?) LIMIT 1 /* app/models/observers/bug_observer.rb:23:in `create_open_event' */
48
+ ````
49
+
50
+ Installation
51
+ ------------
52
+
53
+ To use, add SQL:Origin to your Gemfile:
54
+
55
+ ```` ruby
56
+ gem 'sql_origin'
57
+ ````
58
+
59
+ If you would like to add three-line backtraces below every SQL query in your
60
+ Rails log, add
61
+
62
+ ```` ruby
63
+ SQLOrigin.append_to_log
64
+ ````
65
+
66
+ somewhere in your Rails initialization (e.g., `application.rb` or a
67
+ `config/initializer` file).
68
+
69
+ If you would like to add a one-line backtrace comment to every SQL query, add
70
+
71
+ ```` ruby
72
+ SQLOrigin.append_to_query
73
+ ````
74
+
75
+ somewhere in your Rails initialization.
76
+
77
+ It would be typical to enable `append_to_log` for development and test, and
78
+ `append_to_query` for production, in order to keep production logs small.
79
+
80
+ ### Backtrace Filtering
81
+
82
+ By default, files not under your Rails root, and files under `vendor`, are
83
+ filtered from your backtrace. If you need to filter other files, add them to
84
+ {SQLOrigin::LIBRARY_PATHS}:
85
+
86
+ ```` ruby
87
+ SQLOrigin::LIBRARY_PATHS << 'config/initializers/active_record_hacks.rb'
88
+ ````
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "sql_origin"
18
+ gem.homepage = "http://github.com/RISCfuture/sql_origin"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Add backtraces to your query log and queries themselves.}
21
+ gem.description = %Q{Ever wonder where a SQL query comes from? This gem lets you add abbreviated backtraces to those queries, either in the query log, or as a comment in the query itself.}
22
+ gem.email = "rubygems@timothymorgan.info"
23
+ gem.authors = ["Tim Morgan"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'yard'
29
+ YARD::Rake::YardocTask.new('doc') do |doc|
30
+ doc.options << '-m' << 'markdown' << '-M' << 'redcarpet'
31
+ doc.options << '--protected' << '--no-private'
32
+ doc.options << '-r' << 'README.md'
33
+ doc.options << '-o' << 'doc'
34
+ doc.options << '--title' << 'SQL:Origin Documentation'
35
+
36
+ doc.files = %w( lib/**/* README.md )
37
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,50 @@
1
+ module SQLOrigin
2
+ # @private
3
+ module LogHook
4
+ def log(sql, name = "SQL", binds = [])
5
+ @instrumenter.instrument(
6
+ "sql.active_record",
7
+ :sql => sql,
8
+ :name => name,
9
+ :connection_id => object_id,
10
+ :binds => binds,
11
+ :backtrace => SQLOrigin.filtered_backtrace[0, 3]) { yield }
12
+ rescue Exception => e
13
+ message = "#{e.class.name}: #{e.message}: #{sql}"
14
+ @logger.debug message if @logger
15
+ exception = translate_exception(e, message)
16
+ exception.set_backtrace e.backtrace
17
+ raise exception
18
+ end
19
+ end
20
+
21
+ # @private
22
+ module LogSubscriber
23
+ extend ActiveSupport::Concern
24
+ included { alias_method_chain :sql, :backtrace }
25
+
26
+ def sql_with_backtrace(event)
27
+ sql_without_backtrace event
28
+
29
+ if event.payload[:backtrace].any?
30
+ event.payload[:backtrace].each do |line|
31
+ debug " #{color line, "\e[90m", false}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ # @private
38
+ module QueryAppendHook
39
+ extend ActiveSupport::Concern
40
+ included { alias_method_chain :execute, :backtrace }
41
+
42
+ def execute_with_backtrace(sql, name=nil, binds=[])
43
+ if (line = SQLOrigin.filtered_backtrace.first)
44
+ execute_without_backtrace "#{sql} /* #{line} */", name, binds
45
+ else
46
+ execute_without_backtrace sql, name, binds
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/sql_origin.rb ADDED
@@ -0,0 +1,44 @@
1
+ # Container module for all SQL:Origin methods.
2
+
3
+ module SQLOrigin
4
+ # Paths or path prefixes that are filtered from the backtrace.
5
+ LIBRARY_PATHS = %w(
6
+ vendor
7
+ )
8
+
9
+ # @return [Array<String>] The backtrace less library paths.
10
+
11
+ def self.filtered_backtrace
12
+ caller.map do |line|
13
+ line.sub /^#{Regexp.escape Rails.root.to_s}\//, ''
14
+ end.select do |line|
15
+ !line.starts_with?("/") &&
16
+ LIBRARY_PATHS.none? { |path| line.starts_with?(path) }
17
+ end
18
+ end
19
+
20
+ # Enables SQL:Origin backtrace logging to the Rails log.
21
+
22
+ def self.append_to_log
23
+ %w( PostgreSQLAdapter MysqlAdapter Mysql2Adapter OracleAdapter SQLiteAdapter ).each do |name|
24
+ adapter = ActiveRecord::ConnectionAdapters.const_get(name.to_sym) rescue nil
25
+ if adapter
26
+ adapter.send :include, SQLOrigin::LogHook
27
+ end
28
+ end
29
+ ActiveRecord::LogSubscriber.send :include, SQLOrigin::LogSubscriber
30
+ end
31
+
32
+ # Enables SQL:Origin backtrace logging to SQL query comments.
33
+
34
+ def self.append_to_query
35
+ %w( PostgreSQLAdapter MysqlAdapter Mysql2Adapter OracleAdapter SQLiteAdapter ).each do |name|
36
+ adapter = ActiveRecord::ConnectionAdapters.const_get(name.to_sym) rescue nil
37
+ if adapter
38
+ adapter.send :include, SQLOrigin::QueryAppendHook
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ require 'sql_origin/hooks'
@@ -0,0 +1,62 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sql_origin"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tim Morgan"]
12
+ s.date = "2012-06-14"
13
+ s.description = "Ever wonder where a SQL query comes from? This gem lets you add abbreviated backtraces to those queries, either in the query log, or as a comment in the query itself."
14
+ s.email = "rubygems@timothymorgan.info"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rvmrc",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.md",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/sql_origin.rb",
29
+ "lib/sql_origin/hooks.rb",
30
+ "sql_origin.gemspec"
31
+ ]
32
+ s.homepage = "http://github.com/RISCfuture/sql_origin"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.24"
36
+ s.summary = "Add backtraces to your query log and queries themselves."
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<rails>, ["~> 3.2"])
43
+ s.add_development_dependency(%q<yard>, [">= 0"])
44
+ s.add_development_dependency(%q<redcarpet>, [">= 0"])
45
+ s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<rails>, ["~> 3.2"])
49
+ s.add_dependency(%q<yard>, [">= 0"])
50
+ s.add_dependency(%q<redcarpet>, [">= 0"])
51
+ s.add_dependency(%q<bundler>, [">= 0"])
52
+ s.add_dependency(%q<jeweler>, [">= 0"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rails>, ["~> 3.2"])
56
+ s.add_dependency(%q<yard>, [">= 0"])
57
+ s.add_dependency(%q<redcarpet>, [">= 0"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, [">= 0"])
60
+ end
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_origin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tim Morgan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: yard
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: redcarpet
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Ever wonder where a SQL query comes from? This gem lets you add abbreviated
95
+ backtraces to those queries, either in the query log, or as a comment in the query
96
+ itself.
97
+ email: rubygems@timothymorgan.info
98
+ executables: []
99
+ extensions: []
100
+ extra_rdoc_files:
101
+ - LICENSE.txt
102
+ - README.md
103
+ files:
104
+ - .document
105
+ - .rvmrc
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - VERSION
112
+ - lib/sql_origin.rb
113
+ - lib/sql_origin/hooks.rb
114
+ - sql_origin.gemspec
115
+ homepage: http://github.com/RISCfuture/sql_origin
116
+ licenses:
117
+ - MIT
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ segments:
129
+ - 0
130
+ hash: -915926160300598218
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 1.8.24
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Add backtraces to your query log and queries themselves.
143
+ test_files: []