Fix schema_up_to_date connection

This method was failing to return the connection to the correct state
which would break tests when eager loading the application. What's
happening is that when we check `schema_up_to_date` when the application
is eager loaded all the models are eager loaded as well so it sees that
ApplicationRecord has a connection already but that connection is set to
the wrong one. It doesn't get replaced with the right one so we see
errors about looking for tables in the wrong database. This doesn't
happen when eager loading is off because ApplicationRecord isn't set and
gets initialized with the correct connection (from it's
`connects_to/establish_connection` call).

I also refactored this to pull `needs_update` into a method and make the
loop easier to read.
This commit is contained in:
eileencodes 2022-10-27 11:28:40 -04:00
parent 4d102ebbca
commit 845ba3013e
No known key found for this signature in database
GPG Key ID: BA5C575120BBE8DF
3 changed files with 53 additions and 5 deletions

View File

@ -642,11 +642,7 @@ module ActiveRecord
end
def load_schema_if_pending!
needs_update = !db_configs_in_current_env.all? do |db_config|
Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord.schema_format)
end
if needs_update
if any_schema_needs_update?
# Roundtrip to Rake to allow plugins to hook into database initialization.
root = defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
FileUtils.cd(root) do
@ -690,6 +686,12 @@ module ActiveRecord
end
private
def any_schema_needs_update?
!db_configs_in_current_env.all? do |db_config|
Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord.schema_format)
end
end
def pending_migrations
prev_db_config = Base.connection_db_config
pending_migrations = []

View File

@ -378,6 +378,7 @@ module ActiveRecord
end
def schema_up_to_date?(configuration, format = ActiveRecord.schema_format, file = nil)
original_db_config = ActiveRecord::Base.connection_db_config
db_config = resolve_configuration(configuration)
file ||= schema_dump_path(db_config)
@ -391,6 +392,8 @@ module ActiveRecord
return false unless connection.internal_metadata.table_exists?
connection.internal_metadata[:schema_sha1] == schema_sha1(file)
ensure
ActiveRecord::Base.establish_connection(original_db_config)
end
def reconstruct_from_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :nodoc:

View File

@ -1259,6 +1259,49 @@ module ApplicationTests
end
end
end
test "after schema is loaded test run on the correct connections" do
require "#{app_path}/config/environment"
app_file "config/database.yml", <<-YAML
development:
primary:
database: db/default.sqlite3
adapter: sqlite3
animals:
database: db/development_animals.sqlite3
adapter: sqlite3
migrations_paths: db/animals_migrate
test:
primary:
database: db/default_test.sqlite3
adapter: sqlite3
animals:
database: db/test_animals.sqlite3
adapter: sqlite3
migrations_paths: db/animals_migrate
YAML
Dir.chdir(app_path) do
generate_models_for_animals
File.open("test/models/book_test.rb", "w") do |file|
file.write(<<~EOS)
require "test_helper"
class BookTest < ActiveSupport::TestCase
test "a book" do
assert Book.first
end
end
EOS
end
rails "db:migrate"
rails "db:schema:dump"
output = rails "test"
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors, 0 skips/, output)
end
end
end
end
end