Skip to content

Commit 4c00c6e

Browse files
committed
Use YAML to serialize schema cache
1 parent 6d6249b commit 4c00c6e

File tree

7 files changed

+435
-17
lines changed

7 files changed

+435
-17
lines changed

activerecord/lib/active_record/connection_adapters/column.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ def human_name
4040
Base.human_attribute_name(@name)
4141
end
4242

43+
def init_with(coder)
44+
@name = coder["name"]
45+
@table_name = coder["table_name"]
46+
@sql_type_metadata = coder["sql_type_metadata"]
47+
@null = coder["null"]
48+
@default = coder["default"]
49+
@default_function = coder["default_function"]
50+
@collation = coder["collation"]
51+
@comment = coder["comment"]
52+
end
53+
54+
def encode_with(coder)
55+
coder["name"] = @name
56+
coder["table_name"] = @table_name
57+
coder["sql_type_metadata"] = @sql_type_metadata
58+
coder["null"] = @null
59+
coder["default"] = @default
60+
coder["default_function"] = @default_function
61+
coder["collation"] = @collation
62+
coder["comment"] = @comment
63+
end
64+
4365
def ==(other)
4466
other.is_a?(Column) &&
4567
attributes_for_hash == other.attributes_for_hash

activerecord/lib/active_record/connection_adapters/schema_cache.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ def initialize_dup(other)
2121
@data_sources = @data_sources.dup
2222
end
2323

24+
def encode_with(coder)
25+
coder["columns"] = @columns
26+
coder["columns_hash"] = @columns_hash
27+
coder["primary_keys"] = @primary_keys
28+
coder["data_sources"] = @data_sources
29+
coder["version"] = ActiveRecord::Migrator.current_version
30+
end
31+
32+
def init_with(coder)
33+
@columns = coder["columns"]
34+
@columns_hash = coder["columns_hash"]
35+
@primary_keys = coder["primary_keys"]
36+
@data_sources = coder["data_sources"]
37+
@version = coder["version"]
38+
end
39+
2440
def primary_keys(table_name)
2541
@primary_keys[table_name] ||= data_source_exists?(table_name) ? connection.primary_key(table_name) : nil
2642
end

activerecord/lib/active_record/railtie.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ class Railtie < Rails::Railtie # :nodoc:
8282
if config.active_record.delete(:use_schema_cache_dump)
8383
config.after_initialize do |app|
8484
ActiveSupport.on_load(:active_record) do
85-
filename = File.join(app.config.paths["db"].first, "schema_cache.dump")
85+
filename = File.join(app.config.paths["db"].first, "schema_cache.yml")
8686

8787
if File.file?(filename)
88-
cache = Marshal.load File.binread filename
88+
cache = YAML.load(File.read(filename))
8989
if cache.version == ActiveRecord::Migrator.current_version
9090
self.connection.schema_cache = cache
9191
self.connection_pool.schema_cache = cache.dup
9292
else
93-
warn "Ignoring db/schema_cache.dump because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
93+
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{ActiveRecord::Migrator.current_version}, but the one in the cache is #{cache.version}."
9494
end
9595
end
9696
end

activerecord/lib/active_record/railties/databases.rake

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,19 @@ db_namespace = namespace :db do
265265
end
266266

267267
namespace :cache do
268-
desc "Creates a db/schema_cache.dump file."
268+
desc "Creates a db/schema_cache.yml file."
269269
task dump: [:environment, :load_config] do
270-
con = ActiveRecord::Base.connection
271-
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.dump")
270+
conn = ActiveRecord::Base.connection
271+
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
272272

273-
con.schema_cache.clear!
274-
con.data_sources.each { |table| con.schema_cache.add(table) }
275-
open(filename, "wb") { |f| f.write(Marshal.dump(con.schema_cache)) }
273+
conn.schema_cache.clear!
274+
conn.data_sources.each { |table| conn.schema_cache.add(table) }
275+
open(filename, "wb") { |f| f.write(YAML.dump(conn.schema_cache)) }
276276
end
277277

278-
desc "Clears a db/schema_cache.dump file."
278+
desc "Clears a db/schema_cache.yml file."
279279
task clear: [:environment, :load_config] do
280-
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.dump")
280+
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
281281
rm_f filename, verbose: false
282282
end
283283
end

0 commit comments

Comments
 (0)