class ActiveRecord::ConnectionAdapters::SchemaReflection

def add(connection, name)

def add(connection, name)
  cache(connection).add(connection, name)
end

def cache(connection)

def cache(connection)
  @cache ||= load_cache(connection) || empty_cache
end

def cached?(table_name)

def cached?(table_name)
  if @cache.nil?
    # If `check_schema_cache_dump_version` is enabled we can't load
    # the schema cache dump without connecting to the database.
    unless self.class.check_schema_cache_dump_version
      @cache = load_cache(nil)
    end
  end
  @cache&.cached?(table_name)
end

def clear!

def clear!
  @cache = empty_cache
  nil
end

def clear_data_source_cache!(connection, name)

def clear_data_source_cache!(connection, name)
  return if @cache.nil? && !possible_cache_available?
  cache(connection).clear_data_source_cache!(connection, name)
end

def columns(connection, table_name)

def columns(connection, table_name)
  cache(connection).columns(connection, table_name)
end

def columns_hash(connection, table_name)

def columns_hash(connection, table_name)
  cache(connection).columns_hash(connection, table_name)
end

def columns_hash?(connection, table_name)

def columns_hash?(connection, table_name)
  cache(connection).columns_hash?(connection, table_name)
end

def data_source_exists?(connection, name)

def data_source_exists?(connection, name)
  cache(connection).data_source_exists?(connection, name)
end

def data_sources(connection, name)

def data_sources(connection, name)
  cache(connection).data_sources(connection, name)
end

def database_version(connection) # :nodoc:

:nodoc:
def database_version(connection) # :nodoc:
  cache(connection).database_version(connection)
end

def dump_to(connection, filename)

def dump_to(connection, filename)
  fresh_cache = empty_cache
  fresh_cache.add_all(connection)
  fresh_cache.dump_to(filename)
  @cache = fresh_cache
end

def empty_cache

def empty_cache
  new_cache = SchemaCache.allocate
  new_cache.send(:initialize)
  new_cache
end

def indexes(connection, table_name)

def indexes(connection, table_name)
  cache(connection).indexes(connection, table_name)
end

def initialize(cache_path, cache = nil)

def initialize(cache_path, cache = nil)
  @cache = cache
  @cache_path = cache_path
end

def load!(connection)

def load!(connection)
  cache(connection)
  self
end

def load_cache(connection)

def load_cache(connection)
  # Can't load if schema dumps are disabled
  return unless possible_cache_available?
  # Check we can find one
  return unless new_cache = SchemaCache._load_from(@cache_path)
  if self.class.check_schema_cache_dump_version
    begin
      current_version = connection.schema_version
      if new_cache.version(connection) != current_version
        warn "Ignoring #{@cache_path} because it has expired. The current schema version is #{current_version}, but the one in the schema cache file is #{new_cache.schema_version}."
        return
      end
    rescue ActiveRecordError => error
      warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
      return
    end
  end
  new_cache
end

def possible_cache_available?

def possible_cache_available?
  self.class.use_schema_cache_dump &&
    @cache_path &&
    File.file?(@cache_path)
end

def primary_keys(connection, table_name)

def primary_keys(connection, table_name)
  cache(connection).primary_keys(connection, table_name)
end

def set_schema_cache(cache)

def set_schema_cache(cache)
  @cache = cache
end

def size(connection)

def size(connection)
  cache(connection).size
end

def version(connection)

def version(connection)
  cache(connection).version(connection)
end