active_record_shards 2.7.1 → 2.7.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  ActiveRecord Shards is an extension for ActiveRecord that provides support for sharded database and slaves. Basically it is just a nice way to
4
4
  switch between database connection. We've made the implementation very small, and have tried not to reinvent any wheels already present in ActiveRecord.
5
5
 
6
- ActiveRecord Shards has only been used and tested on Rails 2.3.x, and has in some form or another been used on in production on a large rails app for
7
- more than a year!
6
+ ActiveRecord Shards has used and tested on Rails 2.3.x, 3.0.x, and 3.2.x and has in some form or another been used on in production on a large rails app for
7
+ more than a year.
8
8
 
9
9
  ## Installation
10
10
 
@@ -1,6 +1,6 @@
1
1
  module ActiveRecordShards
2
2
  module DefaultSlavePatches
3
- CLASS_SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate, :find_one, :find_some, :find_every, :quote_value, :sanitize_sql_hash_for_conditions, :exists? ]
3
+ CLASS_SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate, :find_one, :find_some, :find_every, :quote_value, :sanitize_sql_hash_for_conditions, :exists? ]
4
4
 
5
5
  def self.extended(base)
6
6
  base_methods = (base.methods | base.private_methods).map(&:to_sym)
@@ -94,5 +94,19 @@ module ActiveRecordShards
94
94
  yield
95
95
  end
96
96
  end
97
+
98
+ module ActiveRelationPatches
99
+ def self.included(base)
100
+ base.send :alias_method_chain, :calculate, :default_slave
101
+ end
102
+
103
+ def on_slave_unless_tx
104
+ @klass.on_slave_unless_tx { yield }
105
+ end
106
+
107
+ def calculate_with_default_slave(*args, &block)
108
+ on_slave_unless_tx { calculate_without_default_slave(*args, &block) }
109
+ end
110
+ end
97
111
  end
98
112
  end
@@ -18,6 +18,10 @@ ActiveRecord::Base.extend(ActiveRecordShards::Model)
18
18
  ActiveRecord::Base.extend(ActiveRecordShards::ConnectionSwitcher)
19
19
  ActiveRecord::Base.extend(ActiveRecordShards::DefaultSlavePatches)
20
20
 
21
+ if ActiveRecord.const_defined?(:Relation)
22
+ ActiveRecord::Relation.send(:include, ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
23
+ end
24
+
21
25
  if ActiveRecord::VERSION::STRING >= "3.1.0"
22
26
  ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
23
27
  else
@@ -402,6 +402,14 @@ class ConnectionSwitchingTest < ActiveSupport::TestCase
402
402
  end
403
403
  end
404
404
 
405
+ should "count associations on the slave" do
406
+ AccountThing.on_slave_by_default = true
407
+ Account.on_slave.connection.execute("INSERT INTO account_things (id, account_id) VALUES(123123, 1000)")
408
+ Account.on_slave.connection.execute("INSERT INTO account_things (id, account_id) VALUES(123124, 1000)")
409
+ assert_equal 2, Account.find(1000).account_things.size
410
+ AccountThing.on_slave_by_default = false
411
+ end
412
+
405
413
  should "Allow override using on_master" do
406
414
  model = Account.on_master.find(1000)
407
415
  assert_equal "master_name", model.name
data/test/models.rb CHANGED
@@ -3,6 +3,11 @@ class Account < ActiveRecord::Base
3
3
  not_sharded
4
4
 
5
5
  has_many :tickets
6
+ has_many :account_things
7
+ end
8
+
9
+ class AccountThing < ActiveRecord::Base
10
+ not_sharded
6
11
  end
7
12
 
8
13
  class Email < ActiveRecord::Base
data/test/schema.rb CHANGED
@@ -7,6 +7,10 @@ ActiveRecord::Schema.define(:version => 1) do
7
7
  t.datetime "updated_at"
8
8
  end
9
9
 
10
+ create_table "account_things", :force => true do |t|
11
+ t.integer "account_id"
12
+ end
13
+
10
14
  create_table "emails", :force => true do |t|
11
15
  t.string "from"
12
16
  t.string "to"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1
4
+ version: 2.7.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-03-13 00:00:00.000000000 Z
14
+ date: 2013-03-14 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord