active_record_shards 3.0.0.beta1 → 3.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6c4ca6523464fd8739f44b617fd634d0ade3a7e3
4
+ data.tar.gz: d21d6cfa9601754888ff291d367732d4642c4f8f
5
+ SHA512:
6
+ metadata.gz: e8553793c3c910c79ea73251306ed33f41a2bce328a365bd8ee2ca20c644fc78c5b12a700b36c1d6ddaeeb62d34ffc10d5454221c6fba40e3e114d41f4930c94
7
+ data.tar.gz: 6b6725a7d70a810ee440366cd892cc4af3a26ac1cbdab855d5073b6e3c62202994a7c013da7869a16026f73f021c93313728cac7e3e6bc1ce6ae902a25938f99
@@ -8,14 +8,10 @@ require 'active_record_shards/association_collection_connection_selection'
8
8
  require 'active_record_shards/connection_pool'
9
9
  require 'active_record_shards/migration'
10
10
  require 'active_record_shards/default_slave_patches'
11
- require 'active_record_shards/arel_engine'
12
11
  require 'active_record_shards/connection_handler'
12
+ require 'active_record_shards/connection_specification'
13
13
 
14
- if ActiveRecord::VERSION::STRING >= "3.2.0"
15
- require 'active_record_shards/connection_specification'
16
- end
17
-
18
- if ActiveRecord::VERSION::STRING >= "4.0.0"
14
+ if ActiveRecord::VERSION::MAJOR >= 4
19
15
  methods_to_override = [:establish_connection, :remove_connection, :pool_for,
20
16
  :pool_from_any_process_for]
21
17
  ActiveRecordShards::ConnectionSpecification = ActiveRecord::ConnectionAdapters::ConnectionSpecification
@@ -35,12 +31,12 @@ if ActiveRecord.const_defined?(:Relation)
35
31
  ActiveRecord::Relation.send(:include, ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
36
32
  end
37
33
 
38
- if ActiveRecord::VERSION::STRING >= "3.1.0"
39
- ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
40
- else
41
- ActiveRecord::Associations::AssociationCollection.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
34
+ if ActiveRecord::Associations.const_defined?(:Preloader)
35
+ ActiveRecord::Associations::Preloader::HasAndBelongsToMany.send(:include, ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyPreloaderPatches)
42
36
  end
43
37
 
38
+ ActiveRecord::Associations::CollectionProxy.send(:include, ActiveRecordShards::AssociationCollectionConnectionSelection)
39
+
44
40
  module ActiveRecordShards
45
41
  def self.rails_env
46
42
  env = Rails.env if Object.const_defined?(:Rails)
@@ -33,12 +33,7 @@ module ActiveRecordShards
33
33
  def method_missing(method, *args, &block)
34
34
  # would love to not rely on version here, unfortunately @association_collection
35
35
  # is a sensitive little bitch of an object.
36
- if ActiveRecord::VERSION::STRING >= "3.1.0"
37
- reflection = @association_collection.proxy_association.reflection
38
- else
39
- reflection = @association_collection.proxy_reflection
40
- end
41
-
36
+ reflection = @association_collection.proxy_association.reflection
42
37
  reflection.klass.on_cx_switch_block(@which) { @association_collection.send(method, *args, &block) }
43
38
  end
44
39
  end
@@ -10,7 +10,7 @@ class ActiveRecord::Base
10
10
  remove_connection
11
11
  specification_cache[connection_pool_name] = spec
12
12
 
13
- if ActiveRecord::VERSION::STRING >= "4.0.0"
13
+ if ActiveRecord::VERSION::MAJOR >= 4
14
14
  connection_handler.establish_connection self, spec
15
15
  else
16
16
  connection_handler.establish_connection connection_pool_name, spec
@@ -95,11 +95,7 @@ module ActiveRecordShards
95
95
  if self == ActiveRecord::Base || !switch_to_slave || options[:construct_ro_scope] == false
96
96
  yield
97
97
  else
98
- if ActiveRecord::VERSION::MAJOR == 2
99
- with_scope({:find => {:readonly => true}}, &block)
100
- else
101
- readonly.scoping(&block)
102
- end
98
+ readonly.scoping(&block)
103
99
  end
104
100
  ensure
105
101
  @disallow_slave -= 1 if which == :master
@@ -177,13 +173,8 @@ module ActiveRecordShards
177
173
  # note that since we're subverting the standard establish_connection path, we have to handle the funky autoloading of the
178
174
  # connection adapter ourselves.
179
175
  specification_cache[name] ||= begin
180
- if ActiveRecord::VERSION::STRING >= "3.2.0"
181
- resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
182
- resolver.spec
183
- else
184
- autoload_adapter(spec['adapter'])
185
- ActiveRecordShards::ConnectionSpecification.new(spec, "#{spec['adapter']}_connection")
186
- end
176
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
177
+ resolver.spec
187
178
  end
188
179
 
189
180
  connection_handler.establish_connection(connection_pool_name, specification_cache[name])
@@ -194,15 +185,11 @@ module ActiveRecordShards
194
185
  end
195
186
 
196
187
  def connection_pool_key
197
- if ActiveRecord::VERSION::STRING >= "3.1.0"
198
- specification_cache[connection_pool_name]
199
- else
200
- connection_pool_name
201
- end
188
+ specification_cache[connection_pool_name]
202
189
  end
203
190
 
204
191
  def connected_to_shard?
205
- if ActiveRecord::VERSION::MAJOR == 4
192
+ if ActiveRecord::VERSION::MAJOR >= 4
206
193
  specs_to_pools = Hash[connection_handler.connection_pool_list.map { |pool| [pool.spec, pool] }]
207
194
  else
208
195
  specs_to_pools = connection_handler.connection_pools
@@ -1,23 +1,32 @@
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
+ def self.wrap_method_in_on_slave(class_method, base, method)
4
4
 
5
- def self.extended(base)
6
- base_methods = (base.methods | base.private_methods).map(&:to_sym)
7
- (CLASS_SLAVE_METHODS & base_methods).each do |slave_method|
8
- _, slave_method, punctuation = slave_method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
9
- base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
10
- class << self
11
- def #{slave_method}_with_default_slave#{punctuation}(*args, &block)
12
- on_slave_unless_tx do
13
- #{slave_method}_without_default_slave#{punctuation}(*args, &block)
14
- end
15
- end
5
+ if class_method
6
+ base_methods = (base.methods | base.private_methods).map(&:to_sym)
7
+ else
8
+ base_methods = (base.instance_methods | base.private_instance_methods).map(&:to_sym)
9
+ end
16
10
 
17
- alias_method_chain :#{slave_method}#{punctuation}, :default_slave
11
+ return unless base_methods.include?(method)
12
+ _, method, punctuation = method.to_s.match(/^(.*?)([\?\!]?)$/).to_a
13
+ base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
14
+ #{class_method ? "class << self" : ""}
15
+ def #{method}_with_default_slave#{punctuation}(*args, &block)
16
+ on_slave_unless_tx do
17
+ #{method}_without_default_slave#{punctuation}(*args, &block)
18
+ end
18
19
  end
19
- RUBY
20
- end
20
+
21
+ alias_method_chain :#{method}#{punctuation}, :default_slave
22
+ #{class_method ? "end" : ""}
23
+ RUBY
24
+ end
25
+
26
+ CLASS_SLAVE_METHODS = [ :find_by_sql, :count_by_sql, :calculate, :find_one, :find_some, :find_every, :quote_value, :sanitize_sql_hash_for_conditions, :exists?, :table_exists? ]
27
+
28
+ def self.extended(base)
29
+ CLASS_SLAVE_METHODS.each { |m| ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(true, base, m) }
21
30
 
22
31
  base.class_eval do
23
32
  # fix ActiveRecord to do the right thing, and use our aliased quote_value
@@ -58,33 +67,10 @@ module ActiveRecordShards
58
67
  end
59
68
 
60
69
  alias_method_chain :transaction, :slave_off
61
-
62
-
63
- def table_exists_with_default_slave?(*args)
64
- on_slave_unless_tx(*args) { table_exists_without_default_slave?(*args) }
65
- end
66
-
67
- alias_method_chain :table_exists?, :default_slave
68
70
  end
69
71
  end
70
-
71
-
72
- ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
73
- def construct_sql_with_default_slave(*args, &block)
74
- on_slave_unless_tx do
75
- construct_sql_without_default_slave(*args, &block)
76
- end
77
- end
78
-
79
- def construct_find_options_with_default_slave!(*args, &block)
80
- on_slave_unless_tx do
81
- construct_find_options_without_default_slave!(*args, &block)
82
- end
83
- end
84
-
85
- alias_method_chain :construct_sql, :default_slave if respond_to?(:construct_sql)
86
- alias_method_chain :construct_find_options!, :default_slave if respond_to?(:construct_find_options!)
87
- end
72
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_sql)
73
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, ActiveRecord::Associations::HasAndBelongsToManyAssociation, :construct_find_options!)
88
74
  end
89
75
 
90
76
  def on_slave_unless_tx(&block)
@@ -97,16 +83,23 @@ module ActiveRecordShards
97
83
 
98
84
  module ActiveRelationPatches
99
85
  def self.included(base)
100
- base.send :alias_method_chain, :calculate, :default_slave
101
- base.send :alias_method_chain, :exists?, :default_slave
86
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :calculate)
87
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :exists?)
88
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :pluck)
102
89
  end
103
90
 
104
91
  def on_slave_unless_tx
105
92
  @klass.on_slave_unless_tx { yield }
106
93
  end
94
+ end
107
95
 
108
- def calculate_with_default_slave(*args, &block)
109
- on_slave_unless_tx { calculate_without_default_slave(*args, &block) }
96
+ module HasAndBelongsToManyPreloaderPatches
97
+ def self.included(base)
98
+ ActiveRecordShards::DefaultSlavePatches.wrap_method_in_on_slave(false, base, :records_for) rescue nil
99
+ end
100
+
101
+ def on_slave_unless_tx
102
+ klass.on_slave_unless_tx { yield }
110
103
  end
111
104
 
112
105
  def exists_with_default_slave?(*args, &block)
@@ -106,18 +106,11 @@ end
106
106
  ActiveRecord::Migration.class_eval do
107
107
  extend ActiveRecordShards::MigrationClassExtension
108
108
 
109
- if ActiveRecord::VERSION::STRING >= "3.1.0"
110
- include ActiveRecordShards::ActualMigrationExtension
111
- define_method :migration_shard do
112
- self.class.migration_shard
113
- end
114
- alias_method_chain :migrate, :forced_shard
115
- else
116
- extend ActiveRecordShards::ActualMigrationExtension
117
- class << self
118
- alias_method_chain :migrate, :forced_shard
119
- end
109
+ include ActiveRecordShards::ActualMigrationExtension
110
+ define_method :migration_shard do
111
+ self.class.migration_shard
120
112
  end
113
+ alias_method_chain :migrate, :forced_shard
121
114
  end
122
115
 
123
116
  ActiveRecord::MigrationProxy.delegate :migration_shard, :to => :migration
@@ -48,10 +48,6 @@ module ActiveRecordShards
48
48
 
49
49
  def self.extended(base)
50
50
  base.send(:include, InstanceMethods)
51
- if ActiveRecord::VERSION::MAJOR == 2 && !base.method_defined?(:after_initialize)
52
- # trick rails 2 into running callbacks
53
- base.send(:define_method, :after_initialize){}
54
- end
55
51
  base.after_initialize :initialize_shard_and_slave
56
52
  end
57
53
  end
@@ -1,7 +1,7 @@
1
1
  require 'active_record_shards'
2
2
 
3
3
  %w[db:drop db:create db:abort_if_pending_migrations db:reset].each do |name|
4
- Rake.application.instance_variable_get(:@tasks).delete(name) || warn("could not delete #{name} task, potential load-order problem")
4
+ Rake::Task[name].clean
5
5
  end
6
6
 
7
7
  namespace :db do
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.beta1
5
- prerelease: 6
4
+ version: 3.0.0.beta2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mick Staugaard
@@ -11,27 +10,25 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-11-06 00:00:00.000000000 Z
13
+ date: 2014-01-31 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activerecord
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ! '>='
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
- version: 2.3.5
21
+ version: 3.2.16
24
22
  - - <=
25
23
  - !ruby/object:Gem::Version
26
24
  version: '4.1'
27
25
  type: :runtime
28
26
  prerelease: false
29
27
  version_requirements: !ruby/object:Gem::Requirement
30
- none: false
31
28
  requirements:
32
- - - ! '>='
29
+ - - '>='
33
30
  - !ruby/object:Gem::Version
34
- version: 2.3.5
31
+ version: 3.2.16
35
32
  - - <=
36
33
  - !ruby/object:Gem::Version
37
34
  version: '4.1'
@@ -44,7 +41,6 @@ executables: []
44
41
  extensions: []
45
42
  extra_rdoc_files: []
46
43
  files:
47
- - lib/active_record_shards/arel_engine.rb
48
44
  - lib/active_record_shards/association_collection_connection_selection.rb
49
45
  - lib/active_record_shards/configuration_parser.rb
50
46
  - lib/active_record_shards/connection_handler.rb
@@ -58,54 +54,28 @@ files:
58
54
  - lib/active_record_shards/tasks.rb
59
55
  - lib/active_record_shards.rb
60
56
  - README.md
61
- - test/configuration_parser_test.rb
62
- - test/connection_switching_test.rb
63
- - test/cowardly_migration/20110824010215_cowardly_migration.rb
64
- - test/database.yml
65
- - test/database_parse_test.yml
66
- - test/failure_migration/20110824010215_failure_migration.rb
67
- - test/helper.rb
68
- - test/migrations/20110824010216_shard_migration.rb
69
- - test/migrations/20110829215912_account_migration.rb
70
- - test/migrator_test.rb
71
- - test/models.rb
72
- - test/schema.rb
73
- homepage: http://github.com/zendesk/active_record_shards
57
+ homepage: https://github.com/zendesk/active_record_shards
74
58
  licenses:
75
59
  - MIT
60
+ metadata: {}
76
61
  post_install_message:
77
62
  rdoc_options: []
78
63
  require_paths:
79
64
  - lib
80
65
  required_ruby_version: !ruby/object:Gem::Requirement
81
- none: false
82
66
  requirements:
83
- - - ! '>='
67
+ - - '>='
84
68
  - !ruby/object:Gem::Version
85
69
  version: '0'
86
70
  required_rubygems_version: !ruby/object:Gem::Requirement
87
- none: false
88
71
  requirements:
89
- - - ! '>'
72
+ - - '>'
90
73
  - !ruby/object:Gem::Version
91
74
  version: 1.3.1
92
75
  requirements: []
93
76
  rubyforge_project:
94
- rubygems_version: 1.8.25
77
+ rubygems_version: 2.0.14
95
78
  signing_key:
96
- specification_version: 3
79
+ specification_version: 4
97
80
  summary: Simple database switching for ActiveRecord.
98
- test_files:
99
- - test/configuration_parser_test.rb
100
- - test/connection_switching_test.rb
101
- - test/cowardly_migration/20110824010215_cowardly_migration.rb
102
- - test/database.yml
103
- - test/database_parse_test.yml
104
- - test/failure_migration/20110824010215_failure_migration.rb
105
- - test/helper.rb
106
- - test/migrations/20110824010216_shard_migration.rb
107
- - test/migrations/20110829215912_account_migration.rb
108
- - test/migrator_test.rb
109
- - test/models.rb
110
- - test/schema.rb
111
- has_rdoc:
81
+ test_files: []
@@ -1,13 +0,0 @@
1
- if ActiveRecord::VERSION::MAJOR == 3 && ActiveRecord::VERSION::MINOR == 0
2
- class ActiveRecord::Base
3
- def self.arel_engine
4
- @arel_engine ||= begin
5
- if self == ActiveRecord::Base
6
- Arel::Table.engine
7
- else
8
- connection_handler.connection_pools[connection_pool_name] ? self : superclass.arel_engine
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,98 +0,0 @@
1
- require_relative 'helper'
2
-
3
- describe ActiveRecordShards::ConfigurationParser do
4
- describe "exploding the database.yml" do
5
- before do
6
- @exploded_conf = ActiveRecordShards::ConfigurationParser.explode(YAML::load(IO.read(File.dirname(__FILE__) + '/database_parse_test.yml')))
7
- end
8
-
9
- describe "main slave" do
10
- before { @conf = @exploded_conf['test_slave'] }
11
- it "be exploded" do
12
- @conf["shard_names"] = @conf["shard_names"].to_set
13
- assert_equal({
14
- "adapter" => "mysql",
15
- "encoding" => "utf8",
16
- "database" => "ars_test",
17
- "port" => 123,
18
- "username" => "root",
19
- "password" => nil,
20
- "host" => "main_slave_host",
21
- "shard_names" => ["a", "b"].to_set
22
- }, @conf)
23
- end
24
- end
25
-
26
- describe "shard a" do
27
- describe "master" do
28
- before { @conf = @exploded_conf['test_shard_a'] }
29
- it "be exploded" do
30
- @conf["shard_names"] = @conf["shard_names"].to_set
31
- assert_equal({
32
- "adapter" => "mysql",
33
- "encoding" => "utf8",
34
- "database" => "ars_test_shard_a",
35
- "port" => 123,
36
- "username" => "root",
37
- "password" => nil,
38
- "host" => "shard_a_host",
39
- "shard_names" => ["a", "b"].to_set
40
- }, @conf)
41
- end
42
- end
43
-
44
- describe "slave" do
45
- before { @conf = @exploded_conf['test_shard_a_slave'] }
46
- it "be exploded" do
47
- @conf["shard_names"] = @conf["shard_names"].to_set
48
- assert_equal({
49
- "adapter" => "mysql",
50
- "encoding" => "utf8",
51
- "database" => "ars_test_shard_a",
52
- "port" => 123,
53
- "username" => "root",
54
- "password" => nil,
55
- "host" => "shard_a_slave_host",
56
- "shard_names" => ["a", "b"].to_set
57
- }, @conf)
58
- end
59
- end
60
- end
61
-
62
- describe "shard b" do
63
- describe "master" do
64
- before { @conf = @exploded_conf['test_shard_b'] }
65
- it "be exploded" do
66
- @conf["shard_names"] = @conf["shard_names"].to_set
67
- assert_equal({
68
- "adapter" => "mysql",
69
- "encoding" => "utf8",
70
- "database" => "ars_test_shard_b",
71
- "port" => 123,
72
- "username" => "root",
73
- "password" => nil,
74
- "host" => "shard_b_host",
75
- "shard_names" => ["a", "b"].to_set
76
- }, @conf)
77
- end
78
- end
79
-
80
- describe "slave" do
81
- before { @conf = @exploded_conf['test_shard_b_slave'] }
82
- it "be exploded" do
83
- @conf["shard_names"] = @conf["shard_names"].to_set
84
- assert_equal({
85
- "adapter" => "mysql",
86
- "encoding" => "utf8",
87
- "database" => "ars_test_shard_b_slave",
88
- "port" => 123,
89
- "username" => "root",
90
- "password" => nil,
91
- "host" => "shard_b_host",
92
- "shard_names" => ["a", "b"].to_set
93
- }, @conf)
94
- end
95
- end
96
- end
97
- end
98
- end