db_debug 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/db_debug/active_record.rb +28 -26
- data/lib/db_debug/engine.rb +2 -2
- data/lib/db_debug/logger.rb +11 -1
- data/lib/db_debug/middleware.rb +6 -16
- data/lib/db_debug/version.rb +2 -2
- data/lib/db_debug.rb +17 -2
- metadata +2 -3
- data/lib/db_debug/action_controller.rb +0 -41
@@ -1,40 +1,42 @@
|
|
1
|
-
|
1
|
+
require 'active_record/connection_adapters/postgresql_adapter'
|
2
|
+
|
3
|
+
class DbDebug
|
2
4
|
module ActiveRecord
|
3
|
-
module
|
4
|
-
|
5
|
-
def included(klass)
|
6
|
-
klass.extend ClassMethods
|
5
|
+
module ConnectionAdapters
|
6
|
+
module PostgreSQLAdapter
|
7
|
+
def self.included(klass)
|
7
8
|
klass.class_eval do
|
8
|
-
|
9
|
-
alias_method_chain :find_by_sql, :record_counter
|
10
|
-
end
|
9
|
+
alias_method_chain :exec_query, :db_debug
|
11
10
|
end
|
12
|
-
include InstanceMethods
|
13
11
|
end
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if
|
23
|
-
Logger.log :white, "
|
12
|
+
|
13
|
+
def exec_query_with_db_debug(sql, name = 'SQL', binds = [])
|
14
|
+
if DbDebug.enabled
|
15
|
+
DbDebug.count += 1
|
16
|
+
start = Time.now
|
17
|
+
res = exec_query_without_db_debug(sql, name, binds)
|
18
|
+
time = (Time.now - start) * 1000
|
19
|
+
|
20
|
+
if DbDebug.verbose
|
21
|
+
Logger.log :white, ""
|
22
|
+
Logger.log :red, "DB CALL " + "="*72
|
23
|
+
Logger.log :green, "SQL: #{sql.split("\n").join("").strip}"
|
24
|
+
Logger.log :green, "BINDS: #{binds}" if binds.present?
|
25
|
+
Logger.log :white, "TIME: #{time} ms"
|
24
26
|
Logger.log :white, caller.delete_if{ |str| str.include?("/gems/") || str.include?("script/rails") }.join("\n")
|
27
|
+
Logger.log :red, "="*80
|
28
|
+
Logger.log :white, ""
|
29
|
+
|
25
30
|
end
|
26
|
-
|
27
|
-
|
31
|
+
DbDebug.time += time
|
32
|
+
res
|
28
33
|
else
|
29
|
-
|
34
|
+
exec_query_without_db_debug(sql, name, binds)
|
30
35
|
end
|
31
36
|
end
|
32
37
|
end
|
33
|
-
|
34
|
-
module InstanceMethods
|
35
|
-
end
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
ActiveRecord::
|
42
|
+
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.send(:include, DbDebug::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
data/lib/db_debug/engine.rb
CHANGED
data/lib/db_debug/logger.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class DbDebug
|
2
2
|
module Logger
|
3
3
|
COLOR_MAP = {
|
4
4
|
:black => 30,
|
@@ -15,5 +15,15 @@ module DbDebug
|
|
15
15
|
code = COLOR_MAP[color]
|
16
16
|
Rails.logger.debug "\033[0;#{code}m#{str}\033[0;#37m"
|
17
17
|
end
|
18
|
+
|
19
|
+
def self.determine_color count, time
|
20
|
+
if count <= 10 || time < 50.0
|
21
|
+
:green
|
22
|
+
elsif count <= 20 || time < 100.0
|
23
|
+
:yellow
|
24
|
+
else
|
25
|
+
:red
|
26
|
+
end
|
27
|
+
end
|
18
28
|
end
|
19
29
|
end
|
data/lib/db_debug/middleware.rb
CHANGED
@@ -1,25 +1,15 @@
|
|
1
|
-
|
1
|
+
class DbDebug
|
2
2
|
class Middleware
|
3
3
|
def initialize(app)
|
4
4
|
@app = app
|
5
5
|
end
|
6
6
|
|
7
7
|
def call(env)
|
8
|
-
env[
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
resp
|
14
|
-
end
|
15
|
-
|
16
|
-
def determine_color count, time
|
17
|
-
if count <= 10 || time < 50.0
|
18
|
-
:green
|
19
|
-
elsif count <= 20 || time < 100.0
|
20
|
-
:yellow
|
21
|
-
else
|
22
|
-
:red
|
8
|
+
db_debug = env["QUERY_STRING"].gsub!(/&db_debug/, "")
|
9
|
+
|
10
|
+
|
11
|
+
DbDebug.trace verbose: db_debug.present? do
|
12
|
+
@app.call(env)
|
23
13
|
end
|
24
14
|
end
|
25
15
|
end
|
data/lib/db_debug/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.0.
|
1
|
+
class DbDebug
|
2
|
+
VERSION = "0.0.4"
|
3
3
|
end
|
data/lib/db_debug.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
require "db_debug/middleware"
|
2
2
|
require "db_debug/active_record"
|
3
|
-
require "db_debug/action_controller"
|
4
3
|
require "db_debug/version"
|
5
4
|
require "db_debug/logger"
|
6
5
|
require "db_debug/engine"
|
7
6
|
|
8
|
-
|
7
|
+
class DbDebug
|
8
|
+
cattr_accessor :count, :time, :verbose, :enabled
|
9
|
+
|
10
|
+
def self.trace opts = {}
|
11
|
+
DbDebug.enabled = true
|
12
|
+
DbDebug.count = opts[:count] || 0
|
13
|
+
DbDebug.time = opts[:time] || 0
|
14
|
+
DbDebug.verbose = opts[:verbose] || false
|
15
|
+
|
16
|
+
res = yield
|
17
|
+
|
18
|
+
color = Logger.determine_color DbDebug.count, DbDebug.time
|
19
|
+
Logger.log color, "You hit the database #{DbDebug.count} times and it took %5.3f ms" % DbDebug.time
|
20
|
+
|
21
|
+
DbDebug.enabled = false
|
22
|
+
res
|
23
|
+
end
|
9
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_debug
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-07-
|
13
|
+
date: 2012-07-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -35,7 +35,6 @@ executables: []
|
|
35
35
|
extensions: []
|
36
36
|
extra_rdoc_files: []
|
37
37
|
files:
|
38
|
-
- lib/db_debug/action_controller.rb
|
39
38
|
- lib/db_debug/active_record.rb
|
40
39
|
- lib/db_debug/engine.rb
|
41
40
|
- lib/db_debug/logger.rb
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module DbDebug
|
2
|
-
module ActionController
|
3
|
-
module Base
|
4
|
-
class << self
|
5
|
-
def included(klass)
|
6
|
-
klass.class_eval do
|
7
|
-
around_filter :hack_to_pass_shit_to_rack_middleware
|
8
|
-
|
9
|
-
protected
|
10
|
-
|
11
|
-
def hack_to_pass_shit_to_rack_middleware
|
12
|
-
klasses = [ActiveRecord::Base, ActiveRecord::Base.class]
|
13
|
-
methods = ["session", "cookies", "params", "request"]
|
14
|
-
|
15
|
-
methods.each do |shenanigan|
|
16
|
-
oops = instance_variable_get(:"@_#{shenanigan}")
|
17
|
-
|
18
|
-
klasses.each do |klass|
|
19
|
-
klass.send(:define_method, shenanigan, proc { oops })
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
yield
|
24
|
-
|
25
|
-
methods.each do |shenanigan|
|
26
|
-
klasses.each do |klass|
|
27
|
-
klass.send :remove_method, shenanigan
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
ActionController::Base.send(:include, DbDebug::ActionController::Base)
|