Class: ActiveRecord::ConnectionAdapters::SchemaCache

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/schema_cache.rb

Overview

Active Record Connection Adapters Schema Cache

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaCache

:nodoc:



255
256
257
258
259
260
261
262
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 255

def initialize # :nodoc:
  @columns      = {}
  @columns_hash = {}
  @primary_keys = {}
  @data_sources = {}
  @indexes      = {}
  @version = nil
end

Class Method Details

._load_from(filename) ⇒ Object

:nodoc:



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 228

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

Instance Method Details

#add(pool, table_name) ⇒ Object

Add internal cache for table with table_name.



326
327
328
329
330
331
332
333
334
335
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 326

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

#add_all(pool) ⇒ Object

:nodoc:



396
397
398
399
400
401
402
403
404
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 396

def add_all(pool) # :nodoc:
  pool.with_connection do
    tables_to_cache(pool).each do |table|
      add(pool, table)
    end

    version(pool)
  end
end

#cached?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


294
295
296
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 294

def cached?(table_name)
  @columns.key?(table_name)
end

#clear_data_source_cache!(_connection, name) ⇒ Object

Clear out internal caches for the data source name.



388
389
390
391
392
393
394
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 388

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

#columns(pool, table_name) ⇒ Object

Get the columns for a table



338
339
340
341
342
343
344
345
346
347
348
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 338

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

#columns_hash(pool, table_name) ⇒ Object

Get the columns for a table as a hash, key is the column name value is the column object.



352
353
354
355
356
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 352

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

#columns_hash?(_pool, table_name) ⇒ Boolean

Checks whether the columns hash is already cached for a table.

Returns:

  • (Boolean)


359
360
361
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 359

def columns_hash?(_pool, table_name)
  @columns_hash.key?(table_name)
end

#data_source_exists?(pool, name) ⇒ Boolean

A cached lookup for table existence.

Returns:

  • (Boolean)


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 309

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

#dump_to(filename) ⇒ Object



406
407
408
409
410
411
412
413
414
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 406

def dump_to(filename)
  open(filename) { |f|
    if filename.include?(".dump")
      f.write(Marshal.dump(self))
    else
      f.write(YAML.dump(self))
    end
  }
end

#encode_with(coder) ⇒ Object

:nodoc:



273
274
275
276
277
278
279
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 273

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

#indexes(pool, table_name) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 363

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

#init_with(coder) ⇒ Object

:nodoc:



281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 281

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

#initialize_dup(other) ⇒ Object

:nodoc:



264
265
266
267
268
269
270
271
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 264

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

#marshal_dumpObject

:nodoc:



416
417
418
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 416

def marshal_dump # :nodoc:
  [@version, @columns, {}, @primary_keys, @data_sources, @indexes]
end

#marshal_load(array) ⇒ Object

:nodoc:



420
421
422
423
424
425
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 420

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

#primary_keys(pool, table_name) ⇒ Object



298
299
300
301
302
303
304
305
306
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 298

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

#schema_versionObject



379
380
381
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 379

def schema_version
  @version
end

#sizeObject



383
384
385
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 383

def size
  [@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size)
end

#version(pool) ⇒ Object



375
376
377
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 375

def version(pool)
  @version ||= pool.with_connection(&:schema_version)
end