active_record_shards 3.8.0 → 3.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ddf4f7e3054f0ae5d1d0a1fb9d50c43ff98411f
4
- data.tar.gz: 22b7e496f726e396b8bece299b1b6e36d3e32e91
3
+ metadata.gz: 9a95ee1107402ac016dcd5dbe7caf84b77973c5e
4
+ data.tar.gz: 7871eaecc9cf5971d2b1a35f002a8c0c82b1951c
5
5
  SHA512:
6
- metadata.gz: a3b9ec6899a75d390321297b48f9ce18e8198bcfb3919556b4935f9e803bf229551bda1bc98b69f5eb7d5cb5a050f5aeee773f3a5dbd7eefb14c2f3514feee4f
7
- data.tar.gz: d63f9cd510b2c164a7fb1d2d69899560401fb9115d9425838bdeb0ee1ff224e492b3dfc2e15b7e4daf5cc11d19b3e48eca094c989359b7442e1a3db05134c036
6
+ metadata.gz: edb32557a10fce0738d2015bcf4e747f00ec1b49cc72a99c1cbbfbd3d4583a202e19087abb374e434bd87387e7e0ef5e584b046ca2888d0fc6774c63e026ac6c
7
+ data.tar.gz: bd8d79125096f2228cf4d862b3fac99b346c303b4f4304d70c7294906c239a5b85f2342d7721984413879541620c6927ee64c4320fbb8ee53649f2f09143c8e0
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  ActiveRecord Shards is an extension for ActiveRecord that provides support for sharded database and slaves. Basically it is just a nice way to
6
6
  switch between database connections. We've made the implementation very small, and have tried not to reinvent any wheels already present in ActiveRecord.
7
7
 
8
- ActiveRecord Shards has been used and tested on Rails 3.2, 4.0, 4.1, 4.2 and 5.0 and has in some form or another been used in production on a large Rails app for several years.
8
+ ActiveRecord Shards has been used and tested on Rails 3.2, 4.2 and 5.0 and has in some form or another been used in production on a large Rails app for several years.
9
9
 
10
10
  ## Installation
11
11
 
@@ -28,10 +28,8 @@ ActiveRecord::Associations::CollectionProxy.include(ActiveRecordShards::Associat
28
28
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
29
29
  when '3.2'
30
30
  require 'active_record_shards-3-2'
31
- when '4.0'
32
- require 'active_record_shards-4-0'
33
- when '4.1', '4.2'
34
- require 'active_record_shards-4-1'
31
+ when '4.2'
32
+ require 'active_record_shards-4-2'
35
33
  when '5.0', '5.1'
36
34
  require 'active_record_shards-5-0'
37
35
  else
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
  ActiveRecord::Base.class_eval do
3
3
  def self.establish_connection(spec = ENV["DATABASE_URL"])
4
- if ActiveRecord::VERSION::STRING >= '4.1.0'
4
+ if ActiveRecord::VERSION::MAJOR >= 4
5
5
  spec ||= ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
6
6
  spec = spec.to_sym if spec.is_a?(String)
7
7
  resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
@@ -30,19 +30,19 @@ module ActiveRecordShards
30
30
 
31
31
  # note that since we're subverting the standard establish_connection path, we have to handle the funky autoloading of the
32
32
  # connection adapter ourselves.
33
- specification_cache[name] ||= begin
34
- if ActiveRecord::VERSION::STRING >= '4.1.0'
33
+ if ActiveRecord::VERSION::MAJOR >= 4
34
+ specification_cache[name] ||= begin
35
35
  resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new configurations
36
36
  resolver.spec(spec)
37
- else
38
- resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
39
- resolver.spec
40
37
  end
41
- end
42
38
 
43
- if ActiveRecord::VERSION::MAJOR >= 4
44
39
  connection_handler.establish_connection(self, specification_cache[name])
45
40
  else
41
+ specification_cache[name] ||= begin
42
+ resolver = ActiveRecordShards::ConnectionSpecification::Resolver.new spec, configurations
43
+ resolver.spec
44
+ end
45
+
46
46
  connection_handler.establish_connection(connection_pool_name, specification_cache[name])
47
47
  end
48
48
  end
@@ -20,15 +20,8 @@ module ActiveRecordShards
20
20
 
21
21
  pool = connection_handler.retrieve_connection_pool(spec_name)
22
22
  if pool.nil?
23
- spec =
24
- if ActiveRecord::VERSION::MAJOR == 5 && ActiveRecord::VERSION::MINOR.zero?
25
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(configurations)
26
- resolver.spec(spec_name.to_sym, spec_name)
27
- else
28
- # Resolver will be applied by Rails 5.1+
29
- spec_name.to_sym
30
- end
31
-
23
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(configurations)
24
+ spec = resolver.spec(spec_name.to_sym, spec_name)
32
25
  connection_handler.establish_connection(spec)
33
26
  end
34
27
  end
@@ -0,0 +1,25 @@
1
+ module ActiveRecordShards
2
+ module ConnectionSwitcher
3
+ def connection_specification_name
4
+ name = current_shard_selection.resolve_connection_name(sharded: is_sharded?, configurations: configurations)
5
+
6
+ unless configurations[name] || name == "primary"
7
+ raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in database.yml"
8
+ end
9
+
10
+ name
11
+ end
12
+
13
+ private
14
+
15
+ def ensure_shard_connection
16
+ # See if we've connected before. If not, call `#establish_connection`
17
+ # so that ActiveRecord can resolve connection_specification_name to an
18
+ # ARS connection.
19
+ spec_name = connection_specification_name
20
+
21
+ pool = connection_handler.retrieve_connection_pool(spec_name)
22
+ connection_handler.establish_connection(spec_name.to_sym) if pool.nil?
23
+ end
24
+ end
25
+ end
@@ -158,11 +158,21 @@ module ActiveRecordShards
158
158
  ActiveRecordShards.rails_env
159
159
  end
160
160
 
161
- def with_default_shard
162
- if is_sharded? && current_shard_id.nil? && table_name != ActiveRecord::Migrator.schema_migrations_table_name
163
- on_first_shard { yield }
164
- else
165
- yield
161
+ if ActiveRecord::VERSION::MAJOR >= 4
162
+ def with_default_shard
163
+ if is_sharded? && current_shard_id.nil? && table_name != ActiveRecord::SchemaMigration.table_name
164
+ on_first_shard { yield }
165
+ else
166
+ yield
167
+ end
168
+ end
169
+ else
170
+ def with_default_shard
171
+ if is_sharded? && current_shard_id.nil? && table_name != ActiveRecord::Migrator.schema_migrations_table_name
172
+ on_first_shard { yield }
173
+ else
174
+ yield
175
+ end
166
176
  end
167
177
  end
168
178
 
@@ -188,10 +198,12 @@ module ActiveRecordShards
188
198
  end
189
199
 
190
200
  case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
191
- when '3.2', '4.0', '4.1', '4.2'
201
+ when '3.2', '4.2'
192
202
  require 'active_record_shards/connection_switcher-4-0'
193
- when '5.0', '5.1'
203
+ when '5.0'
194
204
  require 'active_record_shards/connection_switcher-5-0'
205
+ when '5.1'
206
+ require 'active_record_shards/connection_switcher-5-1'
195
207
  else
196
208
  raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
197
209
  end
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: 3.8.0
4
+ version: 3.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-02-24 00:00:00.000000000 Z
13
+ date: 2017-03-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -189,8 +189,7 @@ extra_rdoc_files: []
189
189
  files:
190
190
  - README.md
191
191
  - lib/active_record_shards-3-2.rb
192
- - lib/active_record_shards-4-0.rb
193
- - lib/active_record_shards-4-1.rb
192
+ - lib/active_record_shards-4-2.rb
194
193
  - lib/active_record_shards-5-0.rb
195
194
  - lib/active_record_shards.rb
196
195
  - lib/active_record_shards/association_collection_connection_selection.rb
@@ -200,6 +199,7 @@ files:
200
199
  - lib/active_record_shards/connection_specification.rb
201
200
  - lib/active_record_shards/connection_switcher-4-0.rb
202
201
  - lib/active_record_shards/connection_switcher-5-0.rb
202
+ - lib/active_record_shards/connection_switcher-5-1.rb
203
203
  - lib/active_record_shards/connection_switcher.rb
204
204
  - lib/active_record_shards/default_slave_patches.rb
205
205
  - lib/active_record_shards/migration.rb
@@ -228,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
228
228
  version: '0'
229
229
  requirements: []
230
230
  rubyforge_project:
231
- rubygems_version: 2.5.1
231
+ rubygems_version: 2.5.2
232
232
  signing_key:
233
233
  specification_version: 4
234
234
  summary: Simple database switching for ActiveRecord.
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'active_record_shards/connection_pool'
3
- require 'active_record_shards/connection_handler'
4
- require 'active_record_shards/connection_specification'
5
- require 'active_record_shards/schema_dumper_extension'
6
-
7
- ActiveRecordShards::ConnectionSpecification = ActiveRecord::ConnectionAdapters::ConnectionSpecification
8
- methods_to_override = [:establish_connection, :remove_connection, :pool_for, :pool_from_any_process_for]
9
- ActiveRecordShards.override_connection_handler_methods(methods_to_override)
10
-
11
- ActiveRecord::Associations::Preloader::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyPreloaderPatches)
12
-
13
- ActiveRecord::SchemaDumper.prepend(ActiveRecordShards::SchemaDumperExtension)