class ActiveRecord::ConnectionAdapters::SchemaCache
Active Record Connection Adapters Schema Cache
Public class methods
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 215
def new(connection)
BoundSchemaReflection.new(SchemaReflection.new(nil), connection)
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 253
def initialize
@columns = {}
@columns_hash = {}
@primary_keys = {}
@data_sources = {}
@indexes = {}
@version = nil
end
Public instance methods
Add internal cache for table with table_name
.
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 314
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
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 292
def cached?(table_name)
@columns.key?(table_name)
end
Clear out internal caches for the data source name
.
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 375
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
Get the columns for a table
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 329
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
Get the columns for a table as a hash, key is the column name value is the column object.
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 341
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
Checks whether the columns hash is already cached for a table.
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 348
def columns_hash?(connection, table_name)
@columns_hash.key?(table_name)
end
A cached lookup for table existence.
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 305
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
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 391
def dump_to(filename)
open(filename) { |f|
if filename.include?(".dump")
f.write(Marshal.dump(self))
else
f.write(YAML.dump(self))
end
}
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 352
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
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 279
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"]
unless coder["deduplicated"]
derive_columns_hash_and_deduplicate_values
end
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 296
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
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 366
def schema_version
@version
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 370
def size
[@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size)
end
Source code GitHub
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 362
def version(connection)
@version ||= connection.schema_version
end