1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix schema cache load and corresponding test

This test was incorrect. `primary` was winning for the schema cache load
but when you boot an application it's actually the first configuration
that wins (in a multi db app).

The test didn't catch this because I forgot to add a migrations_paths to
the configuration.

We updated the schema cache loader railtie as well because any
application that didn't have a `primary` config would not be able to use
the schema cache. Originally we thought we'd enforce a `primary`
configuration but no longer feel that's correct. It's simpler to say
that the first wins in a 3-tier rather than implementing a solution to
require `primary` and / or allow aliases.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
Co-authored-by: John Hawthorn <john@hawthorn.email>
This commit is contained in:
eileencodes 2020-06-04 14:50:48 -04:00
parent c42b355c39
commit e7b1e118b9
No known key found for this signature in database
GPG key ID: BA5C575120BBE8DF
2 changed files with 13 additions and 11 deletions

View file

@ -129,13 +129,11 @@ To keep using the current cache store, you can turn off cache versioning entirel
if config.active_record.delete(:use_schema_cache_dump)
config.after_initialize do |app|
ActiveSupport.on_load(:active_record) do
db_config = ActiveRecord::Base.configurations.configs_for(
env_name: Rails.env,
name: "primary",
)
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
"primary",
schema_cache_path: db_config&.schema_cache_path,
db_config.name,
schema_cache_path: db_config&.schema_cache_path
)
cache = ActiveRecord::ConnectionAdapters::SchemaCache.load_from(filename)

View file

@ -343,11 +343,14 @@ module ApplicationTests
db_migrate_and_status database_url_db_name
end
def db_schema_dump
def db_schema_dump(database: nil)
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
args = ["generate", "model", "book", "title:string"]
args << "--database=#{database}" if database
rails args
rails "db:migrate", "db:schema:dump"
schema_dump = File.read("db/schema.rb")
dump_name = database ? "#{database}_schema.rb" : "schema.rb"
schema_dump = File.read("db/#{dump_name}")
assert_match(/create_table \"books\"/, schema_dump)
end
end
@ -412,7 +415,7 @@ module ApplicationTests
ENV["SCHEMA_CACHE"] = @old_schema_cache_env
end
test "db:schema:cache:dump primary wins" do
test "db:schema:cache:dump first config wins" do
Dir.chdir(app_path) do
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
@ -426,6 +429,7 @@ module ApplicationTests
some_entry:
<<: *default
database: db/development_other.sqlite3
migrations_paths: db/some_entry_migrate
primary:
<<: *default
database: db/development.sqlite3
@ -433,7 +437,7 @@ module ApplicationTests
end
end
db_schema_dump
db_schema_dump(database: "some_entry")
db_schema_cache_dump
end