rails_multisite 2.5.0 → 4.0.1

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: c1b6e5b2d0e1ca292a9ab4b32619f7b090cc8f6cd7f7e961dd8d84b6d8eb5327
4
+ data.tar.gz: 24fb6fe44ef528dd8dbfdd1e79ef2b70d8c41f53d133a23ec457fea9743423fc
5
5
  SHA512:
6
- metadata.gz: 8a9bccdd03ddb205a20325ed127cc0e40b6e7c790147334ac9154350f5bc802e0462893541a39ab1ac0c54939d3cabadd7af455af5c779b882d93d5a4b5e4037
7
- data.tar.gz: 7b0065714791944b3cabf2673f93a18277ec2cc721d85e8b5609ab94b7ff5928f8adf1500219aad4f659c27f5774e465739960c5a84468d6a0759ac07862c43b
6
+ metadata.gz: 597ff3dce81db99a341eb8fd10a656bc789b6f6dcc540e580b53b9e7ea8a392c006255f37bd249731412e71b883497479cd18e168871c7ee8260df712dc48d71
7
+ data.tar.gz: a6dd8debbb37fba683da03fb7cf71a95c757a8ce2e9393ecbb211cfc64b407f7a78adc3bfcc357efb4049c9e60b733aa62ad6a704a7b3e31b875bef7f297ba6c
@@ -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,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
@@ -179,9 +178,9 @@ module RailsMultisite
179
178
  end
180
179
 
181
180
  def load_config!
182
- configs = YAML::load(File.open(@config_filename))
181
+ configs = YAML.safe_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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsMultisite
4
+ class CookieSalt
5
+ COOKIE_SALT_KEYS = [
6
+ "action_dispatch.signed_cookie_salt",
7
+ "action_dispatch.encrypted_cookie_salt",
8
+ "action_dispatch.encrypted_signed_cookie_salt",
9
+ "action_dispatch.authenticated_encrypted_cookie_salt"
10
+ ]
11
+
12
+ def self.update_cookie_salts(env:, host:)
13
+ COOKIE_SALT_KEYS.each { |key| env[key] = "#{env[key]} #{host}" }
14
+ end
15
+ end
16
+ end
@@ -22,6 +22,7 @@ module RailsMultisite
22
22
 
23
23
  ActiveRecord::Base.connection_handler.clear_active_connections!
24
24
  ConnectionManagement.establish_connection(host: host, db: db)
25
+ CookieSalt.update_cookie_salts(env: env, host: host)
25
26
  @app.call(env)
26
27
  ensure
27
28
  ActiveRecord::Base.connection_handler.clear_active_connections!
@@ -9,9 +9,14 @@ module RailsMultisite
9
9
  initializer "RailsMultisite.init" do |app|
10
10
  app.config.multisite = false
11
11
 
12
- config_file = ConnectionManagement.default_config_filename
12
+ config_file =
13
+ app.config.respond_to?(:multisite_config_path) &&
14
+ app.config.multisite_config_path.presence
15
+
16
+ config_file ||= ConnectionManagement.default_config_filename
17
+
13
18
  if File.exist?(config_file)
14
- ConnectionManagement.config_filename = ConnectionManagement.default_config_filename
19
+ ConnectionManagement.config_filename = config_file
15
20
  app.config.multisite = true
16
21
  Rails.logger.formatter = RailsMultisite::Formatter.new
17
22
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.5.0"
4
+ VERSION = "4.0.1"
5
5
  end
@@ -7,3 +7,4 @@ require 'rails_multisite/railtie'
7
7
  require 'rails_multisite/formatter'
8
8
  require 'rails_multisite/connection_management'
9
9
  require 'rails_multisite/middleware'
10
+ require 'rails_multisite/cookie_salt'
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: 4.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-17 00:00:00.000000000 Z
11
+ date: 2022-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7'
22
+ version: '7.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: railties
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -39,7 +39,7 @@ dependencies:
39
39
  version: '5.0'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '7'
42
+ version: '7.1'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
@@ -49,7 +49,7 @@ dependencies:
49
49
  version: '5.0'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '7'
52
+ version: '7.1'
53
53
  description: Multi tenancy support for Rails
54
54
  email:
55
55
  - sam.saffron@gmail.com
@@ -61,6 +61,9 @@ 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
66
+ - lib/rails_multisite/cookie_salt.rb
64
67
  - lib/rails_multisite/formatter.rb
65
68
  - lib/rails_multisite/middleware.rb
66
69
  - lib/rails_multisite/railtie.rb
@@ -70,7 +73,7 @@ files:
70
73
  homepage: ''
71
74
  licenses: []
72
75
  metadata: {}
73
- post_install_message:
76
+ post_install_message:
74
77
  rdoc_options: []
75
78
  require_paths:
76
79
  - lib
@@ -78,15 +81,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
81
  requirements:
79
82
  - - ">="
80
83
  - !ruby/object:Gem::Version
81
- version: 2.4.0
84
+ version: 2.5.0
82
85
  required_rubygems_version: !ruby/object:Gem::Requirement
83
86
  requirements:
84
87
  - - ">="
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
90
  requirements: []
88
- rubygems_version: 3.0.3
89
- signing_key:
91
+ rubygems_version: 3.1.6
92
+ signing_key:
90
93
  specification_version: 4
91
94
  summary: Multi tenancy support for Rails
92
95
  test_files: []