123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::ConnectionAdapters::SchemaCache

Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/connection_adapters/schema_cache.rb

Class Method Summary

Instance Method Summary

Constructor Details

.new(connection) ⇒ SchemaCache

Deprecated.

use ActiveRecord::ConnectionAdapters::SchemaReflection instead

[ GitHub ]

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

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

#initializeSchemaCache

[ GitHub ]

  
# File 'activerecord/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

Instance Method Details

#add(connection, table_name)

Add internal cache for table with table_name.

[ GitHub ]

  
# File 'activerecord/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

#cached?(table_name) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/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)

Clear out internal caches for the data source name.

[ GitHub ]

  
# File 'activerecord/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)

Get the columns for a table

[ GitHub ]

  
# File 'activerecord/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)

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

[ GitHub ]

  
# File 'activerecord/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.

[ GitHub ]

  
# File 'activerecord/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.

[ GitHub ]

  
# File 'activerecord/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

#dump_to(filename)

[ GitHub ]

  
# File 'activerecord/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

#indexes(connection, table_name)

[ GitHub ]

  
# File 'activerecord/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)

[ GitHub ]

  
# File 'activerecord/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

#primary_keys(connection, table_name)

[ GitHub ]

  
# File 'activerecord/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_version

[ GitHub ]

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

def schema_version
  @version
end

#size

[ GitHub ]

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

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

#version(connection)

[ GitHub ]

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

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