未验证 提交 e7b1e118 编写于 作者: E eileencodes

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: NJohn Crepezzi <john.crepezzi@gmail.com>
Co-authored-by: NJohn Hawthorn <john@hawthorn.email>
上级 c42b355c
......@@ -129,13 +129,11 @@ 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
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)
......
......@@ -343,11 +343,14 @@ def db_migrate_and_status(expected_database)
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 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
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 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
some_entry:
<<: *default
database: db/development_other.sqlite3
migrations_paths: db/some_entry_migrate
primary:
<<: *default
database: db/development.sqlite3
......@@ -433,7 +437,7 @@ def db_schema_cache_dump(filename = "db/schema_cache.yml")
end
end
db_schema_dump
db_schema_dump(database: "some_entry")
db_schema_cache_dump
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册