query_tracer 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,11 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ree
6
+ - rbx-2.0
7
+ - jruby
8
+
9
+ branches:
10
+ only:
11
+ - master
data/Gemfile CHANGED
@@ -1 +1,13 @@
1
+ source 'http://rubygems.org'
2
+
1
3
  gemspec
4
+
5
+ gem "activesupport", '>= 3.0.0'
6
+ gem "activerecord", '>= 3.0.0'
7
+
8
+ group :development, :test do
9
+ gem "rake"
10
+ gem "rspec"
11
+ gem 'capybara'
12
+ gem 'sqlite3'
13
+ end
@@ -23,7 +23,7 @@ Add following code to config/initializers/query_tracer.rb:
23
23
  tracer.colorize = true
24
24
  tracer.show_revision = true
25
25
  tracer.multiline = true
26
- tracer.exclude_queries << %r{FROM sqlite_master}
26
+ tracer.exclude_sql << %r{FROM sqlite_master}
27
27
  end
28
28
 
29
29
  QueryTracer::Logger.attach_to :active_record
data/Rakefile CHANGED
@@ -1,31 +1,8 @@
1
- begin
2
- require "bundler"
3
- Bundler.setup
4
- rescue LoadError
5
- $stderr.puts "You need to have Bundler installed to be able build this gem."
6
- end
1
+ require 'bundler/setup'
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
7
4
 
8
- gemspec = eval(File.read(Dir["*.gemspec"].first))
5
+ task :default => :spec
9
6
 
10
-
11
- desc "Validate the gemspec"
12
- task :gemspec do
13
- gemspec.validate
14
- end
15
-
16
- desc "Build gem locally"
17
- task :build => :gemspec do
18
- system "gem build #{gemspec.name}.gemspec"
19
- FileUtils.mkdir_p "pkg"
20
- FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
21
- end
22
-
23
- desc "Install gem locally"
24
- task :install => :build do
25
- system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
26
- end
27
-
28
- desc "Clean automatically generated files"
29
- task :clean do
30
- FileUtils.rm_rf "pkg"
31
- end
7
+ desc "Test query_tracer plugin."
8
+ RSpec::Core::RakeTask.new('spec')
@@ -1,4 +1,6 @@
1
1
  require 'ostruct'
2
+ require 'active_support'
3
+ require 'active_record'
2
4
  require 'query_tracer/tracer'
3
5
  require 'query_tracer/logger'
4
6
  require 'query_tracer/db'
@@ -8,29 +10,21 @@ module QueryTracer
8
10
  def self.configure
9
11
  @config = OpenStruct.new({
10
12
  :enabled => true,
11
- :colorize => true,
12
13
  :show_revision => true,
13
14
  :multiline => true,
14
- :log_level => :debug,
15
15
  :exclude_sql => [],
16
- :root => Rails.root
16
+ :log_level => :debug
17
17
  })
18
18
 
19
- @config.include_codepoints = [
20
- %r{^#{@config.root}/(app/presenters/.*)},
21
- %r{^#{@config.root}/(app/views/.*)},
22
- %r{^#{@config.root}/(app/controllers/.*)},
23
- %r{^#{@config.root}/(app/models/.*)},
24
- %r{^#{@config.root}/(lib/.*)},
25
- %r{^#{@config.root}/(spec/.*)},
26
- %r{^#{@config.root}/(app/.*)},
27
- %r{^#{@config.root}/(vendor/(?:gems|plugins)/.*)},
28
- %r{^#{@config.root}/(.*)},
29
- %r{in `(irb)_binding'}
19
+ @config.default_codepoints = [
20
+ '(app/presenters/.*)',
21
+ '(app/views/.*)',
22
+ '(app/controllers/.*)',
23
+ '(app/models/.*)',
24
+ '(lib/.*)',
25
+ '(spec/.*)',
26
+ '(app/.*)'
30
27
  ]
31
-
32
- @config.exclude_codepoint = %r{^(#{@config.root}/(?:vendor/(?:rails|gems/(?:composite_primary_keys|db-charmer)|plugins/(?:paginating_find|acts_as_sluggable))|config/initializers/mysql_adapter_extensions\.rb|tmp/gems|lib/query_tracer))|\.rvm/|/gems/}
33
-
34
28
  @config.db_adapter = ActiveRecord::Base.connection.adapter_name.capitalize
35
29
 
36
30
  begin
@@ -40,10 +34,30 @@ module QueryTracer
40
34
  end
41
35
 
42
36
  yield @config
37
+
38
+ @config.root ||= ::Rails.root
39
+
40
+ @config.include_codepoints = build_codepoints
41
+
42
+ Logger.attach_to :active_record
43
43
  end
44
44
 
45
45
  def self.config
46
46
  @config
47
47
  end
48
+
49
+ private
50
+ def self.build_codepoints
51
+ @config.default_codepoints.map do |cp|
52
+ case cp
53
+ when String
54
+ %r{^#{@config.root}/#{cp}}
55
+ when Regexp
56
+ cp
57
+ else
58
+ nil
59
+ end
60
+ end.compact
61
+ end
48
62
 
49
63
  end
@@ -1,5 +1,7 @@
1
1
  require 'query_tracer/db/mysql'
2
+ require 'query_tracer/db/mysql2'
2
3
  require 'query_tracer/db/sqlite'
4
+ require 'query_tracer/db/sqlite3'
3
5
 
4
6
  module QueryTracer
5
7
  module Db
@@ -0,0 +1,7 @@
1
+ module QueryTracer
2
+ module Db
3
+ module Mysql2
4
+ include QueryTracer::Db::Mysql
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module QueryTracer
2
+ module Db
3
+ module Sqlite3
4
+ include QueryTracer::Db::Sqlite
5
+ end
6
+ end
7
+ end
@@ -13,16 +13,12 @@ module QueryTracer
13
13
  sql = event.payload[:sql]
14
14
  # Skip noisy queries
15
15
  trace = Tracer.build_trace(sql)
16
- return if trace.blank?
17
- # We're done
18
- if QueryTracer.config.colorize
19
- message = "\e[34m\e[43m^^^^ Called from:\e[0m "
20
- indent = "\e[34m\e[43m->\e[0m "
21
- else
22
- message = '^^^^ Called from: '
23
- indent = " "
16
+ unless trace.blank?
17
+ message = color("^^^^ Called from: ", YELLOW, true)
18
+ indent = color("-> ", YELLOW, true)
19
+
20
+ send QueryTracer.config.log_level, message + trace.join("\n#{indent}")
24
21
  end
25
- logger.send QueryTracer.config.log_level.to_sym, message + trace.join("\n#{indent}")
26
22
  end
27
23
  end
28
24
  end
@@ -5,26 +5,31 @@ module QueryTracer
5
5
  extend self
6
6
 
7
7
  def build_trace(sql)
8
- unless skip_query?(sql)
9
- # Skip noisy codepoints
10
- lines = caller.inject([]) do |filtered, line|
11
- unless line =~ QueryTracer.config.exclude_codepoint
12
- filtered << line unless QueryTracer.config.include_codepoints.select{ |expr| line =~ expr }.blank?
13
- end
14
- filtered
15
- end
16
-
17
- unless lines.blank?
18
- lines = lines.first unless QueryTracer.config.multiline
19
- [QueryTracer::Tracer::Revision.current, lines].flatten
20
- end
21
-
8
+ return nil if skip_query?(sql)
9
+
10
+ # Include only selected code points
11
+ lines = []
12
+ if QueryTracer.config.multiline
13
+ caller.each {|line| lines << line if include_line?(line)}
14
+ else
15
+ lines << caller.find {|line| include_line?(line)}
16
+ end
17
+
18
+ unless lines.blank?
19
+ rev = QueryTracer::Tracer::Revision.current
20
+ lines << rev unless rev.blank?
22
21
  end
22
+
23
+ lines
23
24
  end
24
25
 
25
26
  def skip_query?(sql)
26
27
  !QueryTracer.config.exclude_sql.select { |expr| sql =~ expr }.blank?
27
28
  end
29
+
30
+ def include_line?(line)
31
+ !QueryTracer.config.include_codepoints.select{ |expr| line =~ expr }.blank?
32
+ end
28
33
 
29
34
  end
30
35
  end
@@ -0,0 +1,3 @@
1
+ module QueryTracer
2
+ VERSION = "0.1.0"
3
+ end
@@ -1,30 +1,27 @@
1
- # coding: UTF-8
1
+ # encoding: UTF-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require 'query_tracer/version'
2
5
 
3
6
  Gem::Specification.new do |s|
4
7
  s.name = "query_tracer"
5
- s.version = "0.0.4"
6
- s.platform = Gem::Platform::RUBY
8
+ s.version = QueryTracer::VERSION
7
9
  s.authors = ["Dmitry Shaposhnik", "Dmytro Shteflyuk"]
8
10
  s.email = ["dmitry@shaposhnik.name", "kpumuk@kpumuk.info"]
9
11
  s.homepage = "http://github.com/daemon/query_tracer"
10
12
  s.summary = "Query tracer and logger for Rails3"
11
- s.description = ""
12
-
13
- s.required_rubygems_version = ">= 1.3.6"
14
-
15
- # If you have runtime dependencies, add them here
16
- # s.add_runtime_dependency "other", "~> 1.2"
17
-
18
- # If you have development dependencies, add them here
19
- # s.add_development_dependency "another", "= 0.9"
13
+ s.description = "This library helps to find where in your rails app sql query was run."
20
14
 
21
15
  # The list of files to be contained in the gem
22
16
  s.files = `git ls-files`.split("\n")
23
- # s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
- # s.extensions = `git ls-files ext/extconf.rb`.split("\n")
25
17
 
26
18
  s.require_path = 'lib'
27
19
 
28
- # For C extensions
29
- # s.extensions = "ext/extconf.rb"
20
+ s.add_dependency('activesupport', '>= 3.0.0')
21
+ s.add_dependency('activerecord', '>= 3.0.0')
22
+
23
+ s.add_development_dependency('rake')
24
+ s.add_development_dependency('rspec')
25
+ s.add_development_dependency('capybara')
26
+
30
27
  end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe QueryTracer::Logger do
4
+ it "should log in debug level" do
5
+ initialize_logger
6
+ QueryTracer.config.log_level.should == :debug
7
+ end
8
+
9
+ it "should use colors if Rails colorize enabled" do
10
+ # ActiveSupport::LogSubscriber.colorize_logging = true
11
+ # initialize_logger
12
+ end
13
+
14
+ it "should not use colors if Rails colorize disabled" do
15
+ # ActiveSupport::LogSubscriber.colorize_logging = false
16
+ # initialize_logger
17
+ end
18
+
19
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe QueryTracer do
4
+
5
+ it "should accept configuration" do
6
+ root = File.expand_path(File.dirname(__FILE__))
7
+
8
+ QueryTracer.configure do |tracer|
9
+ tracer.enabled = true
10
+ tracer.show_revision = true
11
+ tracer.multiline = true
12
+ tracer.root = root
13
+ end
14
+
15
+ QueryTracer.config.enabled.should be_true
16
+ QueryTracer.config.root.should == root
17
+ end
18
+
19
+ it "should load exclude_sql from db adapter" do
20
+ QueryTracer.config.exclude_sql.should == QueryTracer::Db::Sqlite::SKIP_QUERIES
21
+ end
22
+
23
+ it "should attach logger to active_record"
24
+
25
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe QueryTracer::Tracer do
4
+ describe "SQLite" do
5
+ it "should skip queries from matching exclude_sql" do
6
+ ActiveRecord::Base.connection.stub(:adapter_name).and_return 'Sqlite'
7
+ initialize_logger
8
+ QueryTracer::Tracer.build_trace("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name").should be_nil
9
+ end
10
+ end
11
+
12
+ describe "MySQL" do
13
+ it "should skip queries from matching exclude_sql" do
14
+ ActiveRecord::Base.connection.stub(:adapter_name).and_return 'Mysql'
15
+ initialize_logger
16
+ QueryTracer::Tracer.build_trace("SHOW TABLES").should be_nil
17
+ end
18
+ end
19
+
20
+ it "should produce mutliline trace if multiline enabled"
21
+ it "should produce only codepoint if multiline disabled"
22
+
23
+ end
@@ -0,0 +1,54 @@
1
+ require "bundler/setup"
2
+ require "active_record"
3
+ require "active_support/all"
4
+ require "query_tracer"
5
+ require 'capybara/rspec'
6
+ # require 'rspec/rails'
7
+
8
+ RSpec.configure do |config|
9
+
10
+ end
11
+
12
+ def initialize_logger(multiline = true, revision = true)
13
+ QueryTracer.configure do |tracer|
14
+ tracer.enabled = true
15
+ tracer.show_revision = multiline
16
+ tracer.multiline = revision
17
+ tracer.root = File.expand_path(File.dirname(__FILE__))
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
22
+ ActiveRecord::Migration.verbose = false
23
+
24
+ ActiveRecord::Schema.define do
25
+ create_table :projects, :force => true do |t|
26
+ t.string :name
27
+ end
28
+ end
29
+
30
+ class Project < ActiveRecord::Base
31
+ # column :name, :string
32
+ has_many :tasks
33
+ accepts_nested_attributes_for :tasks
34
+ end
35
+
36
+ ActiveRecord::Schema.define do
37
+ create_table :tasks, :force => true do |t|
38
+ t.integer :project_id
39
+ t.string :name
40
+ end
41
+ end
42
+
43
+ class Task < ActiveRecord::Base
44
+ # column :project_id, :integer
45
+ # column :name, :string
46
+ belongs_to :project
47
+ end
48
+
49
+ # app = Class.new(Rails::Application)
50
+ # app.config.secret_token = "token"
51
+ # app.config.session_store :cookie_store, :key => "_myapp_session"
52
+ # app.config.active_support.deprecation = :log
53
+ # app.config.action_controller.perform_caching = false
54
+ # app.initialize!
metadata CHANGED
@@ -1,82 +1,129 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: query_tracer
3
- version: !ruby/object:Gem::Version
4
- hash: 23
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 4
10
- version: 0.0.4
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Dmitry Shaposhnik
14
9
  - Dmytro Shteflyuk
15
10
  autorequire:
16
11
  bindir: bin
17
12
  cert_chain: []
18
-
19
- date: 2011-09-19 00:00:00 Z
20
- dependencies: []
21
-
22
- description: ""
23
- email:
13
+ date: 2011-12-14 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &70176486865720 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70176486865720
26
+ - !ruby/object:Gem::Dependency
27
+ name: activerecord
28
+ requirement: &70176486865120 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *70176486865120
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ requirement: &70176486864640 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *70176486864640
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &70176486863640 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *70176486863640
59
+ - !ruby/object:Gem::Dependency
60
+ name: capybara
61
+ requirement: &70176486863100 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70176486863100
70
+ description: This library helps to find where in your rails app sql query was run.
71
+ email:
24
72
  - dmitry@shaposhnik.name
25
73
  - kpumuk@kpumuk.info
26
74
  executables: []
27
-
28
75
  extensions: []
29
-
30
76
  extra_rdoc_files: []
31
-
32
- files:
77
+ files:
33
78
  - .gitignore
79
+ - .rspec
80
+ - .travis.yml
34
81
  - Gemfile
35
82
  - README.markdown
36
83
  - Rakefile
37
84
  - lib/query_tracer.rb
38
85
  - lib/query_tracer/db.rb
39
86
  - lib/query_tracer/db/mysql.rb
87
+ - lib/query_tracer/db/mysql2.rb
40
88
  - lib/query_tracer/db/sqlite.rb
89
+ - lib/query_tracer/db/sqlite3.rb
41
90
  - lib/query_tracer/logger.rb
42
91
  - lib/query_tracer/tracer.rb
43
92
  - lib/query_tracer/tracer/revision.rb
93
+ - lib/query_tracer/version.rb
44
94
  - query_tracer.gemspec
45
- - test/test_helper.rb
95
+ - spec/query_tracer/logger_spec.rb
96
+ - spec/query_tracer/query_tracer_spec.rb
97
+ - spec/query_tracer/tracer_spec.rb
98
+ - spec/spec_helper.rb
46
99
  homepage: http://github.com/daemon/query_tracer
47
100
  licenses: []
48
-
49
101
  post_install_message:
50
102
  rdoc_options: []
51
-
52
- require_paths:
103
+ require_paths:
53
104
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
105
+ required_ruby_version: !ruby/object:Gem::Requirement
55
106
  none: false
56
- requirements:
57
- - - ">="
58
- - !ruby/object:Gem::Version
59
- hash: 3
60
- segments:
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ segments:
61
112
  - 0
62
- version: "0"
63
- required_rubygems_version: !ruby/object:Gem::Requirement
113
+ hash: 712837335364335563
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
115
  none: false
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- hash: 23
69
- segments:
70
- - 1
71
- - 3
72
- - 6
73
- version: 1.3.6
116
+ requirements:
117
+ - - ! '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ segments:
121
+ - 0
122
+ hash: 712837335364335563
74
123
  requirements: []
75
-
76
124
  rubyforge_project:
77
- rubygems_version: 1.8.6
125
+ rubygems_version: 1.8.10
78
126
  signing_key:
79
127
  specification_version: 3
80
128
  summary: Query tracer and logger for Rails3
81
129
  test_files: []
82
-
File without changes