rails_multisite 2.5.0 → 3.0.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
  SHA256:
3
- metadata.gz: e38f660f7fbb4a5505adeb4d46f73a02f290cc65f9f566a03fc4fc7051e68392
4
- data.tar.gz: 90242e454a09c2faed90d8561ac6b3de61c0d723344ac107e1fc89e23c20f0cd
3
+ metadata.gz: ee4885d481591f47f16a367e206e103af5ef3ea1504d65e0c34f97fa8766b91f
4
+ data.tar.gz: b6dc6f7d07e5ced80e7da754f5940b30a42a60d02e859afc8c949ffdf24f8b60
5
5
  SHA512:
6
- metadata.gz: 8a9bccdd03ddb205a20325ed127cc0e40b6e7c790147334ac9154350f5bc802e0462893541a39ab1ac0c54939d3cabadd7af455af5c779b882d93d5a4b5e4037
7
- data.tar.gz: 7b0065714791944b3cabf2673f93a18277ec2cc721d85e8b5609ab94b7ff5928f8adf1500219aad4f659c27f5774e465739960c5a84468d6a0759ac07862c43b
6
+ metadata.gz: c9d34362e98ddce5e8f771233ad43a803cf66e71bcd546dedaee3ae4a06991149f2e07d9e6024cbc111ee3166db7c193772cfdf7c78bec729da772b5db06a5bf
7
+ data.tar.gz: 4c9fdee8046e62e40be983d6f28f8e0fc703f89129e3d41d7ee9003aefa1c344bd170b7d4a5c1575729b80363ee2a7cfb48ad6a299564db3aa52da2fef0090c4
@@ -1,10 +1,14 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
+ if Rails.version >= '6.1'
4
+ require 'rails_multisite/connection_management/rails_61_compat'
5
+ else
6
+ require 'rails_multisite/connection_management/rails_60_compat'
7
+ end
8
+
3
9
  module RailsMultisite
4
10
  class ConnectionManagement
5
-
6
11
  DEFAULT = 'default'
7
- SPEC_KLASS = ActiveRecord::ConnectionAdapters::ConnectionSpecification
8
12
 
9
13
  def self.default_config_filename
10
14
  File.absolute_path(Rails.root.to_s + "/config/multisite.yml")
@@ -104,16 +108,11 @@ module RailsMultisite
104
108
  end
105
109
 
106
110
  def self.current_hostname
107
- spec = @instance.connection_spec(db: self.current_db) if @instance
108
- spec ||= ActiveRecord::Base.connection_pool.spec
109
- config = spec.config
110
- config[:host_names].nil? ? config[:host] : config[:host_names].first
111
+ current_db_hostnames.first
111
112
  end
112
113
 
113
114
  def self.current_db_hostnames
114
- spec = @instance.connection_spec(db: self.current_db) if @instance
115
- spec ||= ActiveRecord::Base.connection_pool.spec
116
- config = spec.config
115
+ config = (@instance&.connection_spec(db: current_db) || ConnectionSpecification.current).config
117
116
  config[:host_names].nil? ? [config[:host]] : config[:host_names]
118
117
  end
119
118
 
@@ -121,7 +120,7 @@ module RailsMultisite
121
120
  if @instance
122
121
  @instance.connection_spec(opts)
123
122
  else
124
- ActiveRecord::Base.connection_pool.spec
123
+ ConnectionSpecification.current
125
124
  end
126
125
  end
127
126
 
@@ -170,7 +169,7 @@ module RailsMultisite
170
169
  end
171
170
 
172
171
  @db_spec_cache = {}
173
- @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
172
+ @default_spec = ConnectionSpecification.default
174
173
  @default_connection_handler = ActiveRecord::Base.connection_handler
175
174
 
176
175
  @reload_mutex = Mutex.new
@@ -181,7 +180,7 @@ module RailsMultisite
181
180
  def load_config!
182
181
  configs = YAML::load(File.open(@config_filename))
183
182
 
184
- no_prepared_statements = ActiveRecord::Base.configurations[Rails.env]["prepared_statements"] == false
183
+ no_prepared_statements = @default_spec.config[:prepared_statements] == false
185
184
 
186
185
  configs.each do |k, v|
187
186
  raise ArgumentError.new("Please do not name any db default!") if k == DEFAULT
@@ -189,17 +188,8 @@ module RailsMultisite
189
188
  v[:prepared_statements] = false if no_prepared_statements
190
189
  end
191
190
 
192
- resolve_configs = configs
193
-
194
- # rails 6 needs to use a proper object for the resolver
195
- if defined?(ActiveRecord::DatabaseConfigurations)
196
- resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
197
- end
198
-
199
- resolver = SPEC_KLASS::Resolver.new(resolve_configs)
200
-
201
191
  # Build a hash of db name => spec
202
- new_db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
192
+ new_db_spec_cache = ConnectionSpecification.db_spec_cache(configs)
203
193
  new_db_spec_cache.each do |k, v|
204
194
  # If spec already existed, use the old version
205
195
  if v&.to_hash == @db_spec_cache[k]&.to_hash
@@ -217,7 +207,7 @@ module RailsMultisite
217
207
  end
218
208
 
219
209
  # Add the default hostnames as well
220
- ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
210
+ @default_spec.config[:host_names].each do |host|
221
211
  new_host_spec_cache[host] = @default_spec
222
212
  end
223
213
 
@@ -365,7 +355,7 @@ module RailsMultisite
365
355
  end
366
356
 
367
357
  def current_db
368
- ActiveRecord::Base.connection_pool.spec.config[:db_key] || DEFAULT
358
+ ConnectionSpecification.current.config[:db_key] || DEFAULT
369
359
  end
370
360
 
371
361
  def current_hostname
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class ConnectionManagement
5
+ class ConnectionSpecification
6
+ class << self
7
+ def current
8
+ ActiveRecord::Base.connection_pool.spec
9
+ end
10
+
11
+ def db_spec_cache(configs)
12
+ resolve_configs = configs
13
+ # rails 6 needs to use a proper object for the resolver
14
+ if defined?(ActiveRecord::DatabaseConfigurations)
15
+ resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
16
+ end
17
+ resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(resolve_configs)
18
+ configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.to_h
19
+ end
20
+
21
+ def default
22
+ ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
23
+ .new(ActiveRecord::Base.configurations)
24
+ .spec(Rails.env.to_sym)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class ConnectionManagement
5
+ class ConnectionSpecification
6
+ class << self
7
+ def current
8
+ new(ActiveRecord::Base.connection_pool.db_config)
9
+ end
10
+
11
+ def db_spec_cache(configs)
12
+ resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
13
+ configs.map { |k, _| [k, new(resolve_configs.resolve(k.to_sym))] }.to_h
14
+ end
15
+
16
+ def default
17
+ new(ActiveRecord::Base.configurations.resolve(Rails.env.to_sym))
18
+ end
19
+ end
20
+
21
+ attr_reader :spec
22
+
23
+ def initialize(spec)
24
+ @spec = spec
25
+ end
26
+
27
+ def name
28
+ spec.env_name
29
+ end
30
+
31
+ def to_hash
32
+ spec.configuration_hash
33
+ end
34
+ alias config to_hash
35
+ end
36
+ end
37
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.5.0"
4
+ VERSION = "3.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_multisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2021-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -61,6 +61,8 @@ files:
61
61
  - README.md
62
62
  - lib/rails_multisite.rb
63
63
  - lib/rails_multisite/connection_management.rb
64
+ - lib/rails_multisite/connection_management/rails_60_compat.rb
65
+ - lib/rails_multisite/connection_management/rails_61_compat.rb
64
66
  - lib/rails_multisite/formatter.rb
65
67
  - lib/rails_multisite/middleware.rb
66
68
  - lib/rails_multisite/railtie.rb
@@ -85,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
87
  - !ruby/object:Gem::Version
86
88
  version: '0'
87
89
  requirements: []
88
- rubygems_version: 3.0.3
90
+ rubygems_version: 3.2.2
89
91
  signing_key:
90
92
  specification_version: 4
91
93
  summary: Multi tenancy support for Rails