rails_multisite 2.0.6 → 2.2.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails_multisite might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2dcf0982fa73681b77ead88c24501dd8a8dba972b5bd23ecbc3779095dc53168
4
- data.tar.gz: 985413ce023cebd3e215b9a961547fa4e2643667dacce3136469236d35ad6ccb
3
+ metadata.gz: 6cf1529e713938afd1edfb04e2e52f8199f86901f3278821d8514c9bc5855b27
4
+ data.tar.gz: b5d16cf92694fe94445a141ebc0e8bf2817b665d9789f3ba0ac79ef7af369ed4
5
5
  SHA512:
6
- metadata.gz: 4a459dc64ff1a9396637e74547fc72c4619c5314a919ef906b54cd94c17d5bdb9042daab13fc04e1be85c1e367030933ac8d40585ba83827cd3f3418155e3545
7
- data.tar.gz: e4f77a94d8e91f38c31142175835eb16884d7e5b5f6bcfa4c55c621980f310c48bea4123fd930b50a69f2cb7e0a887154fd2c8210d6a811acc5faffeeb50cb26
6
+ metadata.gz: 7ecfa664b8367ebd065ecfee14ab1f52ab613aef1c7d404c2944cbedcc93c45899ef53544ca6915260aca0cd5ed6823e6b1bf032569f69fce3001bdaccda12bf
7
+ data.tar.gz: d3bb77f2e4906ae9736fba551c40972be4a9bd15777447cb5eaaf45d8dc0b0ecf9fa23d1c2baf09dd4b20304776b2b8f7c929aa2173846d64219c73885be6ccf
data/README.md CHANGED
@@ -35,7 +35,6 @@ mlp:
35
35
  host: dbhost
36
36
  pool: 5
37
37
  timeout: 5000
38
- db_id: 1 # ensure db_id is unique for each site
39
38
  host_names:
40
39
  - discourse.equestria.com
41
40
  - discourse.equestria.internal
@@ -48,7 +47,6 @@ drwho:
48
47
  host: dbhost
49
48
  pool: 5
50
49
  timeout: 5000
51
- db_id: 2 # ensure db_id is unique for each site
52
50
  host_names:
53
51
  - discuss.tardis.gallifrey
54
52
  ```
@@ -92,7 +90,7 @@ db_two:
92
90
  To get a Rails console that is connected to `some_database_1` database:
93
91
 
94
92
  ```
95
- RAILS_ENV=db_one rails console
93
+ RAILS_DB=db_one rails console
96
94
  ```
97
95
 
98
96
 
@@ -4,12 +4,17 @@ module RailsMultisite
4
4
  class ConnectionManagement
5
5
 
6
6
  DEFAULT = 'default'
7
+ SPEC_KLASS = ActiveRecord::ConnectionAdapters::ConnectionSpecification
7
8
 
8
9
  def self.default_config_filename
9
10
  File.absolute_path(Rails.root.to_s + "/config/multisite.yml")
10
11
  end
11
12
 
12
13
  def self.clear_settings!
14
+ @instance&.db_spec_cache&.each do |key, spec|
15
+ @instance.connection_handlers.delete(self.handler_key(spec))
16
+ end
17
+
13
18
  @instance = nil
14
19
  end
15
20
 
@@ -35,7 +40,7 @@ module RailsMultisite
35
40
  end
36
41
 
37
42
  def self.reload
38
- @instance = new(instance.config_filename)
43
+ @instance.reload
39
44
  end
40
45
 
41
46
  def self.has_db?(db)
@@ -97,6 +102,13 @@ module RailsMultisite
97
102
  config[:host_names].nil? ? config[:host] : config[:host_names].first
98
103
  end
99
104
 
105
+ def self.current_db_hostnames
106
+ spec = @instance.connection_spec(db: self.current_db) if @instance
107
+ spec ||= ActiveRecord::Base.connection_pool.spec
108
+ config = spec.config
109
+ config[:host_names].nil? ? [config[:host]] : config[:host_names]
110
+ end
111
+
100
112
  def self.connection_spec(opts)
101
113
  if @instance
102
114
  @instance.connection_spec(opts)
@@ -113,12 +125,42 @@ module RailsMultisite
113
125
  end
114
126
  end
115
127
 
116
- attr_reader :config_filename
128
+ def self.handler_key(spec)
129
+ @handler_key_suffix ||= begin
130
+ if ActiveRecord::Base.respond_to?(:writing_role)
131
+ "_#{ActiveRecord::Base.writing_role}"
132
+ else
133
+ ""
134
+ end
135
+ end
136
+
137
+ :"#{spec.name}#{@handler_key_suffix}"
138
+ end
139
+
140
+ attr_reader :config_filename, :db_spec_cache, :connection_handlers
117
141
 
118
142
  def initialize(config_filename)
119
143
  @config_filename = config_filename
120
144
 
121
- spec_klass = ActiveRecord::ConnectionAdapters::ConnectionSpecification
145
+ @connection_handlers = begin
146
+ if ActiveRecord::Base.respond_to?(:connection_handlers)
147
+ ActiveRecord::Base.connection_handlers
148
+ else
149
+ {}
150
+ end
151
+ end
152
+
153
+ @db_spec_cache = {}
154
+ @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
155
+ @default_connection_handler = ActiveRecord::Base.connection_handler
156
+ @established_default = false
157
+
158
+ @reload_mutex = Mutex.new
159
+
160
+ load_config!
161
+ end
162
+
163
+ def load_config!
122
164
  configs = YAML::load(File.open(@config_filename))
123
165
 
124
166
  no_prepared_statements = ActiveRecord::Base.configurations[Rails.env]["prepared_statements"] == false
@@ -129,27 +171,52 @@ module RailsMultisite
129
171
  v[:prepared_statements] = false if no_prepared_statements
130
172
  end
131
173
 
132
- resolver = spec_klass::Resolver.new(configs)
133
- @db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
174
+ resolve_configs = configs
175
+
176
+ # rails 6 needs to use a proper object for the resolver
177
+ if defined?(ActiveRecord::DatabaseConfigurations)
178
+ resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
179
+ end
134
180
 
135
- @host_spec_cache = {}
181
+ resolver = SPEC_KLASS::Resolver.new(resolve_configs)
136
182
 
183
+ # Build a hash of db name => spec
184
+ new_db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
185
+ new_db_spec_cache.each do |k, v|
186
+ # If spec already existed, use the old version
187
+ if v&.to_hash == @db_spec_cache[k]&.to_hash
188
+ new_db_spec_cache[k] = @db_spec_cache[k]
189
+ end
190
+ end
191
+
192
+ # Build a hash of hostname => spec
193
+ new_host_spec_cache = {}
137
194
  configs.each do |k, v|
138
195
  next unless v["host_names"]
139
196
  v["host_names"].each do |host|
140
- @host_spec_cache[host] = @db_spec_cache[k]
197
+ new_host_spec_cache[host] = new_db_spec_cache[k]
141
198
  end
142
199
  end
143
200
 
144
- @default_spec = spec_klass::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
201
+ # Add the default hostnames as well
145
202
  ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
146
- @host_spec_cache[host] = @default_spec
203
+ new_host_spec_cache[host] = @default_spec
147
204
  end
148
205
 
149
- @default_connection_handler = ActiveRecord::Base.connection_handler
206
+ removed_dbs = @db_spec_cache.keys - new_db_spec_cache.keys
207
+ removed_specs = @db_spec_cache.values_at(*removed_dbs)
150
208
 
151
- @connection_handlers = {}
152
- @established_default = false
209
+ @host_spec_cache = new_host_spec_cache
210
+ @db_spec_cache = new_db_spec_cache
211
+
212
+ # Clean up connection handler cache.
213
+ removed_specs.each { |s| @connection_handlers.delete(handler_key(s)) }
214
+ end
215
+
216
+ def reload
217
+ @reload_mutex.synchronize do
218
+ load_config!
219
+ end
153
220
  end
154
221
 
155
222
  def has_db?(db)
@@ -171,11 +238,11 @@ module RailsMultisite
171
238
  spec ||= @default_spec
172
239
  handler = nil
173
240
  if spec != @default_spec
174
- handler = @connection_handlers[spec]
241
+ handler = @connection_handlers[handler_key(spec)]
175
242
  unless handler
176
243
  handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
177
244
  handler_establish_connection(handler, spec)
178
- @connection_handlers[spec] = handler
245
+ @connection_handlers[handler_key(spec)] = handler
179
246
  end
180
247
  else
181
248
  handler = @default_connection_handler
@@ -313,11 +380,11 @@ module RailsMultisite
313
380
  private
314
381
 
315
382
  def handler_establish_connection(handler, spec)
316
- if Rails::VERSION::MAJOR >= 5
317
- handler.establish_connection(spec.config)
318
- else
319
- handler.establish_connection(ActiveRecord::Base, spec)
320
- end
383
+ handler.establish_connection(spec.config)
384
+ end
385
+
386
+ def handler_key(spec)
387
+ self.class.handler_key(spec)
321
388
  end
322
389
 
323
390
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module RailsMultisite
2
3
  class Middleware
3
4
  def initialize(app, config = nil)
@@ -7,20 +7,28 @@ module RailsMultisite
7
7
  end
8
8
 
9
9
  initializer "RailsMultisite.init" do |app|
10
- Rails.configuration.multisite = false
10
+ app.config.multisite = false
11
11
 
12
12
  config_file = ConnectionManagement.default_config_filename
13
13
  if File.exist?(config_file)
14
14
  ConnectionManagement.config_filename = ConnectionManagement.default_config_filename
15
- Rails.configuration.multisite = true
15
+ app.config.multisite = true
16
16
  Rails.logger.formatter = RailsMultisite::Formatter.new
17
- app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
18
- app.middleware.delete(ActionDispatch::Executor)
17
+
18
+ if !skip_middleware?(app.config)
19
+ app.middleware.insert_after(ActionDispatch::Executor, RailsMultisite::Middleware)
20
+ app.middleware.delete(ActionDispatch::Executor)
21
+ end
19
22
 
20
23
  if ENV['RAILS_DB'].present?
21
24
  ConnectionManagement.establish_connection(db: ENV['RAILS_DB'], raise_on_missing: true)
22
25
  end
23
26
  end
24
27
  end
28
+
29
+ def skip_middleware?(config)
30
+ return false if !config.respond_to?(:skip_multisite_middleware)
31
+ config.skip_multisite_middleware
32
+ end
25
33
  end
26
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.0.6"
4
+ VERSION = "2.2.2"
5
5
  end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  desc "migrate all sites in tier"
2
3
  task "multisite:migrate" => :environment do
3
4
  RailsMultisite::ConnectionManagement.each_connection do |db|
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  desc "generate multisite config file (if missing)"
2
3
  task "multisite:generate:config" => :environment do
3
4
  filename = RailsMultisite::ConnectionManagement.config_filename
@@ -14,7 +15,6 @@ task "multisite:generate:config" => :environment do
14
15
  # host: localhost
15
16
  # pool: 5
16
17
  # timeout: 5000
17
- # db_id: 1 # optionally include other settings you need
18
18
  # host_names:
19
19
  # - www.mysite.com
20
20
  # - www.anothersite.com
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.0.6
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Saffron
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-23 00:00:00.000000000 Z
11
+ date: 2020-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - ">"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.2'
19
+ version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6'
22
+ version: '7'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - ">"
28
28
  - !ruby/object:Gem::Version
29
- version: '4.2'
29
+ version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6'
32
+ version: '7'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: railties
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - ">"
38
38
  - !ruby/object:Gem::Version
39
- version: '4.2'
39
+ version: '5.0'
40
40
  - - "<"
41
41
  - !ruby/object:Gem::Version
42
- version: '6'
42
+ version: '7'
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - ">"
48
48
  - !ruby/object:Gem::Version
49
- version: '4.2'
49
+ version: '5.0'
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
- version: '6'
52
+ version: '7'
53
53
  description: Multi tenancy support for Rails
54
54
  email:
55
55
  - sam.saffron@gmail.com
@@ -78,15 +78,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="
80
80
  - !ruby/object:Gem::Version
81
- version: 2.2.0
81
+ version: 2.4.0
82
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  requirements:
84
84
  - - ">="
85
85
  - !ruby/object:Gem::Version
86
86
  version: '0'
87
87
  requirements: []
88
- rubyforge_project:
89
- rubygems_version: 2.7.6
88
+ rubygems_version: 3.0.3
90
89
  signing_key:
91
90
  specification_version: 4
92
91
  summary: Multi tenancy support for Rails