Class: ActiveRecord::ConnectionAdapters::SchemaCache

Inherits:
Object
  • Object
show all
Defined in:
activerecord/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:



251
252
253
254
255
256
257
258
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 251

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
# File 'activerecord/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
      YAML.unsafe_load(file)
    end
  end
end

Instance Method Details

#add(pool, table_name) ⇒ Object

Add internal cache for table with table_name.



322
323
324
325
326
327
328
329
330
331
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 322

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:



392
393
394
395
396
397
398
399
400
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 392

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)


290
291
292
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 290

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.



384
385
386
387
388
389
390
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 384

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



334
335
336
337
338
339
340
341
342
343
344
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 334

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.



348
349
350
351
352
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 348

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)


355
356
357
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 355

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)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 305

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



402
403
404
405
406
407
408
409
410
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 402

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:



269
270
271
272
273
274
275
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 269

def encode_with(coder) # :nodoc:
  coder["columns"]          = @columns.sort.to_h.transform_values { _1.sort_by(&:name) }
  coder["primary_keys"]     = @primary_keys.sort.to_h
  coder["data_sources"]     = @data_sources.sort.to_h
  coder["indexes"]          = @indexes.sort.to_h.transform_values { _1.sort_by(&:name) }
  coder["version"]          = @version
end

#indexes(pool, table_name) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 359

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:



277
278
279
280
281
282
283
284
285
286
287
288
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 277

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:



260
261
262
263
264
265
266
267
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 260

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:



412
413
414
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 412

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

#marshal_load(array) ⇒ Object

:nodoc:



416
417
418
419
420
421
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 416

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



294
295
296
297
298
299
300
301
302
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 294

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



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

def schema_version
  @version
end

#sizeObject



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

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

#version(pool) ⇒ Object



371
372
373
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 371

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