master_slave_adapter_soundcloud 0.1.10 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ # 0.2.0 (April 2, 2012)
2
+
3
+ * Add support for ActiveRecord's query cache
4
+
1
5
  # 0.1.10 (March 06, 2012)
2
6
 
3
7
  * Delegate #visitor to master connection
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.10
1
+ 0.2.0
@@ -61,7 +61,7 @@ module ActiveRecord
61
61
  end
62
62
 
63
63
  def load_adapter(adapter_name)
64
- unless self.respond_to?("#{adapter_name}_connection")
64
+ unless respond_to?("#{adapter_name}_connection")
65
65
  begin
66
66
  require 'rubygems'
67
67
  gem "activerecord-#{adapter_name}-adapter"
@@ -135,11 +135,11 @@ module ActiveRecord
135
135
  # MASTER SLAVE ADAPTER INTERFACE ========================================
136
136
 
137
137
  def with_master
138
- with(self.master_connection, :master) { yield }
138
+ with(master_connection) { yield }
139
139
  end
140
140
 
141
141
  def with_slave
142
- with(self.slave_connection!, :slave) { yield }
142
+ with(slave_connection!) { yield }
143
143
  end
144
144
 
145
145
  def with_consistency(clock)
@@ -148,12 +148,12 @@ module ActiveRecord
148
148
  slave = slave_connection!
149
149
  conn =
150
150
  if !open_transaction? && slave_consistent?(slave, clock)
151
- [ slave, :slave ]
151
+ slave
152
152
  else
153
- [ master_connection, :master ]
153
+ master_connection
154
154
  end
155
155
 
156
- with(*conn) { yield }
156
+ with(conn) { yield }
157
157
 
158
158
  self.current_clock || clock
159
159
  end
@@ -208,25 +208,41 @@ module ActiveRecord
208
208
 
209
209
  def rollback_db_transaction
210
210
  on_commit_callbacks.clear
211
- with(master_connection, :master) { |conn| conn.rollback_db_transaction }
211
+ with(master_connection) { |conn| conn.rollback_db_transaction }
212
212
  on_rollback_callbacks.shift.call until on_rollback_callbacks.blank?
213
213
  end
214
214
 
215
215
  def active?
216
216
  return true if @disable_connection_test
217
- self.connections.map { |c| c.active? }.reduce(true) { |m,s| s ? m : s }
217
+ connections.map { |c| c.active? }.all?
218
218
  end
219
219
 
220
220
  def reconnect!
221
- self.connections.each { |c| c.reconnect! }
221
+ connections.each { |c| c.reconnect! }
222
222
  end
223
223
 
224
224
  def disconnect!
225
- self.connections.each { |c| c.disconnect! }
225
+ connections.each { |c| c.disconnect! }
226
226
  end
227
227
 
228
228
  def reset!
229
- self.connections.each { |c| c.reset! }
229
+ connections.each { |c| c.reset! }
230
+ end
231
+
232
+ def cache(&block)
233
+ connections.inject(block) do |block, connection|
234
+ lambda { connection.cache(&block) }
235
+ end.call
236
+ end
237
+
238
+ def uncached(&block)
239
+ connections.inject(block) do |block, connection|
240
+ lambda { connection.uncached(&block) }
241
+ end.call
242
+ end
243
+
244
+ def clear_query_cache
245
+ connections.each { |connection| connection.clear_query_cache }
230
246
  end
231
247
 
232
248
  # Someone calling execute directly on the connection is likely to be a
@@ -282,13 +298,13 @@ module ActiveRecord
282
298
  # ok, we might have missed more
283
299
  def method_missing(name, *args, &blk)
284
300
  master_connection.send(name.to_sym, *args, &blk).tap do
285
- warn %Q{
301
+ @logger.try(:warn, %Q{
286
302
  You called the unsupported method '#{name}' on #{self.class.name}.
287
303
  In order to help us improve master_slave_adapter, please report this
288
304
  to: https://github.com/soundcloud/master_slave_adapter/issues
289
305
 
290
306
  Thank you.
291
- }
307
+ })
292
308
  end
293
309
  end
294
310
 
@@ -301,7 +317,7 @@ module ActiveRecord
301
317
  :to => :connection_for_read
302
318
 
303
319
  def connection_for_read
304
- open_transaction? ? master_connection : self.current_connection
320
+ open_transaction? ? master_connection : current_connection
305
321
  end
306
322
  private :connection_for_read
307
323
 
@@ -366,7 +382,7 @@ module ActiveRecord
366
382
  protected
367
383
 
368
384
  def on_write
369
- with(master_connection, :master) do |conn|
385
+ with(master_connection) do |conn|
370
386
  yield(conn).tap do
371
387
  unless open_transaction?
372
388
  if mc = master_clock
@@ -379,29 +395,13 @@ module ActiveRecord
379
395
  end
380
396
  end
381
397
 
382
- def with(conn, name)
398
+ def with(conn)
383
399
  self.current_connection = conn
384
400
  yield(conn).tap { connection_stack.shift }
385
401
  end
386
402
 
387
403
  private
388
404
 
389
- def logger
390
- @logger # ||= Logger.new(STDOUT)
391
- end
392
-
393
- def info(msg)
394
- logger.try(:info, msg)
395
- end
396
-
397
- def warn(msg)
398
- logger.try(:warn, msg)
399
- end
400
-
401
- def debug(msg)
402
- logger.debug(msg) if logger && logger.debug?
403
- end
404
-
405
405
  def connect(cfg, name)
406
406
  adapter_method = "#{cfg.fetch(:adapter)}_connection".to_sym
407
407
  ActiveRecord::Base.send(adapter_method, { :name => name }.merge(cfg))
@@ -3,7 +3,6 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'master_slave_adapter_soundcloud'
5
5
  s.version = File.read('VERSION').to_s
6
- s.date = '2011-11-15'
7
6
  s.platform = Gem::Platform::RUBY
8
7
  s.authors = [ 'Mauricio Linhares', 'Torsten Curdt', 'Kim Altintop', 'Omid Aladini', 'SoundCloud' ]
9
8
  s.email = %q{kim@soundcloud.com tcurdt@soundcloud.com omid@soundcloud.com}
@@ -44,14 +44,14 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
44
44
  'master connection',
45
45
  mocked_methods.merge(:open_transactions => 0)
46
46
  ).tap do |conn|
47
- conn.stub!(:uncached) { |blk| blk.call }
47
+ conn.stub!(:uncached).and_yield
48
48
  ActiveRecord::Base.master_mock = conn
49
49
  end
50
50
  end
51
51
 
52
52
  let!(:slave_connection) do
53
53
  mock('slave connection', mocked_methods).tap do |conn|
54
- conn.stub!(:uncached) { |blk| blk.call }
54
+ conn.stub!(:uncached).and_yield
55
55
  ActiveRecord::Base.slave_mock = conn
56
56
  end
57
57
  end
@@ -77,7 +77,7 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
77
77
  ActiveRecord::Base.connection_handler.clear_all_connections!
78
78
  end
79
79
 
80
- describe 'with common configuration' do
80
+ describe 'common configuration' do
81
81
  before do
82
82
  [ master_connection, slave_connection ].each do |c|
83
83
  c.stub!( :select_value ).with( "SELECT 1", "test select" ).and_return( true )
@@ -160,7 +160,7 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
160
160
  end
161
161
  end
162
162
 
163
- context "connection testing" do
163
+ describe "connection testing" do
164
164
  context "disabled" do
165
165
  let(:database_setup) do
166
166
  default_database_setup.merge(:disable_connection_test => 'true')
@@ -198,7 +198,7 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
198
198
  end
199
199
  end
200
200
 
201
- describe 'with consistency' do
201
+ describe 'consistency' do
202
202
  before do
203
203
  ActiveRecord::ConnectionAdapters::MasterSlaveAdapter.reset!
204
204
 
@@ -287,7 +287,6 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
287
287
  new_clock.should be_a(zero.class)
288
288
  new_clock.should > old_clock
289
289
  end
290
-
291
290
  end
292
291
 
293
292
  it "should update the clock after a transaction" do
@@ -301,7 +300,7 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
301
300
  master_connection.
302
301
  should_receive('update').exactly(3).times.with('testing').
303
302
  and_return(true)
304
- master_connection.
303
+ master_connection.
305
304
  should_receive('select_all').exactly(5).times.with('testing').
306
305
  and_return(true)
307
306
  %w(begin_db_transaction
@@ -512,7 +511,7 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
512
511
  end
513
512
  end
514
513
 
515
- context "rollback" do
514
+ context "on rollback" do
516
515
  it "on_commit callback should not be called" do
517
516
  x = false
518
517
  adapter_connection.on_commit { x = true }
@@ -526,4 +525,41 @@ describe ActiveRecord::ConnectionAdapters::MasterSlaveAdapter do
526
525
  end
527
526
  end
528
527
  end
528
+
529
+ describe "query cache" do
530
+ describe "#cache" do
531
+ it "activities query caching on all connections" do
532
+ master_connection.should_receive(:cache).and_yield
533
+ slave_connection.should_receive(:cache).and_yield
534
+ master_connection.should_not_receive(:select_value)
535
+ slave_connection.should_receive(:select_value)
536
+
537
+ adapter_connection.cache do
538
+ adapter_connection.select_value("SELECT 42")
539
+ end
540
+ end
541
+ end
542
+
543
+ describe "#uncached" do
544
+ it "deactivates query caching on all connections" do
545
+ master_connection.should_receive(:uncached).and_yield
546
+ slave_connection.should_receive(:uncached).and_yield
547
+ master_connection.should_not_receive(:select_value)
548
+ slave_connection.should_receive(:select_value)
549
+
550
+ adapter_connection.uncached do
551
+ adapter_connection.select_value("SELECT 42")
552
+ end
553
+ end
554
+ end
555
+
556
+ describe "#clear_query_cache" do
557
+ it "clears the query cache on all connections" do
558
+ master_connection.should_receive(:clear_query_cache)
559
+ slave_connection.should_receive(:clear_query_cache)
560
+
561
+ adapter_connection.clear_query_cache
562
+ end
563
+ end
564
+ end
529
565
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: master_slave_adapter_soundcloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,11 +13,11 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2011-11-15 00:00:00.000000000 Z
16
+ date: 2012-05-02 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: rspec
20
- requirement: &70148912458220 !ruby/object:Gem::Requirement
20
+ requirement: !ruby/object:Gem::Requirement
21
21
  none: false
22
22
  requirements:
23
23
  - - ! '>='
@@ -25,10 +25,15 @@ dependencies:
25
25
  version: '0'
26
26
  type: :development
27
27
  prerelease: false
28
- version_requirements: *70148912458220
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
29
34
  - !ruby/object:Gem::Dependency
30
35
  name: activerecord
31
- requirement: &70148912457360 !ruby/object:Gem::Requirement
36
+ requirement: !ruby/object:Gem::Requirement
32
37
  none: false
33
38
  requirements:
34
39
  - - ~>
@@ -36,7 +41,12 @@ dependencies:
36
41
  version: 2.3.9
37
42
  type: :runtime
38
43
  prerelease: false
39
- version_requirements: *70148912457360
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ~>
48
+ - !ruby/object:Gem::Version
49
+ version: 2.3.9
40
50
  description: (MySQL) Replication Aware Master/Slave Database Adapter for Rails/ActiveRecord
41
51
  email: kim@soundcloud.com tcurdt@soundcloud.com omid@soundcloud.com
42
52
  executables: []
@@ -75,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
85
  version: 1.3.7
76
86
  requirements: []
77
87
  rubyforge_project:
78
- rubygems_version: 1.8.15
88
+ rubygems_version: 1.8.21
79
89
  signing_key:
80
90
  specification_version: 3
81
91
  summary: Replication Aware Master/Slave Database Adapter for Rails/ActiveRecord