marginalia 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of marginalia might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3a294ff04a314d5e24a21f5995e3e2913d25d5da
4
- data.tar.gz: dd9e9e133e72f570379eec9936d8d0fe794de4e3
3
+ metadata.gz: b4d1334e6fae088b614c33883053c675e8ce3208
4
+ data.tar.gz: ea48070a0ad0b210045ff6cc8eb485d9db115e3a
5
5
  SHA512:
6
- metadata.gz: 7b1b2e1a5da6e9efe3043bdf2485b20990737d65a4d5ea6f86b9cae3a9c7415dd4c7b31702d48625f2f6edd8b15c57c98842819726bef55d347939da5d154256
7
- data.tar.gz: a228a5805de1281142fa85f8c6e9b416956ef8acca7e2e2c6e84b59cbc55f634bfacebdf5228d38f36b1102666b50841f628995b96acfc7b297ed0cc4670a6d0
6
+ metadata.gz: 06d3dc8986bdfc70351ece60860aa79e4822f83e0cbcc0cb8211c9197dbf4fa8320f1017f15cbfa9aac40c296ae6a37f8054aa566fac832fe2f02d628d65d073
7
+ data.tar.gz: fdee2ee3f3e0bec89b52e54c22056811d7bd4e4cd167d6ef38e00446f25c16cc03f3cd730f4e8456a32ba67ff66b39046a2adb289acb0c9d4a8fc149b75957ba
@@ -3,7 +3,7 @@ language: ruby
3
3
  rvm:
4
4
  - 2.1
5
5
  - 2.2
6
- - 2.3
6
+ - 2.3.1
7
7
 
8
8
  sudo: false
9
9
 
data/README.md CHANGED
@@ -93,6 +93,11 @@ default. In addition, implementation is provided for:
93
93
  * `:hostname` to include ```Socket.gethostname```.
94
94
  * `:pid` to include current process id.
95
95
 
96
+ With ActiveRecord >= 3.2.19:
97
+ * `:db_host` to include the configured database hostname.
98
+ * `:socket` to include the configured database socket.
99
+ * `:database` to include the configured database name.
100
+
96
101
  Pull requests for other included comment components are welcome.
97
102
 
98
103
  ## Contributing
@@ -12,34 +12,40 @@ module Marginalia
12
12
  alias_method :execute, :execute_with_marginalia
13
13
  end
14
14
 
15
- is_mysql2 = defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) &&
16
- ActiveRecord::ConnectionAdapters::Mysql2Adapter == instrumented_class
17
- # Dont instrument exec_query on mysql2 and AR 3.2+, as it calls execute internally
18
- unless is_mysql2 && ActiveRecord::VERSION::STRING > "3.1"
19
- if instrumented_class.method_defined?(:exec_query)
20
- alias_method :exec_query_without_marginalia, :exec_query
21
- alias_method :exec_query, :exec_query_with_marginalia
15
+ if instrumented_class.private_method_defined?(:execute_and_clear)
16
+ alias_method :execute_and_clear_without_marginalia, :execute_and_clear
17
+ alias_method :execute_and_clear, :execute_and_clear_with_marginalia
18
+ else
19
+ is_mysql2 = defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) &&
20
+ ActiveRecord::ConnectionAdapters::Mysql2Adapter == instrumented_class
21
+ # Dont instrument exec_query on mysql2 and AR 3.2+, as it calls execute internally
22
+ unless is_mysql2 && ActiveRecord::VERSION::STRING > "3.1"
23
+ if instrumented_class.method_defined?(:exec_query)
24
+ alias_method :exec_query_without_marginalia, :exec_query
25
+ alias_method :exec_query, :exec_query_with_marginalia
26
+ end
22
27
  end
23
- end
24
28
 
25
- is_postgres = defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
26
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter == instrumented_class
27
- # Instrument exec_delete and exec_update on AR 3.2+, since they don't
28
- # call execute internally
29
- if is_postgres && ActiveRecord::VERSION::STRING > "3.1"
30
- if instrumented_class.method_defined?(:exec_delete)
31
- alias_method :exec_delete_without_marginalia, :exec_delete
32
- alias_method :exec_delete, :exec_delete_with_marginalia
33
- end
34
- if instrumented_class.method_defined?(:exec_update)
35
- alias_method :exec_update_without_marginalia, :exec_update
36
- alias_method :exec_update, :exec_update_with_marginalia
29
+ is_postgres = defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
30
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter == instrumented_class
31
+ # Instrument exec_delete and exec_update on AR 3.2+, since they don't
32
+ # call execute internally
33
+ if is_postgres && ActiveRecord::VERSION::STRING > "3.1"
34
+ if instrumented_class.method_defined?(:exec_delete)
35
+ alias_method :exec_delete_without_marginalia, :exec_delete
36
+ alias_method :exec_delete, :exec_delete_with_marginalia
37
+ end
38
+ if instrumented_class.method_defined?(:exec_update)
39
+ alias_method :exec_update_without_marginalia, :exec_update
40
+ alias_method :exec_update, :exec_update_with_marginalia
41
+ end
37
42
  end
38
43
  end
39
44
  end
40
45
  end
41
46
 
42
47
  def annotate_sql(sql)
48
+ Marginalia::Comment.update_adapter!(self)
43
49
  comment = Marginalia::Comment.construct_comment
44
50
  if comment.present? && !sql.include?(comment)
45
51
  "#{sql} /*#{comment}*/"
@@ -70,6 +76,10 @@ module Marginalia
70
76
  def exec_update_with_marginalia(sql, name = 'SQL', binds = [])
71
77
  exec_update_without_marginalia(annotate_sql(sql), name, binds)
72
78
  end
79
+
80
+ def execute_and_clear_with_marginalia(sql, *args, &block)
81
+ execute_and_clear_without_marginalia(annotate_sql(sql), *args, &block)
82
+ end
73
83
  end
74
84
 
75
85
  module ActionControllerInstrumentation
@@ -13,6 +13,10 @@ module Marginalia
13
13
  self.marginalia_job = job
14
14
  end
15
15
 
16
+ def self.update_adapter!(adapter)
17
+ self.marginalia_adapter = adapter
18
+ end
19
+
16
20
  def self.construct_comment
17
21
  ret = ''
18
22
  self.components.each do |c|
@@ -50,6 +54,14 @@ module Marginalia
50
54
  Thread.current[:marginalia_job]
51
55
  end
52
56
 
57
+ def self.marginalia_adapter=(adapter)
58
+ Thread.current[:marginalia_adapter] = adapter
59
+ end
60
+
61
+ def self.marginalia_adapter
62
+ Thread.current[:marginalia_adapter]
63
+ end
64
+
53
65
  def self.application
54
66
  if defined?(Rails.application)
55
67
  Marginalia.application_name ||= Rails.application.class.name.split("::").first
@@ -109,6 +121,31 @@ module Marginalia
109
121
  marginalia_controller.request.uuid
110
122
  end
111
123
  end
124
+
125
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.2.19')
126
+ def self.socket
127
+ if self.connection_config.present?
128
+ self.connection_config[:socket]
129
+ end
130
+ end
131
+
132
+ def self.db_host
133
+ if self.connection_config.present?
134
+ self.connection_config[:host]
135
+ end
136
+ end
137
+
138
+ def self.database
139
+ if self.connection_config.present?
140
+ self.connection_config[:database]
141
+ end
142
+ end
143
+
144
+ def self.connection_config
145
+ return if marginalia_adapter.pool.nil?
146
+ marginalia_adapter.pool.spec.config
147
+ end
148
+ end
112
149
  end
113
150
 
114
151
  end
@@ -8,7 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.test_files = `git ls-files -- {test}/*`.split("\n")
9
9
  gem.name = "marginalia"
10
10
  gem.require_paths = ["lib"]
11
- gem.version = "1.4.0"
11
+ gem.version = "1.5.0"
12
12
  gem.license = "MIT"
13
13
 
14
14
  gem.add_runtime_dependency "actionpack", ">= 2.3"
@@ -13,6 +13,10 @@ def active_job_available?
13
13
  Gem::Version.new(Rails::VERSION::STRING) >= Gem::Version.new('4.2')
14
14
  end
15
15
 
16
+ def adapter_pool_available?
17
+ Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('3.2.19')
18
+ end
19
+
16
20
  require "minitest/autorun"
17
21
  require 'mocha/test_unit'
18
22
  require 'logger'
@@ -114,6 +118,12 @@ class MarginaliaTest < MiniTest::Test
114
118
  ActiveRecord::Base.connection.unstub(:annotate_sql)
115
119
  end
116
120
 
121
+ def test_exists
122
+ skip if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('3.2')
123
+ Post.exists?
124
+ assert_match %r{/\*application:rails\*/$}, @queries.last
125
+ end
126
+
117
127
  def test_query_commenting_on_mysql_driver_with_no_action
118
128
  ActiveRecord::Base.connection.execute "select id from posts"
119
129
  assert_match %r{select id from posts /\*application:rails\*/$}, @queries.first
@@ -205,6 +215,30 @@ class MarginaliaTest < MiniTest::Test
205
215
  assert_match %r{/\*controller_with_namespace:API::V1::PostsController}, @queries.first
206
216
  end
207
217
 
218
+ if adapter_pool_available?
219
+ def test_db_host
220
+ Marginalia::Comment.components = [:db_host]
221
+ API::V1::PostsController.action(:driver_only).call(@env)
222
+ assert_match %r{/\*db_host:localhost}, @queries.first
223
+ end
224
+
225
+ def test_database
226
+ Marginalia::Comment.components = [:database]
227
+ API::V1::PostsController.action(:driver_only).call(@env)
228
+ assert_match %r{/\*database:marginalia_test}, @queries.first
229
+ end
230
+
231
+ def test_socket
232
+ # setting socket in configuration would break some connections - mock it instead
233
+ pool = ActiveRecord::Base.connection_pool
234
+ pool.spec.stubs(:config).returns({:socket => "marginalia_socket"})
235
+ Marginalia::Comment.components = [:socket]
236
+ API::V1::PostsController.action(:driver_only).call(@env)
237
+ assert_match %r{/\*socket:marginalia_socket}, @queries.first
238
+ pool.spec.unstub(:config)
239
+ end
240
+ end
241
+
208
242
  if request_id_available?
209
243
  def test_request_id
210
244
  @env["action_dispatch.request_id"] = "some-uuid"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: marginalia
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Noah Lorang
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-08-15 00:00:00.000000000 Z
13
+ date: 2017-09-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: actionpack
@@ -179,7 +179,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
179
  version: '0'
180
180
  requirements: []
181
181
  rubyforge_project:
182
- rubygems_version: 2.6.6
182
+ rubygems_version: 2.6.11
183
183
  signing_key:
184
184
  specification_version: 4
185
185
  summary: Attach comments to your ActiveRecord queries.