Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Load schema caches for all connections #34449

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 19 additions & 13 deletions activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,25 @@ class Railtie < Rails::Railtie # :nodoc:
if config.active_record.delete(:use_schema_cache_dump)
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
filename = File.join(app.config.paths["db"].first, "schema_cache.yml")

if File.file?(filename)
current_version = ActiveRecord::Migrator.current_version

next if current_version.nil?

cache = YAML.load(File.read(filename))
if cache.version == current_version
connection.schema_cache = cache
connection_pool.schema_cache = cache.dup
else
warn "Ignoring db/schema_cache.yml because it has expired. The current schema version is #{current_version}, but the one in the cache is #{cache.version}."
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
ActiveRecord::Base.connected_to(database: db_config.spec_name.to_sym) do
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.spec_name)
if File.file?(filename)
current_version = ActiveRecord::Migrator.current_version
next if current_version.nil?

cache = YAML.load(File.read(filename))
if cache.version == current_version
connection.schema_cache = cache
connection_pool.schema_cache = cache.dup
else
warn <<~MSG.squish
Ignoring #{File.basename(filename)} because it has expired.
The current schema version is #{current_version},
but the one in the cache is #{cache.version}.
MSG
end
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions railties/test/application/rake/multi_dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,21 @@ def generate_models_for_animals
require "#{app_path}/config/environment"
db_migrate_and_schema_cache_dump_and_schema_cache_clear
end

test "schema cache initializer works on all databases" do
require "#{app_path}/config/environment"
db_migrate_and_schema_cache_dump
cache_sizes = rails("runner", <<~RUBY)
configs = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env)
cache_sizes = configs.map do |db_config|
ActiveRecord::Base.connected_to({ database: db_config.spec_name.to_sym }) do
ActiveRecord::Base.connection.schema_cache.size
end
end
p cache_sizes
RUBY
assert_equal [12, 12], JSON.parse(cache_sizes)
end
end
end
end