1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Support marshal as a schema cache serialization strategy

This commit is contained in:
Katrina Owen 2020-02-07 08:24:16 -07:00
parent b71c1cdddd
commit 9a356fcd42
No known key found for this signature in database
GPG key ID: A2C5A7CA5A4A075C
2 changed files with 47 additions and 15 deletions

View file

@ -6,7 +6,8 @@ module ActiveRecord
def self.load_from(filename)
return unless File.file?(filename)
YAML.load(File.read(filename))
file = File.read(filename)
filename.end_with?(".dump") ? Marshal.load(file) : YAML.load(file)
end
attr_reader :version
@ -138,7 +139,13 @@ module ActiveRecord
def dump_to(filename)
clear!
connection.data_sources.each { |table| add(table) }
open(filename, "wb") { |f| f.write(YAML.dump(self)) }
open(filename, "wb") { |f|
if filename.end_with?(".dump")
f.write(Marshal.dump(self))
else
f.write(YAML.dump(self))
end
}
end
def marshal_dump

View file

@ -118,25 +118,50 @@ module ActiveRecord
assert_nil @cache.instance_variable_get(:@database_version)
end
def test_dump_and_load
@cache.columns("posts")
@cache.columns_hash("posts")
@cache.data_sources("posts")
@cache.primary_keys("posts")
@cache.indexes("posts")
def test_marshal_dump_and_load
# Create an empty cache.
cache = SchemaCache.new @connection
@cache = Marshal.load(Marshal.dump(@cache))
# Populate it.
cache.add("posts")
# Create a new cache by marchal dumping / loading.
cache = Marshal.load(Marshal.dump(cache))
assert_no_queries do
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
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
end
def test_marshal_dump_and_load_via_disk
# Create an empty cache.
cache = SchemaCache.new @connection
tempfile = Tempfile.new(["schema_cache-", ".dump"])
# Dump it. It should get populated before dumping.
cache.dump_to(tempfile.path)
# Load a new cache.
cache = SchemaCache.load_from(tempfile.path)
cache.connection = @connection
assert_no_queries do
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_data_source_exist
assert @cache.data_source_exists?("posts")
assert_not @cache.data_source_exists?("foo")