diff --git a/activerecord/lib/active_record/connection_adapters/schema_cache.rb b/activerecord/lib/active_record/connection_adapters/schema_cache.rb index 46f8552214..7c44baf552 100644 --- a/activerecord/lib/active_record/connection_adapters/schema_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/schema_cache.rb @@ -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 diff --git a/activerecord/test/cases/connection_adapters/schema_cache_test.rb b/activerecord/test/cases/connection_adapters/schema_cache_test.rb index afc333675b..ccd869ba62 100644 --- a/activerecord/test/cases/connection_adapters/schema_cache_test.rb +++ b/activerecord/test/cases/connection_adapters/schema_cache_test.rb @@ -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")