active_record_shards 4.0.0.beta8 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'active_record_shards'
3
4
 
4
5
  %w[db:drop db:create db:abort_if_pending_migrations db:reset db:test:purge].each do |name|
@@ -8,14 +9,15 @@ end
8
9
  namespace :db do
9
10
  desc 'Drops the database for the current RAILS_ENV including shards'
10
11
  task drop: :load_config do
11
- ActiveRecord::Base.configurations.each do |key, conf|
12
- next if !key.starts_with?(ActiveRecordShards.rails_env) || key.ends_with?("_slave")
12
+ ActiveRecord::Base.configurations.to_h.each do |key, conf|
13
+ next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica")
14
+
13
15
  begin
14
16
  ActiveRecordShards::Tasks.root_connection(conf).drop_database(conf['database'])
15
17
  # rescue ActiveRecord::NoDatabaseError # TODO: exists in AR but never is raised here ...
16
18
  # $stderr.puts "Database '#{conf['database']}' does not exist"
17
- rescue StandardError => error
18
- warn error, *error.backtrace
19
+ rescue StandardError => e
20
+ warn e, *e.backtrace
19
21
  warn "Couldn't drop #{conf['database']}"
20
22
  end
21
23
  end
@@ -28,36 +30,45 @@ namespace :db do
28
30
 
29
31
  desc "Create the database defined in config/database.yml for the current RAILS_ENV including shards"
30
32
  task create: :load_config do
31
- ActiveRecord::Base.configurations.each do |key, conf|
32
- next if !key.starts_with?(ActiveRecordShards.rails_env) || key.ends_with?("_slave")
33
+ ActiveRecord::Base.configurations.to_h.each do |key, conf|
34
+ next if !key.start_with?(ActiveRecordShards.app_env) || key.end_with?("_replica")
35
+
33
36
  begin
34
- # MysqlAdapter takes charset instead of encoding in Rails 4
35
- # https://github.com/rails/rails/commit/78b30fed9336336694fb2cb5d2825f95800b541c
37
+ # MysqlAdapter takes charset instead of encoding in Rails 4.2 or greater
38
+ # https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/tasks/mysql_database_tasks.rb#L85-L96
36
39
  symbolized_configuration = conf.symbolize_keys
37
40
  symbolized_configuration[:charset] = symbolized_configuration[:encoding]
38
41
 
39
42
  ActiveRecordShards::Tasks.root_connection(conf).create_database(conf['database'], symbolized_configuration)
40
- rescue ActiveRecord::StatementInvalid => ex
41
- if ex.message.include?('database exists')
43
+ rescue ActiveRecord::StatementInvalid => e
44
+ if e.message.include?('database exists')
42
45
  puts "#{conf['database']} already exists"
43
46
  else
44
- raise ex
47
+ raise e
45
48
  end
46
49
  end
47
50
  end
48
- ActiveRecord::Base.establish_connection(ActiveRecordShards.rails_env.to_sym)
51
+ ActiveRecord::Base.establish_connection(ActiveRecordShards.app_env.to_sym)
49
52
  end
50
53
 
51
54
  desc "Raises an error if there are pending migrations"
52
55
  task abort_if_pending_migrations: :environment do
53
56
  if defined? ActiveRecord
54
57
  pending_migrations =
55
- ActiveRecord::Base.on_shard(nil) { ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations }
58
+ if ActiveRecord::VERSION::MAJOR >= 6
59
+ migrations = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths, ActiveRecord::SchemaMigration).migrations
60
+ ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::SchemaMigration).pending_migrations
61
+ elsif ActiveRecord::VERSION::STRING >= "5.2.0"
62
+ migrations = ActiveRecord::MigrationContext.new(ActiveRecord::Migrator.migrations_paths).migrations
63
+ ActiveRecord::Migrator.new(:up, migrations).pending_migrations
64
+ else
65
+ ActiveRecord::Base.on_shard(nil) { ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations }
66
+ end
56
67
 
57
68
  if pending_migrations.any?
58
- puts "You have #{pending_migrations.size} pending migrations:"
69
+ warn "You have #{pending_migrations.size} pending migrations:"
59
70
  pending_migrations.each do |pending_migration|
60
- puts ' %4d %s' % [pending_migration.version, pending_migration.name]
71
+ warn ' %4d %s' % [pending_migration.version, pending_migration.name]
61
72
  end
62
73
  abort %(Run "rake db:migrate" to update your database then try again.)
63
74
  end
@@ -67,26 +78,39 @@ namespace :db do
67
78
  namespace :test do
68
79
  desc 'Purges the test databases by dropping and creating'
69
80
  task purge: :load_config do |t|
70
- begin
71
- saved_env = Rails.env
72
- Rails.env = 'test'
73
- Rake.application.lookup('db:drop', t.scope).execute
74
- Rake.application.lookup('db:create', t.scope).execute
75
- ensure
76
- Rails.env = saved_env
77
- end
81
+ saved_env = Rails.env
82
+ Rails.env = 'test'
83
+ Rake.application.lookup('db:drop', t.scope).execute
84
+ Rake.application.lookup('db:create', t.scope).execute
85
+ ensure
86
+ Rails.env = saved_env
78
87
  end
79
88
  end
80
89
  end
81
90
 
82
91
  module ActiveRecordShards
83
92
  module Tasks
84
- def self.root_connection(conf)
85
- # this will trigger rails to load the adapter
86
- conf = conf.merge('database' => nil)
87
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
88
- resolver.spec(conf)
89
- ActiveRecord::Base.send("#{conf['adapter']}_connection", conf)
93
+ class << self
94
+ def root_connection(conf)
95
+ conf = conf.merge('database' => nil)
96
+ spec = spec_for(conf)
97
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == '6.1'
98
+ ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.db_config.configuration_hash)
99
+ else
100
+ ActiveRecord::Base.send("#{conf['adapter']}_connection", spec.config)
101
+ end
102
+ end
103
+
104
+ private
105
+
106
+ def spec_for(conf)
107
+ if "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}" == '6.1'
108
+ ActiveRecord::Base.connection_handler.send(:resolve_pool_config, conf, ActiveRecord::Base)
109
+ else
110
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
111
+ resolver.spec(conf)
112
+ end
113
+ end
90
114
  end
91
115
  end
92
116
  end
@@ -1,32 +1,80 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'active_record'
3
4
  require 'active_record/base'
4
- require 'active_record_shards/base_config'
5
- require 'active_record_shards/connection_resolver'
6
5
  require 'active_record_shards/configuration_parser'
7
6
  require 'active_record_shards/model'
8
- require 'active_record_shards/slave_db'
9
7
  require 'active_record_shards/shard_selection'
10
- require 'active_record_shards/no_shard_selection'
11
8
  require 'active_record_shards/connection_switcher'
12
- require 'active_record_shards/sharded_model'
13
9
  require 'active_record_shards/association_collection_connection_selection'
14
- require 'active_record_shards/default_slave_patches'
10
+ require 'active_record_shards/migration'
11
+ require 'active_record_shards/default_replica_patches'
12
+ require 'active_record_shards/default_shard'
13
+ require 'active_record_shards/schema_dumper_extension'
15
14
 
16
15
  module ActiveRecordShards
17
- def self.rails_env
16
+ class << self
17
+ attr_accessor :disable_replica_readonly_records
18
+ end
19
+
20
+ def self.app_env
18
21
  env = Rails.env if defined?(Rails.env)
19
22
  env ||= RAILS_ENV if Object.const_defined?(:RAILS_ENV)
20
23
  env ||= ENV['RAILS_ENV']
24
+ env ||= APP_ENV if Object.const_defined?(:APP_ENV)
25
+ env ||= ENV['APP_ENV']
21
26
  env || 'development'
22
27
  end
23
28
  end
24
29
 
25
- ActiveRecord::Base.extend(ActiveRecordShards::BaseConfig)
26
30
  ActiveRecord::Base.extend(ActiveRecordShards::ConfigurationParser)
27
31
  ActiveRecord::Base.extend(ActiveRecordShards::Model)
28
- ActiveRecord::Base.extend(ActiveRecordShards::SlaveDb)
29
- ActiveRecord::Base.extend(ActiveRecordShards::DefaultSlavePatches)
30
- ActiveRecord::Relation.include(ActiveRecordShards::DefaultSlavePatches::ActiveRelationPatches)
32
+ ActiveRecord::Base.extend(ActiveRecordShards::ConnectionSwitcher)
33
+ ActiveRecord::Base.extend(ActiveRecordShards::DefaultReplicaPatches)
34
+ ActiveRecord::Relation.include(ActiveRecordShards::DefaultReplicaPatches::ActiveRelationPatches)
31
35
  ActiveRecord::Associations::CollectionProxy.include(ActiveRecordShards::AssociationCollectionConnectionSelection)
32
- ActiveRecord::Associations::Builder::HasAndBelongsToMany.include(ActiveRecordShards::DefaultSlavePatches::HasAndBelongsToManyBuilderExtension)
36
+ ActiveRecord::Associations::Builder::HasAndBelongsToMany.include(ActiveRecordShards::DefaultReplicaPatches::Rails41HasAndBelongsToManyBuilderExtension)
37
+ ActiveRecord::SchemaDumper.prepend(ActiveRecordShards::SchemaDumperExtension)
38
+
39
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
40
+ when '5.1'
41
+ # https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/associations/association.rb#L97
42
+ ActiveRecord::Associations::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationAssociationScopePatch)
43
+
44
+ # https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/associations/singular_association.rb#L41
45
+ ActiveRecord::Associations::SingularAssociation.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationFindTargetPatch)
46
+
47
+ # https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/associations/collection_association.rb#L305
48
+ ActiveRecord::Associations::CollectionAssociation.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationFindTargetPatch)
49
+
50
+ # https://github.com/rails/rails/blob/v5.1.7/activerecord/lib/active_record/associations/preloader/association.rb#L120
51
+ ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsPreloaderAssociationLoadRecordsPatch)
52
+ when '5.2'
53
+ # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/relation.rb#L530
54
+ # But the #exec_queries method also calls #connection, and I don't know if we should patch that one, too...
55
+ ActiveRecord::Relation.prepend(ActiveRecordShards::DefaultReplicaPatches::Rails52RelationPatches)
56
+
57
+ # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/singular_association.rb#L42
58
+ ActiveRecord::Associations::SingularAssociation.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationFindTargetPatch)
59
+
60
+ # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/collection_association.rb#L308
61
+ ActiveRecord::Associations::CollectionAssociation.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationFindTargetPatch)
62
+
63
+ # https://github.com/rails/rails/blob/v5.2.6/activerecord/lib/active_record/associations/preloader/association.rb#L96
64
+ ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsPreloaderAssociationLoadRecordsPatch)
65
+ when '6.0', '6.1'
66
+ # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/type_caster/connection.rb#L28
67
+ ActiveRecord::TypeCaster::Connection.prepend(ActiveRecordShards::DefaultReplicaPatches::TypeCasterConnectionConnectionPatch)
68
+
69
+ # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/schema.rb#L53-L54
70
+ ActiveRecord::Schema.prepend(ActiveRecordShards::DefaultReplicaPatches::SchemaDefinePatch)
71
+
72
+ # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/relation.rb#L739
73
+ # But the #exec_queries and #compute_cache_version methods also call #connection, and I don't know if we should patch those, too...
74
+ ActiveRecord::Relation.prepend(ActiveRecordShards::DefaultReplicaPatches::Rails52RelationPatches)
75
+
76
+ # https://github.com/rails/rails/blob/v6.0.4/activerecord/lib/active_record/associations/association.rb#L213
77
+ ActiveRecord::Associations::Association.prepend(ActiveRecordShards::DefaultReplicaPatches::AssociationsAssociationFindTargetPatch)
78
+ else
79
+ raise "ActiveRecordShards is not compatible with #{ActiveRecord::VERSION::STRING}"
80
+ end
metadata CHANGED
@@ -1,45 +1,60 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_shards
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0.beta8
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
+ - Benjamin Quorning
8
+ - Gabe Martin-Dempesy
9
+ - Pierre Schambacher
7
10
  - Mick Staugaard
8
11
  - Eric Chapweske
9
12
  - Ben Osheroff
10
- autorequire:
13
+ autorequire:
11
14
  bindir: bin
12
15
  cert_chain: []
13
- date: 2017-11-13 00:00:00.000000000 Z
16
+ date: 2022-08-23 00:00:00.000000000 Z
14
17
  dependencies:
15
18
  - !ruby/object:Gem::Dependency
16
19
  name: activerecord
17
20
  requirement: !ruby/object:Gem::Requirement
18
21
  requirements:
19
- - - "~>"
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: '5.1'
25
+ - - "<"
20
26
  - !ruby/object:Gem::Version
21
- version: '5.0'
27
+ version: '7.0'
22
28
  type: :runtime
23
29
  prerelease: false
24
30
  version_requirements: !ruby/object:Gem::Requirement
25
31
  requirements:
26
- - - "~>"
32
+ - - ">="
27
33
  - !ruby/object:Gem::Version
28
- version: '5.0'
34
+ version: '5.1'
35
+ - - "<"
36
+ - !ruby/object:Gem::Version
37
+ version: '7.0'
29
38
  - !ruby/object:Gem::Dependency
30
39
  name: activesupport
31
40
  requirement: !ruby/object:Gem::Requirement
32
41
  requirements:
33
- - - "~>"
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '5.1'
45
+ - - "<"
34
46
  - !ruby/object:Gem::Version
35
- version: '5.0'
47
+ version: '7.0'
36
48
  type: :runtime
37
49
  prerelease: false
38
50
  version_requirements: !ruby/object:Gem::Requirement
39
51
  requirements:
40
- - - "~>"
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.1'
55
+ - - "<"
41
56
  - !ruby/object:Gem::Version
42
- version: '5.0'
57
+ version: '7.0'
43
58
  - !ruby/object:Gem::Dependency
44
59
  name: bump
45
60
  requirement: !ruby/object:Gem::Requirement
@@ -60,14 +75,14 @@ dependencies:
60
75
  requirements:
61
76
  - - ">="
62
77
  - !ruby/object:Gem::Version
63
- version: '0'
78
+ version: 5.10.0
64
79
  type: :development
65
80
  prerelease: false
66
81
  version_requirements: !ruby/object:Gem::Requirement
67
82
  requirements:
68
83
  - - ">="
69
84
  - !ruby/object:Gem::Version
70
- version: '0'
85
+ version: 5.10.0
71
86
  - !ruby/object:Gem::Dependency
72
87
  name: minitest-rg
73
88
  requirement: !ruby/object:Gem::Requirement
@@ -88,14 +103,14 @@ dependencies:
88
103
  requirements:
89
104
  - - ">="
90
105
  - !ruby/object:Gem::Version
91
- version: '0'
106
+ version: 1.4.0
92
107
  type: :development
93
108
  prerelease: false
94
109
  version_requirements: !ruby/object:Gem::Requirement
95
110
  requirements:
96
111
  - - ">="
97
112
  - !ruby/object:Gem::Version
98
- version: '0'
113
+ version: 1.4.0
99
114
  - !ruby/object:Gem::Dependency
100
115
  name: mysql2
101
116
  requirement: !ruby/object:Gem::Requirement
@@ -111,66 +126,67 @@ dependencies:
111
126
  - !ruby/object:Gem::Version
112
127
  version: '0'
113
128
  - !ruby/object:Gem::Dependency
114
- name: phenix
129
+ name: rake
115
130
  requirement: !ruby/object:Gem::Requirement
116
131
  requirements:
117
- - - ">="
132
+ - - "~>"
118
133
  - !ruby/object:Gem::Version
119
- version: 0.2.0
134
+ version: '12.0'
120
135
  type: :development
121
136
  prerelease: false
122
137
  version_requirements: !ruby/object:Gem::Requirement
123
138
  requirements:
124
- - - ">="
139
+ - - "~>"
125
140
  - !ruby/object:Gem::Version
126
- version: 0.2.0
141
+ version: '12.0'
127
142
  - !ruby/object:Gem::Dependency
128
- name: rake
143
+ name: rubocop
129
144
  requirement: !ruby/object:Gem::Requirement
130
145
  requirements:
131
146
  - - "~>"
132
147
  - !ruby/object:Gem::Version
133
- version: '12.0'
148
+ version: 0.77.0
134
149
  type: :development
135
150
  prerelease: false
136
151
  version_requirements: !ruby/object:Gem::Requirement
137
152
  requirements:
138
153
  - - "~>"
139
154
  - !ruby/object:Gem::Version
140
- version: '12.0'
155
+ version: 0.77.0
141
156
  - !ruby/object:Gem::Dependency
142
- name: rubocop
157
+ name: rubocop-minitest
143
158
  requirement: !ruby/object:Gem::Requirement
144
159
  requirements:
145
- - - '='
160
+ - - "~>"
146
161
  - !ruby/object:Gem::Version
147
- version: 0.51.0
162
+ version: 0.5.0
148
163
  type: :development
149
164
  prerelease: false
150
165
  version_requirements: !ruby/object:Gem::Requirement
151
166
  requirements:
152
- - - '='
167
+ - - "~>"
153
168
  - !ruby/object:Gem::Version
154
- version: 0.51.0
169
+ version: 0.5.0
155
170
  - !ruby/object:Gem::Dependency
156
- name: wwtd
171
+ name: rubocop-performance
157
172
  requirement: !ruby/object:Gem::Requirement
158
173
  requirements:
159
- - - ">="
174
+ - - "~>"
160
175
  - !ruby/object:Gem::Version
161
- version: '0'
176
+ version: 1.5.1
162
177
  type: :development
163
178
  prerelease: false
164
179
  version_requirements: !ruby/object:Gem::Requirement
165
180
  requirements:
166
- - - ">="
181
+ - - "~>"
167
182
  - !ruby/object:Gem::Version
168
- version: '0'
169
- description: Easily run queries on shard and slave databases.
183
+ version: 1.5.1
184
+ description: Easily run queries on shard and replica databases.
170
185
  email:
186
+ - bquorning@zendesk.com
187
+ - gabe@zendesk.com
188
+ - pschambacher@zendesk.com
171
189
  - mick@staugaard.com
172
- - eac@zendesk.com
173
- - ben@gimbo.net
174
190
  executables: []
175
191
  extensions: []
176
192
  extra_rdoc_files: []
@@ -178,43 +194,40 @@ files:
178
194
  - README.md
179
195
  - lib/active_record_shards.rb
180
196
  - lib/active_record_shards/association_collection_connection_selection.rb
181
- - lib/active_record_shards/base_config.rb
182
197
  - lib/active_record_shards/configuration_parser.rb
183
- - lib/active_record_shards/connection_resolver.rb
198
+ - lib/active_record_shards/connection_switcher-5-1.rb
199
+ - lib/active_record_shards/connection_switcher-6-0.rb
184
200
  - lib/active_record_shards/connection_switcher.rb
185
- - lib/active_record_shards/connection_switcher_5_0.rb
186
- - lib/active_record_shards/connection_switcher_5_1.rb
187
- - lib/active_record_shards/default_slave_patches.rb
201
+ - lib/active_record_shards/default_replica_patches.rb
202
+ - lib/active_record_shards/default_shard.rb
203
+ - lib/active_record_shards/migration.rb
188
204
  - lib/active_record_shards/model.rb
189
- - lib/active_record_shards/no_shard_selection.rb
205
+ - lib/active_record_shards/schema_dumper_extension.rb
190
206
  - lib/active_record_shards/shard_selection.rb
191
207
  - lib/active_record_shards/shard_support.rb
192
- - lib/active_record_shards/sharded_model.rb
193
- - lib/active_record_shards/slave_db.rb
194
208
  - lib/active_record_shards/sql_comments.rb
195
209
  - lib/active_record_shards/tasks.rb
196
210
  homepage: https://github.com/zendesk/active_record_shards
197
211
  licenses:
198
212
  - MIT
199
213
  metadata: {}
200
- post_install_message:
214
+ post_install_message:
201
215
  rdoc_options: []
202
216
  require_paths:
203
217
  - lib
204
218
  required_ruby_version: !ruby/object:Gem::Requirement
205
219
  requirements:
206
- - - "~>"
220
+ - - ">="
207
221
  - !ruby/object:Gem::Version
208
- version: '2.0'
222
+ version: '2.6'
209
223
  required_rubygems_version: !ruby/object:Gem::Requirement
210
224
  requirements:
211
- - - ">"
225
+ - - ">="
212
226
  - !ruby/object:Gem::Version
213
- version: 1.3.1
227
+ version: '0'
214
228
  requirements: []
215
- rubyforge_project:
216
- rubygems_version: 2.7.2
217
- signing_key:
229
+ rubygems_version: 3.1.6
230
+ signing_key:
218
231
  specification_version: 4
219
232
  summary: Simple database switching for ActiveRecord.
220
233
  test_files: []
@@ -1,26 +0,0 @@
1
- module ActiveRecordShards
2
- module BaseConfig
3
- def connection_config
4
- { shard: nil, sharded: false }
5
- end
6
-
7
- def connection_specification_name
8
- name = connection_resolver.resolve_connection_name(connection_config)
9
-
10
- unless configurations[name] || name == "primary"
11
- raise ActiveRecord::AdapterNotSpecified, "No database defined by #{name} in your database config. (configurations: #{configurations.inspect})"
12
- end
13
-
14
- name
15
- end
16
-
17
- def connection_resolver
18
- Thread.current[:connection_resolver] ||=
19
- ActiveRecordShards::ConnectionResolver.new(configurations)
20
- end
21
-
22
- def reset_connection_resolver
23
- Thread.current[:connection_resolver] = nil
24
- end
25
- end
26
- end
@@ -1,31 +0,0 @@
1
- module ActiveRecordShards
2
- class ConnectionResolver
3
- attr_reader :configurations
4
-
5
- def initialize(configurations)
6
- @configurations = configurations
7
- end
8
-
9
- PRIMARY = "primary".freeze
10
- def resolve_connection_name(slave:, shard:, sharded:)
11
- resolved_shard = sharded ? shard : nil
12
-
13
- if !resolved_shard && !slave
14
- return PRIMARY
15
- end
16
-
17
- @shard_names ||= {}
18
- @shard_names[ActiveRecordShards.rails_env] ||= {}
19
- @shard_names[ActiveRecordShards.rails_env][resolved_shard] ||= {}
20
- @shard_names[ActiveRecordShards.rails_env][resolved_shard][slave] ||= begin
21
- s = ActiveRecordShards.rails_env.dup
22
- s << "_shard_#{resolved_shard}" if resolved_shard
23
-
24
- if slave && configurations["#{s}_slave"] # fall back to master connection
25
- s << "_slave"
26
- end
27
- s
28
- end
29
- end
30
- end
31
- end
@@ -1,19 +0,0 @@
1
- module ActiveRecordShards
2
- module BaseConfig
3
- private
4
-
5
- def ensure_shard_connection
6
- # See if we've connected before. If not, call `#establish_connection`
7
- # so that ActiveRecord can resolve connection_specification_name to an
8
- # ARS connection.
9
- spec_name = connection_specification_name
10
-
11
- pool = connection_handler.retrieve_connection_pool(spec_name)
12
- if pool.nil?
13
- resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(configurations)
14
- spec = resolver.spec(spec_name.to_sym, spec_name)
15
- connection_handler.establish_connection(spec)
16
- end
17
- end
18
- end
19
- end