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

Returns a new instance of SchemaCache.



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

def initialize
  @columns      = {}
  @columns_hash = {}
  @primary_keys = {}
  @data_sources = {}
  @indexes      = {}
  @database_version = nil
  @version = nil
end

Class Method Details

._load_from(filename) ⇒ Object

:nodoc:



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 234

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

.load_from(filename) ⇒ Object

:nodoc:



228
229
230
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 228

def load_from(filename) # :nodoc:
  BoundSchemaReflection.new(SchemaReflection.new(filename), nil)
end

.new(connection) ⇒ Object



223
224
225
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 223

def new(connection)
  BoundSchemaReflection.new(SchemaReflection.new(nil), connection)
end

Instance Method Details

#add(connection, table_name) ⇒ Object

Add internal cache for table with table_name.



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

def add(connection, table_name)
  if data_source_exists?(connection, table_name)
    primary_keys(connection, table_name)
    columns(connection, table_name)
    columns_hash(connection, table_name)
    indexes(connection, table_name)
  end
end

#add_all(connection) ⇒ Object

:nodoc:



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

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

  version(connection)
  database_version(connection)
end

#cached?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 303

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.



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

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(connection, table_name) ⇒ Object

Get the columns for a table



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

def columns(connection, table_name)
  if ignored_table?(table_name)
    raise ActiveRecord::StatementInvalid, "Table '#{table_name}' doesn't exist"
  end

  @columns.fetch(table_name) do
    @columns[deep_deduplicate(table_name)] = deep_deduplicate(connection.columns(table_name))
  end
end

#columns_hash(connection, 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(connection, table_name)
  @columns_hash.fetch(table_name) do
    @columns_hash[deep_deduplicate(table_name)] = columns(connection, table_name).index_by(&:name).freeze
  end
end

#columns_hash?(connection, 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?(connection, table_name)
  @columns_hash.key?(table_name)
end

#data_source_exists?(connection, name) ⇒ Boolean

A cached lookup for table existence.

Returns:

  • (Boolean)


316
317
318
319
320
321
322
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 316

def data_source_exists?(connection, name)
  return if ignored_table?(name)
  prepare_data_sources(connection) if @data_sources.empty?
  return @data_sources[name] if @data_sources.key? name

  @data_sources[deep_deduplicate(name)] = connection.data_source_exists?(name)
end

#data_sources(_connection, name) ⇒ Object

:nodoc:



334
335
336
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 334

def data_sources(_connection, name) # :nodoc:
  @data_sources[name]
end

#database_version(connection) ⇒ Object

:nodoc:



373
374
375
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 373

def database_version(connection) # :nodoc:
  @database_version ||= connection.get_database_version
end

#dump_to(filename) ⇒ Object



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

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:



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

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
  coder["database_version"] = @database_version
end

#indexes(connection, table_name) ⇒ Object



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

def indexes(connection, table_name)
  @indexes.fetch(table_name) do
    if data_source_exists?(connection, table_name)
      @indexes[deep_deduplicate(table_name)] = deep_deduplicate(connection.indexes(table_name))
    else
      []
    end
  end
end

#init_with(coder) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 289

def init_with(coder)
  @columns          = coder["columns"]
  @columns_hash     = coder["columns_hash"]
  @primary_keys     = coder["primary_keys"]
  @data_sources     = coder["data_sources"]
  @indexes          = coder["indexes"] || {}
  @version          = coder["version"]
  @database_version = coder["database_version"]

  unless coder["deduplicated"]
    derive_columns_hash_and_deduplicate_values
  end
end

#initialize_dup(other) ⇒ Object

:nodoc:



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

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:



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

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

#marshal_load(array) ⇒ Object

:nodoc:



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

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(connection, table_name) ⇒ Object



307
308
309
310
311
312
313
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 307

def primary_keys(connection, table_name)
  @primary_keys.fetch(table_name) do
    if data_source_exists?(connection, table_name)
      @primary_keys[deep_deduplicate(table_name)] = deep_deduplicate(connection.primary_key(table_name))
    end
  end
end

#schema_versionObject



381
382
383
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 381

def schema_version
  @version
end

#sizeObject



385
386
387
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 385

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

#version(connection) ⇒ Object



377
378
379
# File 'lib/active_record/connection_adapters/schema_cache.rb', line 377

def version(connection)
  @version ||= connection.schema_version
end