Active Record Connection Adapters Schema
Cache
Methods
- add
- cached?
- clear_data_source_cache!
- columns
- columns_hash
- columns_hash?
- data_source_exists?
- dump_to
- indexes
- primary_keys
- schema_version
- size
- version
Instance Public methods
add(pool, table_name)
Add internal cache for table with table_name
.
📝 Source code
# File activerecord/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
🔎 See on GitHub
cached?(table_name)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 294
def cached?(table_name)
@columns.key?(table_name)
end
🔎 See on GitHub
clear_data_source_cache!(_connection, name)
Clear out internal caches for the data source name
.
📝 Source code
# File activerecord/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
🔎 See on GitHub
columns(pool, table_name)
Get the columns for a table
📝 Source code
# File activerecord/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
🔎 See on GitHub
columns_hash(pool, table_name)
Get the columns for a table as a hash, key is the column name value is the column object.
📝 Source code
# File activerecord/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
🔎 See on GitHub
columns_hash?(_pool, table_name)
Checks whether the columns hash is already cached for a table.
📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 359
def columns_hash?(_pool, table_name)
@columns_hash.key?(table_name)
end
🔎 See on GitHub
data_source_exists?(pool, name)
A cached lookup for table existence.
📝 Source code
# File activerecord/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
🔎 See on GitHub
dump_to(filename)
📝 Source code
# File activerecord/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
🔎 See on GitHub
indexes(pool, table_name)
📝 Source code
# File activerecord/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
🔎 See on GitHub
primary_keys(pool, table_name)
📝 Source code
# File activerecord/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
🔎 See on GitHub
schema_version()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 379
def schema_version
@version
end
🔎 See on GitHub
size()
📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 383
def size
[@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size)
end
🔎 See on GitHub
version(pool)
📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 375
def version(pool)
@version ||= pool.with_connection(&:schema_version)
end
🔎 See on GitHub