Active Record Connection Adapters Schema Cache

Methods

Class Public methods

new(connection)

📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 223
        def new(connection)
          BoundSchemaReflection.new(SchemaReflection.new(nil), connection)
        end
🔎 See on GitHub

new()

📝 Source code
# 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
🔎 See on GitHub

Instance Public methods

add(connection, table_name)

Add internal cache for table with table_name.

📝 Source code
# 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
🔎 See on GitHub

cached?(table_name)

📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 303
      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 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
🔎 See on GitHub

columns(connection, table_name)

Get the columns for a table

📝 Source code
# 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
🔎 See on GitHub

columns_hash(connection, 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(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
🔎 See on GitHub

columns_hash?(connection, 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?(connection, table_name)
        @columns_hash.key?(table_name)
      end
🔎 See on GitHub

data_source_exists?(connection, name)

A cached lookup for table existence.

📝 Source code
# 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
🔎 See on GitHub

dump_to(filename)

📝 Source code
# 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
🔎 See on GitHub

indexes(connection, table_name)

📝 Source code
# 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
🔎 See on GitHub

init_with(coder)

📝 Source code
# 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
🔎 See on GitHub

primary_keys(connection, table_name)

📝 Source code
# 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
🔎 See on GitHub

schema_version()

📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 381
      def schema_version
        @version
      end
🔎 See on GitHub

size()

📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 385
      def size
        [@columns, @columns_hash, @primary_keys, @data_sources].sum(&:size)
      end
🔎 See on GitHub

version(connection)

📝 Source code
# File activerecord/lib/active_record/connection_adapters/schema_cache.rb, line 377
      def version(connection)
        @version ||= connection.schema_version
      end
🔎 See on GitHub