rails_multisite 2.0.7 → 2.1.0

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: 1a0e0f9bb773343cb717c3342bdba03296eb1d7f298a26527aa435309aba5c71
4
- data.tar.gz: 8b2d59de4f66e24d753f72b4bf43618769178191e3b25a1117b8a0fd4460e0a0
3
+ metadata.gz: e9d253acd86ab0b218a43d6cf67bc7bd3948b03c8288872f75c8788ff1c54373
4
+ data.tar.gz: 1141a30cad5dba7992b6adc5ab1c315488789430f9bee82ba4bb0b189447d27c
5
5
  SHA512:
6
- metadata.gz: 706c6793500ab5a89ceec191519033263f2851c94fb707614b28c1c2da9fee89b274ee268bac880e8893a0f2ad4c24ee86d087894b4a9bec35fd28abe0200312
7
- data.tar.gz: a1156c538005d8cd01dfc3994c890a12cf872c4400b9c5e6e0ddcdd1e4d018ab9486edd3227b5c67718b643933258f6c009712e7c1c90b474cdc05c7d6e31a31
6
+ metadata.gz: 6a2a3ac0846e4b589a44e5285d8bba6be418e0a535c642a95f23073dcf16d51425e87d27ed74e5e58b04e25a5cc88620d7937530bd25285dd013d8e81689fb6e
7
+ data.tar.gz: 5520157f4aed32cc36b03e289ccb56b4f43385f7a153f64245a4ab570b13138826b8cf1d2aaac78b40b1d7231501575a53df4af0f1d56472ef267e82f90f6d92
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,6 +4,7 @@ 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")
@@ -35,7 +36,7 @@ module RailsMultisite
35
36
  end
36
37
 
37
38
  def self.reload
38
- @instance = new(instance.config_filename)
39
+ @instance.reload
39
40
  end
40
41
 
41
42
  def self.has_db?(db)
@@ -118,7 +119,19 @@ module RailsMultisite
118
119
  def initialize(config_filename)
119
120
  @config_filename = config_filename
120
121
 
121
- spec_klass = ActiveRecord::ConnectionAdapters::ConnectionSpecification
122
+ @connection_handlers = {}
123
+ @db_spec_cache = {}
124
+
125
+ @default_spec = SPEC_KLASS::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
126
+ @default_connection_handler = ActiveRecord::Base.connection_handler
127
+ @established_default = false
128
+
129
+ @reload_mutex = Mutex.new
130
+
131
+ load_config!
132
+ end
133
+
134
+ def load_config!
122
135
  configs = YAML::load(File.open(@config_filename))
123
136
 
124
137
  no_prepared_statements = ActiveRecord::Base.configurations[Rails.env]["prepared_statements"] == false
@@ -136,27 +149,46 @@ module RailsMultisite
136
149
  resolve_configs = ActiveRecord::DatabaseConfigurations.new(configs)
137
150
  end
138
151
 
139
- resolver = spec_klass::Resolver.new(resolve_configs)
140
- @db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
152
+ resolver = SPEC_KLASS::Resolver.new(resolve_configs)
141
153
 
142
- @host_spec_cache = {}
154
+ # Build a hash of db name => spec
155
+ new_db_spec_cache = Hash[*configs.map { |k, _| [k, resolver.spec(k.to_sym)] }.flatten]
156
+ new_db_spec_cache.each do |k, v|
157
+ # If spec already existed, use the old version
158
+ if v&.to_hash == @db_spec_cache[k]&.to_hash
159
+ new_db_spec_cache[k] = @db_spec_cache[k]
160
+ end
161
+ end
143
162
 
163
+ # Build a hash of hostname => spec
164
+ new_host_spec_cache = {}
144
165
  configs.each do |k, v|
145
166
  next unless v["host_names"]
146
167
  v["host_names"].each do |host|
147
- @host_spec_cache[host] = @db_spec_cache[k]
168
+ new_host_spec_cache[host] = new_db_spec_cache[k]
148
169
  end
149
170
  end
150
171
 
151
- @default_spec = spec_klass::Resolver.new(ActiveRecord::Base.configurations).spec(Rails.env.to_sym)
172
+ # Add the default hostnames as well
152
173
  ActiveRecord::Base.configurations[Rails.env]["host_names"].each do |host|
153
- @host_spec_cache[host] = @default_spec
174
+ new_host_spec_cache[host] = @default_spec
154
175
  end
155
176
 
156
- @default_connection_handler = ActiveRecord::Base.connection_handler
177
+ removed_dbs = @db_spec_cache.keys - new_db_spec_cache.keys
178
+ removed_specs = @db_spec_cache.values_at(*removed_dbs)
157
179
 
158
- @connection_handlers = {}
159
- @established_default = false
180
+ @host_spec_cache = new_host_spec_cache
181
+ @db_spec_cache = new_db_spec_cache
182
+
183
+ # Clean up connection handler cache.
184
+ # (@connection_handlers is a hash of spec => handler)
185
+ removed_specs.each { |s| @connection_handlers.delete(s) }
186
+ end
187
+
188
+ def reload
189
+ @reload_mutex.synchronize do
190
+ load_config!
191
+ end
160
192
  end
161
193
 
162
194
  def has_db?(db)
@@ -320,11 +352,7 @@ module RailsMultisite
320
352
  private
321
353
 
322
354
  def handler_establish_connection(handler, spec)
323
- if Rails::VERSION::MAJOR >= 5
324
- handler.establish_connection(spec.config)
325
- else
326
- handler.establish_connection(ActiveRecord::Base, spec)
327
- end
355
+ handler.establish_connection(spec.config)
328
356
  end
329
357
 
330
358
  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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  #
3
3
  module RailsMultisite
4
- VERSION = "2.0.7"
4
+ VERSION = "2.1.0"
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.7
4
+ version: 2.1.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: 2019-04-29 00:00:00.000000000 Z
11
+ date: 2020-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,7 +16,7 @@ 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
22
  version: '7'
@@ -26,7 +26,7 @@ dependencies:
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
32
  version: '7'
@@ -36,7 +36,7 @@ dependencies:
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
42
  version: '7'
@@ -46,7 +46,7 @@ dependencies:
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
52
  version: '7'
@@ -78,7 +78,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
78
  requirements:
79
79
  - - ">="
80
80
  - !ruby/object:Gem::Version
81
- version: 2.3.0
81
+ version: 2.4.0
82
82
  required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  requirements:
84
84
  - - ">="