Push db schema cache dump into schema cache class

上级 6138aa6a
......@@ -129,6 +129,12 @@ def clear_data_source_cache!(name)
@indexes.delete name
end
def dump_to(filename)
clear!
connection.data_sources.each { |table| add(table) }
open(filename, "wb") { |f| f.write(YAML.dump(self)) }
end
def marshal_dump
# if we get current version during initialization, it happens stack over flow.
@version = connection.migration_context.current_version
......
......@@ -449,9 +449,7 @@ def load_seed
# ==== Examples:
# ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(ActiveRecord::Base.connection, "tmp/schema_dump.yaml")
def dump_schema_cache(conn, filename)
conn.schema_cache.clear!
conn.data_sources.each { |table| conn.schema_cache.add(table) }
open(filename, "wb") { |f| f.write(YAML.dump(conn.schema_cache)) }
conn.schema_cache.dump_to(filename)
end
def clear_schema_cache(filename)
......
......@@ -16,13 +16,15 @@ def test_primary_key
end
def test_yaml_dump_and_load
@cache.columns("posts")
@cache.columns_hash("posts")
@cache.data_sources("posts")
@cache.primary_keys("posts")
@cache.indexes("posts")
# Create an empty cache.
cache = SchemaCache.new @connection
tempfile = Tempfile.new(["schema_cache-", ".yml"])
# Dump it. It should get populated before dumping.
cache.dump_to(tempfile.path)
new_cache = YAML.load(YAML.dump(@cache))
# Reload it.
new_cache = YAML.load(File.read(tempfile.path))
assert_no_queries do
assert_equal 12, new_cache.columns("posts").size
assert_equal 12, new_cache.columns_hash("posts").size
......@@ -31,6 +33,8 @@ def test_yaml_dump_and_load
assert_equal 1, new_cache.indexes("posts").size
assert_equal @database_version.to_s, new_cache.database_version.to_s
end
ensure
tempfile.unlink
end
def test_yaml_loads_5_1_dump
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册