123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::ConnectionAdapters::SchemaReflection

Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/connection_adapters/schema_cache.rb

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(cache_path, cache = nil) ⇒ SchemaReflection

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 16

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

Class Attribute Details

.check_schema_cache_dump_version (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 10

attr_accessor :check_schema_cache_dump_version

.use_schema_cache_dump (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 9

attr_accessor :use_schema_cache_dump

Instance Attribute Details

#possible_cache_available?Boolean (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 110

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

Instance Method Details

#add(pool, name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 41

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

#cache(pool) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 106

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

#cached?(table_name) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 79

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

#clear!

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 21

def clear!
  @cache = empty_cache

  nil
end

#clear_data_source_cache!(pool, name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 73

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

  cache(pool).clear_data_source_cache!(pool, name)
end

#columns(pool, table_name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 49

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

#columns_hash(pool, table_name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 53

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

#columns_hash?(pool, table_name) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 57

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

#data_source_exists?(pool, name) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 37

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

#data_sources(pool, name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 45

def data_sources(pool, name)
  cache(pool).data_source_exists?(pool, name)
end

#dump_to(pool, filename)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 91

def dump_to(pool, filename)
  fresh_cache = empty_cache
  fresh_cache.add_all(pool)
  fresh_cache.dump_to(filename)

  @cache = fresh_cache
end

#empty_cache (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 100

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

#indexes(pool, table_name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 61

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

#load!(pool)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 27

def load!(pool)
  cache(pool)

  self
end

#load_cache(pool) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 116

def load_cache(pool)
  # 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
      pool.with_connection do |connection|
        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
      end
    rescue ActiveRecordError => error
      warn "Failed to validate the schema cache because of #{error.class}: #{error.message}"
      return
    end
  end

  new_cache
end

#primary_keys(pool, table_name)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 33

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

#size(pool)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 69

def size(pool)
  cache(pool).size
end

#version(pool)

[ GitHub ]

  
# File 'activerecord/lib/active_record/connection_adapters/schema_cache.rb', line 65

def version(pool)
  cache(pool).version(pool)
end