提交 5cbba30a 编写于 作者: K kennyj

Support judgement expired schema cache dump.

上级 0da12df2
module ActiveRecord
module ConnectionAdapters
class SchemaCache
attr_reader :columns, :columns_hash, :primary_keys, :tables
attr_reader :columns, :columns_hash, :primary_keys, :tables, :version
attr_accessor :connection
def initialize(conn)
......@@ -36,6 +36,7 @@ def clear!
@columns_hash.clear
@primary_keys.clear
@tables.clear
@version = nil
end
# Clear out internal caches for table with +table_name+.
......@@ -47,13 +48,15 @@ def clear_table_cache!(table_name)
end
def marshal_dump
[:@columns, :@columns_hash, :@primary_keys, :@tables].map do |val|
# if we get current version during initialization, it happens stack over flow.
@version = ActiveRecord::Migrator.current_version
[@version] + [:@columns, :@columns_hash, :@primary_keys, :@tables].map do |val|
self.instance_variable_get(val).inject({}) { |h, v| h[v[0]] = v[1]; h }
end
end
def marshal_load(array)
@columns, @columns_hash, @primary_keys, @tables = array
@version, @columns, @columns_hash, @primary_keys, @tables = array
prepare_default_proc
end
......
......@@ -121,7 +121,11 @@ class Railtie < Rails::Railtie
filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
if File.file?(filename)
cache = Marshal.load(open(filename, 'rb') { |f| f.read })
ActiveRecord::Base.connection.schema_cache = cache
if cache.version == ActiveRecord::Migrator.current_version
ActiveRecord::Base.connection.schema_cache = cache
else
warn "schema_cache.dump is expired. Current version is #{ActiveRecord::Migrator.current_version}, but cache version is #{cache.version}."
end
end
end
end
......
......@@ -205,5 +205,19 @@ def from_bar_helper
assert ActiveRecord::Base.connection.schema_cache.tables["posts"]
end
test "expire schema cache dump" do
Dir.chdir(app_path) do
`rails generate model post title:string`
`bundle exec rake db:migrate`
`bundle exec rake db:schema:cache:dump`
`bundle exec rake db:rollback`
end
silence_warnings {
require "#{app_path}/config/environment"
assert !ActiveRecord::Base.connection.schema_cache.tables["posts"]
}
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册