Skip to content

Commit e10019e

Browse files
author
Katrina Owen
committed
Use tempfile when writing schema cache
This avoids situations where the schema cache is read by another process when the file is only partially written, by first writing to a tempfile, and then renaming at the end.
1 parent 69d25a5 commit e10019e

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

activerecord/lib/active_record/connection_adapters/schema_cache.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "active_support/core_ext/file/atomic"
4+
35
module ActiveRecord
46
module ConnectionAdapters
57
class SchemaCache
@@ -141,7 +143,7 @@ def clear_data_source_cache!(name)
141143
def dump_to(filename)
142144
clear!
143145
connection.data_sources.each { |table| add(table) }
144-
open(filename, "wb") { |f|
146+
File.atomic_write(filename) { |f|
145147
if filename.end_with?(".dump")
146148
f.write(Marshal.dump(self))
147149
else

0 commit comments

Comments
 (0)