class ActiveRecord::ConnectionAdapters::SchemaCache
Active Record Connection Adapters Schema
Cache¶ ↑
Public Class Methods
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 223 def new(connection) BoundSchemaReflection.new(SchemaReflection.new(nil), connection) end
# 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
Public Instance Methods
Add internal cache for table with table_name
.
# 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
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 303 def cached?(table_name) @columns.key?(table_name) end
Clear out internal caches for the data source name
.
# 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
Get the columns for a table
# 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
Get the columns for a table as a hash, key is the column name value is the column object.
# 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
Checks whether the columns hash is already cached for a table.
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 359 def columns_hash?(connection, table_name) @columns_hash.key?(table_name) end
A cached lookup for table existence.
# 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
# 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
# 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
# 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
# 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
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 381 def schema_version @version end
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 385 def size [@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size) end
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 377 def version(connection) @version ||= connection.schema_version end