/
schema_cache.rb
480 lines (393 loc) · 12.7 KB
/
schema_cache.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
219
220
221
222
223
224
225
226
227
228
229
230
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# frozen_string_literal: true
require "active_support/core_ext/file/atomic"
module ActiveRecord
module ConnectionAdapters
class SchemaReflection
class << self
attr_accessor :use_schema_cache_dump
attr_accessor :check_schema_cache_dump_version
end
self.use_schema_cache_dump = true
self.check_schema_cache_dump_version = true
def initialize(cache_path, cache = nil)
@cache = cache
@cache_path = cache_path
end
def clear!
@cache = empty_cache
nil
end
def load!(pool)
cache(pool)
self
end
def primary_keys(pool, table_name)
cache(pool).primary_keys(pool, table_name)
end
def data_source_exists?(pool, name)
cache(pool).data_source_exists?(pool, name)
end
def add(pool, name)
cache(pool).add(pool, name)
end
def data_sources(pool, name)
cache(pool).data_source_exists?(pool, name)
end
def columns(pool, table_name)
cache(pool).columns(pool, table_name)
end
def columns_hash(pool, table_name)
cache(pool).columns_hash(pool, table_name)
end
def columns_hash?(pool, table_name)
cache(pool).columns_hash?(pool, table_name)
end
def indexes(pool, table_name)
cache(pool).indexes(pool, table_name)
end
def version(pool)
cache(pool).version(pool)
end
def size(pool)
cache(pool).size
end
def clear_data_source_cache!(pool, name)
return if @cache.nil? && !possible_cache_available?
cache(pool).clear_data_source_cache!(pool, name)
end
def cached?(table_name)
if @cache.nil?
# If `check_schema_cache_dump_version` is enabled we can't load
# the schema cache dump without connecting to the database.
unless self.class.check_schema_cache_dump_version
@cache = load_cache(nil)
end
end
@cache&.cached?(table_name)
end
def dump_to(pool, filename)
fresh_cache = empty_cache
fresh_cache.add_all(pool)
fresh_cache.dump_to(filename)
@cache = fresh_cache
end
private
def empty_cache
new_cache = SchemaCache.allocate
new_cache.send(:initialize)
new_cache
end
def cache(pool)
@cache ||= load_cache(pool) || empty_cache
end
def possible_cache_available?
self.class.use_schema_cache_dump &&
@cache_path &&
File.file?(@cache_path)
end
def load_cache(pool)
# Can't load if schema dumps are disabled
return unless possible_cache_available?
# Check we can find one
return unless new_cache = SchemaCache._load_from(@cache_path)
if self.class.check_schema_cache_dump_version
begin
pool.with_connection do |connection|
current_version = connection.schema_version
if new_cache.version(connection) != current_version
warn "Ignoring #{@cache_path} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{new_cache.schema_version}."
return
end
end
rescue ActiveRecordError => error
warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
return
end
end
new_cache
end
end
class BoundSchemaReflection
class FakePool # :nodoc
def initialize(connection)
@connection = connection
end
def with_connection
yield @connection
end
end
class << self
def for_lone_connection(abstract_schema_reflection, connection) # :nodoc:
new(abstract_schema_reflection, FakePool.new(connection))
end
end
def initialize(abstract_schema_reflection, pool)
@schema_reflection = abstract_schema_reflection
@pool = pool
end
def clear!
@schema_reflection.clear!
end
def load!
@schema_reflection.load!(@pool)
end
def cached?(table_name)
@schema_reflection.cached?(table_name)
end
def primary_keys(table_name)
@schema_reflection.primary_keys(@pool, table_name)
end
def data_source_exists?(name)
@schema_reflection.data_source_exists?(@pool, name)
end
def add(name)
@schema_reflection.add(@pool, name)
end
def data_sources(name)
@schema_reflection.data_sources(@pool, name)
end
def columns(table_name)
@schema_reflection.columns(@pool, table_name)
end
def columns_hash(table_name)
@schema_reflection.columns_hash(@pool, table_name)
end
def columns_hash?(table_name)
@schema_reflection.columns_hash?(@pool, table_name)
end
def indexes(table_name)
@schema_reflection.indexes(@pool, table_name)
end
def version
@schema_reflection.version(@pool)
end
def size
@schema_reflection.size(@pool)
end
def clear_data_source_cache!(name)
@schema_reflection.clear_data_source_cache!(@pool, name)
end
def dump_to(filename)
@schema_reflection.dump_to(@pool, filename)
end
end
# = Active Record Connection Adapters Schema Cache
class SchemaCache
def self._load_from(filename) # :nodoc:
return unless File.file?(filename)
read(filename) do |file|
if filename.include?(".dump")
Marshal.load(file)
else
if YAML.respond_to?(:unsafe_load)
YAML.unsafe_load(file)
else
YAML.load(file)
end
end
end
end
def self.read(filename, &block)
if File.extname(filename) == ".gz"
Zlib::GzipReader.open(filename) { |gz|
yield gz.read
}
else
yield File.read(filename)
end
end
private_class_method :read
def initialize # :nodoc:
@columns = {}
@columns_hash = {}
@primary_keys = {}
@data_sources = {}
@indexes = {}
@version = nil
end
def initialize_dup(other) # :nodoc:
super
@columns = @columns.dup
@columns_hash = @columns_hash.dup
@primary_keys = @primary_keys.dup
@data_sources = @data_sources.dup
@indexes = @indexes.dup
end
def encode_with(coder) # :nodoc:
coder["columns"] = @columns.sort.to_h
coder["primary_keys"] = @primary_keys.sort.to_h
coder["data_sources"] = @data_sources.sort.to_h
coder["indexes"] = @indexes.sort.to_h
coder["version"] = @version
end
def init_with(coder) # :nodoc:
@columns = coder["columns"]
@columns_hash = coder["columns_hash"]
@primary_keys = coder["primary_keys"]
@data_sources = coder["data_sources"]
@indexes = coder["indexes"] || {}
@version = coder["version"]
unless coder["deduplicated"]
derive_columns_hash_and_deduplicate_values
end
end
def cached?(table_name)
@columns.key?(table_name)
end
def primary_keys(pool, table_name)
@primary_keys.fetch(table_name) do
pool.with_connection do |connection|
if data_source_exists?(pool, table_name)
@primary_keys[deep_deduplicate(table_name)] = deep_deduplicate(connection.primary_key(table_name))
end
end
end
end
# A cached lookup for table existence.
def data_source_exists?(pool, name)
return if ignored_table?(name)
if @data_sources.empty?
tables_to_cache(pool).each do |source|
@data_sources[source] = true
end
end
return @data_sources[name] if @data_sources.key? name
@data_sources[deep_deduplicate(name)] = pool.with_connection do |connection|
connection.data_source_exists?(name)
end
end
# Add internal cache for table with +table_name+.
def add(pool, table_name)
pool.with_connection do
if data_source_exists?(pool, table_name)
primary_keys(pool, table_name)
columns(pool, table_name)
columns_hash(pool, table_name)
indexes(pool, table_name)
end
end
end
# Get the columns for a table
def columns(pool, table_name)
if ignored_table?(table_name)
raise ActiveRecord::StatementInvalid.new("Table '#{table_name}' doesn't exist", connection_pool: pool)
end
@columns.fetch(table_name) do
pool.with_connection do |connection|
@columns[deep_deduplicate(table_name)] = deep_deduplicate(connection.columns(table_name))
end
end
end
# Get the columns for a table as a hash, key is the column name
# value is the column object.
def columns_hash(pool, table_name)
@columns_hash.fetch(table_name) do
@columns_hash[deep_deduplicate(table_name)] = columns(pool, table_name).index_by(&:name).freeze
end
end
# Checks whether the columns hash is already cached for a table.
def columns_hash?(_pool, table_name)
@columns_hash.key?(table_name)
end
def indexes(pool, table_name)
@indexes.fetch(table_name) do
pool.with_connection do |connection|
if data_source_exists?(pool, table_name)
@indexes[deep_deduplicate(table_name)] = deep_deduplicate(connection.indexes(table_name))
else
[]
end
end
end
end
def version(pool)
@version ||= pool.with_connection(&:schema_version)
end
def schema_version
@version
end
def size
[@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size)
end
# Clear out internal caches for the data source +name+.
def clear_data_source_cache!(_connection, name)
@columns.delete name
@columns_hash.delete name
@primary_keys.delete name
@data_sources.delete name
@indexes.delete name
end
def add_all(pool) # :nodoc:
pool.with_connection do
tables_to_cache(pool).each do |table|
add(pool, table)
end
version(pool)
end
end
def dump_to(filename)
open(filename) { |f|
if filename.include?(".dump")
f.write(Marshal.dump(self))
else
f.write(YAML.dump(self))
end
}
end
def marshal_dump # :nodoc:
[@version, @columns, {}, @primary_keys, @data_sources, @indexes]
end
def marshal_load(array) # :nodoc:
@version, @columns, _columns_hash, @primary_keys, @data_sources, @indexes, _database_version = array
@indexes ||= {}
derive_columns_hash_and_deduplicate_values
end
private
def tables_to_cache(pool)
pool.with_connection do |connection|
connection.data_sources.reject do |table|
ignored_table?(table)
end
end
end
def ignored_table?(table_name)
ActiveRecord.schema_cache_ignored_tables.any? do |ignored|
ignored === table_name
end
end
def derive_columns_hash_and_deduplicate_values
@columns = deep_deduplicate(@columns)
@columns_hash = @columns.transform_values { |columns| columns.index_by(&:name) }
@primary_keys = deep_deduplicate(@primary_keys)
@data_sources = deep_deduplicate(@data_sources)
@indexes = deep_deduplicate(@indexes)
end
def deep_deduplicate(value)
case value
when Hash
value.transform_keys { |k| deep_deduplicate(k) }.transform_values { |v| deep_deduplicate(v) }
when Array
value.map { |i| deep_deduplicate(i) }
when String, Deduplicable
-value
else
value
end
end
def open(filename)
FileUtils.mkdir_p(File.dirname(filename))
File.atomic_write(filename) do |file|
if File.extname(filename) == ".gz"
zipper = Zlib::GzipWriter.new file
zipper.mtime = 0
yield zipper
zipper.flush
zipper.close
else
yield file
end
end
end
end
end
end