Skip to content

Commit 845ba30

Browse files
committed
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.
1 parent 4d102eb commit 845ba30

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

activerecord/lib/active_record/migration.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -642,11 +642,7 @@ def check_pending!(connection = Base.connection)
642642
end
643643

644644
def load_schema_if_pending!
645-
needs_update = !db_configs_in_current_env.all? do |db_config|
646-
Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord.schema_format)
647-
end
648-
649-
if needs_update
645+
if any_schema_needs_update?
650646
# Roundtrip to Rake to allow plugins to hook into database initialization.
651647
root = defined?(ENGINE_ROOT) ? ENGINE_ROOT : Rails.root
652648
FileUtils.cd(root) do
@@ -690,6 +686,12 @@ def check_pending_migrations # :nodoc:
690686
end
691687

692688
private
689+
def any_schema_needs_update?
690+
!db_configs_in_current_env.all? do |db_config|
691+
Tasks::DatabaseTasks.schema_up_to_date?(db_config, ActiveRecord.schema_format)
692+
end
693+
end
694+
693695
def pending_migrations
694696
prev_db_config = Base.connection_db_config
695697
pending_migrations = []

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ def load_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :n
378378
end
379379

380380
def schema_up_to_date?(configuration, format = ActiveRecord.schema_format, file = nil)
381+
original_db_config = ActiveRecord::Base.connection_db_config
381382
db_config = resolve_configuration(configuration)
382383

383384
file ||= schema_dump_path(db_config)
@@ -391,6 +392,8 @@ def schema_up_to_date?(configuration, format = ActiveRecord.schema_format, file
391392
return false unless connection.internal_metadata.table_exists?
392393

393394
connection.internal_metadata[:schema_sha1] == schema_sha1(file)
395+
ensure
396+
ActiveRecord::Base.establish_connection(original_db_config)
394397
end
395398

396399
def reconstruct_from_schema(db_config, format = ActiveRecord.schema_format, file = nil) # :nodoc:

railties/test/application/rake/multi_dbs_test.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,49 @@ class TwoMigration < ActiveRecord::Migration::Current
12591259
end
12601260
end
12611261
end
1262+
1263+
test "after schema is loaded test run on the correct connections" do
1264+
require "#{app_path}/config/environment"
1265+
app_file "config/database.yml", <<-YAML
1266+
development:
1267+
primary:
1268+
database: db/default.sqlite3
1269+
adapter: sqlite3
1270+
animals:
1271+
database: db/development_animals.sqlite3
1272+
adapter: sqlite3
1273+
migrations_paths: db/animals_migrate
1274+
test:
1275+
primary:
1276+
database: db/default_test.sqlite3
1277+
adapter: sqlite3
1278+
animals:
1279+
database: db/test_animals.sqlite3
1280+
adapter: sqlite3
1281+
migrations_paths: db/animals_migrate
1282+
YAML
1283+
1284+
Dir.chdir(app_path) do
1285+
generate_models_for_animals
1286+
1287+
File.open("test/models/book_test.rb", "w") do |file|
1288+
file.write(<<~EOS)
1289+
require "test_helper"
1290+
1291+
class BookTest < ActiveSupport::TestCase
1292+
test "a book" do
1293+
assert Book.first
1294+
end
1295+
end
1296+
EOS
1297+
end
1298+
1299+
rails "db:migrate"
1300+
rails "db:schema:dump"
1301+
output = rails "test"
1302+
assert_match(/1 runs, 1 assertions, 0 failures, 0 errors, 0 skips/, output)
1303+
end
1304+
end
12621305
end
12631306
end
12641307
end

0 commit comments

Comments
 (0)