arproxy-query_caller_location_annotator 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f69f117c8f97f8e92c07576b64f95ed26b2f945e
4
+ data.tar.gz: c5df8b5ea7f07e2ce0bd7ba8936d79a81a5367e9
5
+ SHA512:
6
+ metadata.gz: 07d2de906a9c3646818b94ada3d650784ce4be73334ddb125a33ec2fc3224ef3c778ea38ddd4c2574539622e3a9885846e2c583ccc8cecfc7f727c3f038c7b44
7
+ data.tar.gz: 18a4875a7f99d85bdd1049343539cb0ddb55d40bca4443177ccb5dff1ac505faed07c769c63cec97443c8e348c7206bd6b1f0a947397642b7f9006813fc7c78b
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ sudo: false
3
+ branches:
4
+ only:
5
+ - master
6
+ rvm:
7
+ - "2.2.5"
8
+ - "2.3.1"
9
+ services:
10
+ - mysql
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ ## CHANGELOG
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in arproxy-query_caller_location_annotator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Taiki Ono
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Arproxy::QueryCallerLocationAnnotator
2
+ [![Build Status](https://travis-ci.org/taiki45/arproxy-query_caller_location_annotator.svg?branch=master)](https://travis-ci.org/taiki45/arproxy-query_caller_location_annotator)
3
+
4
+ Append query caller method to each ActiveRecord's query log like:
5
+
6
+ ```
7
+ User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1 /* app/models/user.rb:3 `xxx` */
8
+ ```
9
+
10
+ This library was originally written by Takatoshi Maeda (@TakatoshiMaeda on GitHub).
11
+
12
+ ## Installation
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'arproxy-query_caller_location_annotator'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ ## Usage
24
+ arproxy-query_caller_location_annotator will be setup automatically if you use mysql2 adapter. You can disable it by specifying env var as `DISABLE_QUERY_LOCATION_ANNOTATE=1`.
25
+
26
+ If you want to setup manually, you can change Gemfile to `require: false` then write a initializer by hand:
27
+
28
+ ```
29
+ # In config/initializers/arpoxy.rb
30
+ require 'arproxy/query_caller_location_annotator/proxy'
31
+
32
+ if ::Rails.env.development? || ::Rails.env.test?
33
+ ::Arproxy.configure do |config|
34
+ config.adapter = 'postgresql' # Your database adapter
35
+ config.use ::Arproxy::QueryCallerLocationAnnotator::Proxy
36
+ end
37
+
38
+ ::ActiveSupport.on_load :active_record do
39
+ ::Arproxy.enable!
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## Contributing
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taiki45/arproxy-query_caller_location_annotator.
46
+
47
+ ## License
48
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ desc 'Run Rails integration test'
6
+ task :test do
7
+ exit(1) unless ruby('rails_integration_test.rb')
8
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'arproxy/query_caller_location_annotator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'arproxy-query_caller_location_annotator'
8
+ spec.version = Arproxy::QueryCallerLocationAnnotator::VERSION
9
+ spec.authors = ['Takatoshi Maeda', 'Taiki Ono']
10
+ spec.email = ['taiks.4559@gmail.com']
11
+
12
+ spec.summary = %q{Append query caller to each ActiveRecord's query log.}
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/taiki45/arproxy-query_caller_location_annotator'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'arproxy'
23
+ spec.add_dependency 'rails'
24
+ spec.add_development_dependency 'bundler', '~> 1.12'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "arproxy/query_caller_location_annotator"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,2 @@
1
+ require 'arproxy/query_caller_location_annotator/proxy'
2
+ require 'arproxy/query_caller_location_annotator/railtie'
@@ -0,0 +1,37 @@
1
+ require 'arproxy'
2
+
3
+ module Arproxy
4
+ module QueryCallerLocationAnnotator
5
+ class Proxy < ::Arproxy::Base
6
+ def execute(sql, name=nil)
7
+ return super(sql, name) unless sql =~ /^(SELECT|INSERT|UPDATE|DELETE)/
8
+
9
+ location = query_caller_location
10
+ if location.present?
11
+ super("#{sql} /* #{location} */", name)
12
+ else
13
+ super(sql, name)
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def query_caller_location
20
+ location = caller_locations.find { |l| annotate_location_regexp.match(l.absolute_path) }
21
+ if location.nil?
22
+ nil
23
+ else
24
+ "app#{location.absolute_path.gsub(annotate_match_location, '')}:#{location.lineno} `#{location.label}`"
25
+ end
26
+ end
27
+
28
+ def annotate_match_location
29
+ @annotate_match_location ||= ::Rails.root.join('app').to_s.freeze
30
+ end
31
+
32
+ def annotate_location_regexp
33
+ @annotate_location_regexp ||= /\A#{Regexp.quote(annotate_match_location)}/.freeze
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ module Arproxy
2
+ module QueryCallerLocationAnnotator
3
+ class Railtie < ::Rails::Railtie
4
+ initializer 'arproxy.query_caller_location_annotator' do
5
+ if (::Rails.env.development? || ::Rails.env.test?) && !(ENV['DISABLE_QUERY_LOCATION_ANNOTATE'] == '1')
6
+ ::Arproxy.configure do |config|
7
+ config.adapter = 'mysql2'
8
+ config.use ::Arproxy::QueryCallerLocationAnnotator::Proxy
9
+ end
10
+
11
+ ::ActiveSupport.on_load :active_record do
12
+ ::Arproxy.enable!
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Arproxy
2
+ module QueryCallerLocationAnnotator
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,54 @@
1
+ require 'fileutils'
2
+ require 'open3'
3
+
4
+ def execute_without_bundler
5
+ defined?(Bundler) ? Bundler.with_clean_env { yield } : yield
6
+ end
7
+
8
+ name = 'test_app'
9
+
10
+ execute_without_bundler do
11
+ system('bundle install -j8')
12
+ FileUtils.rm_rf('tmp')
13
+ FileUtils.mkdir_p('tmp')
14
+ Dir.chdir('tmp') do
15
+ system("bundle exec rails new #{name} --skip-bundle")
16
+ Dir.chdir(name) do
17
+ lines = File.read('Gemfile').split("\n")
18
+ lines.reject! {|l| l.match(/sqlite3/) }
19
+ new = lines.first(1) + [
20
+ %!gem "mysql2"!,
21
+ %!gem "arproxy-query_caller_location_annotator", path: "#{__dir__}"!,
22
+ ] + lines.drop(1)
23
+ File.open('Gemfile', 'w') {|f| f.puts(new.join("\n")) }
24
+
25
+ database_config = File.read('config/database.yml')
26
+ database_config.gsub!('adapter: sqlite3', 'adapter: mysql2')
27
+ database_config.gsub!(%r{database: db/\w+.sqlite3}, 'database: arproxy_test_app')
28
+ File.write('config/database.yml', database_config)
29
+
30
+ system('bundle install -j8')
31
+ system('bin/rails db:drop db:setup')
32
+ system('bin/rails g model user name:string')
33
+ system('bin/rails db:migrate')
34
+
35
+ File.write('app/models/user.rb', <<-EOF)
36
+ class User < ApplicationRecord
37
+ def self.xxx
38
+ first
39
+ end
40
+ end
41
+ EOF
42
+
43
+ config = File.read('config/environments/development.rb')
44
+ config << "\nRails.application.config.logger = Logger.new(STDOUT)\n"
45
+ File.write('config/environments/development.rb', config)
46
+
47
+ log, e, s = Open3.capture3(%!bin/rails runner 'User.xxx'!)
48
+
49
+ raise("Failed to run script:\n#{e}") unless s.success?
50
+ raise("File path or line number is missing:\n#{log}") unless log.include?('app/models/user.rb:3')
51
+ raise("Method name is missing:\n#{log}") unless log.include?('xxx')
52
+ end
53
+ end
54
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: arproxy-query_caller_location_annotator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takatoshi Maeda
8
+ - Taiki Ono
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: arproxy
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rails
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.12'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.12'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ description: Append query caller to each ActiveRecord's query log.
71
+ email:
72
+ - taiks.4559@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - arproxy-query_caller_location_annotator.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/arproxy/query_caller_location_annotator.rb
88
+ - lib/arproxy/query_caller_location_annotator/proxy.rb
89
+ - lib/arproxy/query_caller_location_annotator/railtie.rb
90
+ - lib/arproxy/query_caller_location_annotator/version.rb
91
+ - rails_integration_test.rb
92
+ homepage: https://github.com/taiki45/arproxy-query_caller_location_annotator
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Append query caller to each ActiveRecord's query log.
116
+ test_files: []