Class: RailsMultisite::ConnectionManagement
- Inherits:
-
Object
- Object
- RailsMultisite::ConnectionManagement
show all
- Defined in:
- lib/rails_multisite/connection_management.rb,
lib/rails_multisite/connection_management/rails_60_compat.rb,
lib/rails_multisite/connection_management/rails_61_compat.rb
Defined Under Namespace
Classes: ConnectionSpecification
Constant Summary
collapse
- DEFAULT =
'default'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of ConnectionManagement.
164
165
166
167
168
169
170
171
172
173
174
|
# File 'lib/rails_multisite/connection_management.rb', line 164
def initialize(config_filename)
@config_filename = config_filename
@db_spec_cache = {}
@default_spec = ConnectionSpecification.default
@default_connection_handler = ActiveRecord::Base.connection_handler
@reload_mutex = Mutex.new
load_config!
end
|
Instance Attribute Details
#config_filename ⇒ Object
Returns the value of attribute config_filename.
161
162
163
|
# File 'lib/rails_multisite/connection_management.rb', line 161
def config_filename
@config_filename
end
|
#db_spec_cache ⇒ Object
Returns the value of attribute db_spec_cache.
161
162
163
|
# File 'lib/rails_multisite/connection_management.rb', line 161
def db_spec_cache
@db_spec_cache
end
|
#default_connection_handler=(value) ⇒ Object
Sets the attribute default_connection_handler
162
163
164
|
# File 'lib/rails_multisite/connection_management.rb', line 162
def default_connection_handler=(value)
@default_connection_handler = value
end
|
Class Method Details
.all_dbs ⇒ Object
96
97
98
99
100
101
102
|
# File 'lib/rails_multisite/connection_management.rb', line 96
def self.all_dbs
if @instance
@instance.all_dbs
else
[DEFAULT]
end
end
|
.asset_hostnames ⇒ Object
44
45
46
|
# File 'lib/rails_multisite/connection_management.rb', line 44
def self.asset_hostnames
@asset_hostnames
end
|
.asset_hostnames=(h) ⇒ Object
48
49
50
|
# File 'lib/rails_multisite/connection_management.rb', line 48
def self.asset_hostnames=(h)
@asset_hostnames = h
end
|
.clear_settings! ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/rails_multisite/connection_management.rb', line 19
def self.clear_settings!
@instance&.db_spec_cache&.each do |key, spec|
@instance.connection_handlers.delete(self.handler_key(spec))
end
@instance = nil
end
|
.config_filename ⇒ Object
52
53
54
|
# File 'lib/rails_multisite/connection_management.rb', line 52
def self.config_filename
@instance.config_filename
end
|
.config_filename=(config_filename) ⇒ Object
36
37
38
39
40
41
42
|
# File 'lib/rails_multisite/connection_management.rb', line 36
def self.config_filename=(config_filename)
if config_filename.nil?
@instance = nil
else
@instance = new(config_filename)
end
end
|
.connection_spec(opts) ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/rails_multisite/connection_management.rb', line 121
def self.connection_spec(opts)
if @instance
@instance.connection_spec(opts)
else
ConnectionSpecification.current
end
end
|
.current_db ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/rails_multisite/connection_management.rb', line 104
def self.current_db
if @instance
instance.current_db
else
DEFAULT
end
end
|
.current_db_hostnames ⇒ Object
116
117
118
119
|
# File 'lib/rails_multisite/connection_management.rb', line 116
def self.current_db_hostnames
config = (@instance&.connection_spec(db: current_db) || ConnectionSpecification.current).config
config[:host_names].nil? ? [config[:host]] : config[:host_names]
end
|
.current_hostname ⇒ Object
112
113
114
|
# File 'lib/rails_multisite/connection_management.rb', line 112
def self.current_hostname
current_db_hostnames.first
end
|
.default_config_filename ⇒ Object
15
16
17
|
# File 'lib/rails_multisite/connection_management.rb', line 15
def self.default_config_filename
File.absolute_path(Rails.root.to_s + "/config/multisite.yml")
end
|
.default_connection_handler=(connection_handler) ⇒ Object
151
152
153
154
155
156
157
158
159
|
# File 'lib/rails_multisite/connection_management.rb', line 151
def self.default_connection_handler=(connection_handler)
if @instance
unless connection_handler.is_a?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
raise ArgumentError.new("Invalid connection handler")
end
@instance.default_connection_handler = connection_handler
end
end
|
.each_connection(opts = nil, &blk) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/rails_multisite/connection_management.rb', line 88
def self.each_connection(opts = nil, &blk)
if @instance
@instance.each_connection(opts, &blk)
else
with_connection(&blk)
end
end
|
.establish_connection(opts) ⇒ Object
65
66
67
|
# File 'lib/rails_multisite/connection_management.rb', line 65
def self.establish_connection(opts)
@instance.establish_connection(opts) if @instance
end
|
.handler_key(spec) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/rails_multisite/connection_management.rb', line 137
def self.handler_key(spec)
@handler_key_suffix ||= begin
if ActiveRecord.respond_to?(:writing_role)
"_#{ActiveRecord.writing_role}"
elsif ActiveRecord::Base.respond_to?(:writing_role)
"_#{ActiveRecord::Base.writing_role}"
else
""
end
end
:"#{spec.name}#{@handler_key_suffix}"
end
|
.has_db?(db) ⇒ Boolean
60
61
62
63
|
# File 'lib/rails_multisite/connection_management.rb', line 60
def self.has_db?(db)
return true if db == DEFAULT
!!(@instance && @instance.has_db?(db))
end
|
.host(env) ⇒ Object
129
130
131
132
133
134
135
|
# File 'lib/rails_multisite/connection_management.rb', line 129
def self.host(env)
if @instance
@instance.host(env)
else
env["HTTP_HOST"]
end
end
|
.instance ⇒ Object
32
33
34
|
# File 'lib/rails_multisite/connection_management.rb', line 32
def self.instance
@instance
end
|
.load_settings! ⇒ Object
27
28
29
30
|
# File 'lib/rails_multisite/connection_management.rb', line 27
def self.load_settings!
STDERR.puts "RailsMultisite::ConnectionManagement.load_settings! is deprecated"
end
|
.reload ⇒ Object
56
57
58
|
# File 'lib/rails_multisite/connection_management.rb', line 56
def self.reload
@instance.reload
end
|
.with_connection(db = DEFAULT, &blk) ⇒ Object
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/rails_multisite/connection_management.rb', line 77
def self.with_connection(db = DEFAULT, &blk)
if @instance
@instance.with_connection(db, &blk)
else
connected = ActiveRecord::Base.connection_pool.connected?
result = blk.call db
ActiveRecord::Base.clear_active_connections! unless connected
result
end
end
|
.with_hostname(hostname, &blk) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/rails_multisite/connection_management.rb', line 69
def self.with_hostname(hostname, &blk)
if @instance
@instance.with_hostname(hostname, &blk)
else
blk.call hostname
end
end
|
Instance Method Details
#all_dbs ⇒ Object
349
350
351
|
# File 'lib/rails_multisite/connection_management.rb', line 349
def all_dbs
[DEFAULT] + @db_spec_cache.keys.to_a
end
|
#connection_spec(opts) ⇒ Object
379
380
381
382
383
384
385
|
# File 'lib/rails_multisite/connection_management.rb', line 379
def connection_spec(opts)
if opts[:host]
@host_spec_cache[opts[:host]]
else
@db_spec_cache[opts[:db]]
end
end
|
#current_db ⇒ Object
353
354
355
|
# File 'lib/rails_multisite/connection_management.rb', line 353
def current_db
ConnectionSpecification.current.config[:db_key] || DEFAULT
end
|
#current_hostname ⇒ Object
357
358
359
|
# File 'lib/rails_multisite/connection_management.rb', line 357
def current_hostname
ConnectionManagement.current_hostname
end
|
#each_connection(opts = nil, &blk) ⇒ Object
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# File 'lib/rails_multisite/connection_management.rb', line 292
def each_connection(opts = nil, &blk)
old = current_db
connected = ActiveRecord::Base.connection_pool.connected?
queue = nil
threads = nil
if (opts && (threads = opts[:threads]))
queue = Queue.new
all_dbs.each { |db| queue << db }
end
errors = nil
if queue
threads.times.map do
Thread.new do
while true
begin
db = queue.deq(true)
rescue ThreadError
db = nil
end
break unless db
establish_connection(db: db)
begin
blk.call(db)
rescue => e
(errors ||= []) << e
end
ActiveRecord::Base.connection_handler.clear_active_connections!
end
end
end.map(&:join)
else
all_dbs.each do |db|
establish_connection(db: db)
blk.call(db)
ActiveRecord::Base.connection_handler.clear_active_connections!
end
end
if errors && errors.length > 0
raise StandardError, "Failed to run queries #{errors.inspect}"
end
ensure
establish_connection(db: old)
ActiveRecord::Base.connection_handler.clear_active_connections! unless connected
end
|
#establish_connection(opts) ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/rails_multisite/connection_management.rb', line 231
def establish_connection(opts)
opts[:db] = opts[:db].to_s
if opts[:db] != DEFAULT
spec = connection_spec(opts)
if (!spec && opts[:raise_on_missing])
raise "ERROR: #{opts[:db]} not found!"
end
end
spec ||= @default_spec
handler = nil
if spec != @default_spec
handler = connection_handlers[handler_key(spec)]
unless handler
handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
handler_establish_connection(handler, spec)
connection_handlers[handler_key(spec)] = handler
end
else
handler = @default_connection_handler
end
ActiveRecord::Base.connection_handler = handler
end
|
#has_db?(db) ⇒ Boolean
226
227
228
229
|
# File 'lib/rails_multisite/connection_management.rb', line 226
def has_db?(db)
return true if db == DEFAULT
@db_spec_cache[db]
end
|
#host(env) ⇒ Object
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
# File 'lib/rails_multisite/connection_management.rb', line 361
def host(env)
if host = env["RAILS_MULTISITE_HOST"]
return host
end
request = Rack::Request.new(env)
host =
if request['__ws'] && self.class.asset_hostnames&.include?(request.host)
request.cookies.clear
request['__ws']
else
request.host
end
env["RAILS_MULTISITE_HOST"] = host
end
|
#load_config! ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/rails_multisite/connection_management.rb', line 176
def load_config!
configs = YAML.safe_load(File.open(@config_filename))
no_prepared_statements = @default_spec.config[:prepared_statements] == false
configs.each do |k, v|
raise ArgumentError.new("Please do not name any db default!") if k == DEFAULT
v[:db_key] = k
v[:prepared_statements] = false if no_prepared_statements
end
new_db_spec_cache = ConnectionSpecification.db_spec_cache(configs)
new_db_spec_cache.each do |k, v|
if v&.to_hash == @db_spec_cache[k]&.to_hash
new_db_spec_cache[k] = @db_spec_cache[k]
end
end
new_host_spec_cache = {}
configs.each do |k, v|
next unless v["host_names"]
v["host_names"].each do |host|
new_host_spec_cache[host] = new_db_spec_cache[k]
end
end
@default_spec.config[:host_names].each do |host|
new_host_spec_cache[host] = @default_spec
end
removed_dbs = @db_spec_cache.keys - new_db_spec_cache.keys
removed_specs = @db_spec_cache.values_at(*removed_dbs)
@host_spec_cache = new_host_spec_cache
@db_spec_cache = new_db_spec_cache
removed_specs.each { |s| connection_handlers.delete(handler_key(s)) }
end
|
#reload ⇒ Object
220
221
222
223
224
|
# File 'lib/rails_multisite/connection_management.rb', line 220
def reload
@reload_mutex.synchronize do
load_config!
end
end
|
#with_connection(db = DEFAULT) ⇒ Object
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/rails_multisite/connection_management.rb', line 275
def with_connection(db = DEFAULT)
old = current_db
connected = ActiveRecord::Base.connection_pool.connected?
establish_connection(db: db) unless connected && db == old
rval = yield db
unless connected && db == old
ActiveRecord::Base.connection_handler.clear_active_connections!
establish_connection(db: old)
ActiveRecord::Base.connection_handler.clear_active_connections! unless connected
end
rval
end
|
#with_hostname(hostname) ⇒ Object
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/rails_multisite/connection_management.rb', line 258
def with_hostname(hostname)
old = current_hostname
connected = ActiveRecord::Base.connection_pool.connected?
establish_connection(host: hostname) unless connected && hostname == old
rval = yield hostname
unless connected && hostname == old
ActiveRecord::Base.connection_handler.clear_active_connections!
establish_connection(host: old)
ActiveRecord::Base.connection_handler.clear_active_connections! unless connected
end
rval
end
|