mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Push schema cache loading into schema cache class
This commit is contained in:
parent
0a51442722
commit
b71c1cdddd
3 changed files with 42 additions and 38 deletions
|
@ -3,6 +3,12 @@
|
|||
module ActiveRecord
|
||||
module ConnectionAdapters
|
||||
class SchemaCache
|
||||
def self.load_from(filename)
|
||||
return unless File.file?(filename)
|
||||
|
||||
YAML.load(File.read(filename))
|
||||
end
|
||||
|
||||
attr_reader :version
|
||||
attr_accessor :connection
|
||||
|
||||
|
|
|
@ -132,24 +132,23 @@ To keep using the current cache store, you can turn off cache versioning entirel
|
|||
env_name: Rails.env,
|
||||
spec_name: "primary",
|
||||
)
|
||||
|
||||
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
|
||||
"primary",
|
||||
schema_cache_path: db_config&.schema_cache_path,
|
||||
)
|
||||
|
||||
if File.file?(filename)
|
||||
current_version = ActiveRecord::Migrator.current_version
|
||||
cache = ActiveRecord::ConnectionAdapters::SchemaCache.load_from(filename)
|
||||
next if cache.nil?
|
||||
|
||||
next if current_version.nil?
|
||||
current_version = ActiveRecord::Migrator.current_version
|
||||
next if current_version.nil?
|
||||
|
||||
cache = YAML.load(File.read(filename))
|
||||
if cache.version == current_version
|
||||
connection_pool.schema_cache = cache.dup
|
||||
else
|
||||
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}."
|
||||
end
|
||||
if cache.version != current_version
|
||||
warn "Ignoring #{filename} because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}."
|
||||
next
|
||||
end
|
||||
|
||||
connection_pool.set_schema_cache(cache.dup)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,56 +23,55 @@ module ActiveRecord
|
|||
# Dump it. It should get populated before dumping.
|
||||
cache.dump_to(tempfile.path)
|
||||
|
||||
# Reload it.
|
||||
new_cache = YAML.load(File.read(tempfile.path))
|
||||
# Load the cache.
|
||||
cache = SchemaCache.load_from(tempfile.path)
|
||||
|
||||
# Give it a connection. Usually the connection
|
||||
# would get set on the cache when it's retrieved
|
||||
# from the pool.
|
||||
cache.connection = @connection
|
||||
|
||||
assert_no_queries do
|
||||
assert_equal 12, new_cache.columns("posts").size
|
||||
assert_equal 12, new_cache.columns_hash("posts").size
|
||||
assert new_cache.data_sources("posts")
|
||||
assert_equal "id", new_cache.primary_keys("posts")
|
||||
assert_equal 1, new_cache.indexes("posts").size
|
||||
assert_equal @database_version.to_s, new_cache.database_version.to_s
|
||||
assert_equal 12, cache.columns("posts").size
|
||||
assert_equal 12, cache.columns_hash("posts").size
|
||||
assert cache.data_sources("posts")
|
||||
assert_equal "id", cache.primary_keys("posts")
|
||||
assert_equal 1, cache.indexes("posts").size
|
||||
assert_equal @database_version.to_s, cache.database_version.to_s
|
||||
end
|
||||
ensure
|
||||
tempfile.unlink
|
||||
end
|
||||
|
||||
def test_yaml_loads_5_1_dump
|
||||
@cache = YAML.load(File.read(schema_dump_path))
|
||||
cache = SchemaCache.load_from(schema_dump_path)
|
||||
cache.connection = @connection
|
||||
|
||||
assert_no_queries do
|
||||
assert_equal 11, @cache.columns("posts").size
|
||||
assert_equal 11, @cache.columns_hash("posts").size
|
||||
assert @cache.data_sources("posts")
|
||||
assert_equal "id", @cache.primary_keys("posts")
|
||||
assert_equal 11, cache.columns("posts").size
|
||||
assert_equal 11, cache.columns_hash("posts").size
|
||||
assert cache.data_sources("posts")
|
||||
assert_equal "id", cache.primary_keys("posts")
|
||||
end
|
||||
end
|
||||
|
||||
def test_yaml_loads_5_1_dump_without_indexes_still_queries_for_indexes
|
||||
@cache = YAML.load(File.read(schema_dump_path))
|
||||
|
||||
# Simulate assignment in railtie after loading the cache.
|
||||
old_cache, @connection.schema_cache = @connection.schema_cache, @cache
|
||||
cache = SchemaCache.load_from(schema_dump_path)
|
||||
cache.connection = @connection
|
||||
|
||||
assert_queries :any, ignore_none: true do
|
||||
assert_equal 1, @cache.indexes("posts").size
|
||||
assert_equal 1, cache.indexes("posts").size
|
||||
end
|
||||
ensure
|
||||
@connection.schema_cache = old_cache
|
||||
end
|
||||
|
||||
def test_yaml_loads_5_1_dump_without_database_version_still_queries_for_database_version
|
||||
@cache = YAML.load(File.read(schema_dump_path))
|
||||
|
||||
# Simulate assignment in railtie after loading the cache.
|
||||
old_cache, @connection.schema_cache = @connection.schema_cache, @cache
|
||||
cache = SchemaCache.load_from(schema_dump_path)
|
||||
cache.connection = @connection
|
||||
|
||||
# We can't verify queries get executed because the database version gets
|
||||
# cached in both MySQL and PostgreSQL outside of the schema cache.
|
||||
assert_nil @cache.instance_variable_get(:@database_version)
|
||||
assert_equal @database_version.to_s, @cache.database_version.to_s
|
||||
ensure
|
||||
@connection.schema_cache = old_cache
|
||||
assert_nil cache.instance_variable_get(:@database_version)
|
||||
assert_equal @database_version.to_s, cache.database_version.to_s
|
||||
end
|
||||
|
||||
def test_primary_key_for_non_existent_table
|
||||
|
|
Loading…
Reference in a new issue