Skip to content

Commit ef1ccde

Browse files
committed
Avoid purging the test database when loading a schema:
- Fix rails#50672 - Closes rails#50946 - ### Problem In adb64db, we added a change to purge the test database whenever the `load:schema:<name>` was run. This change meant that in order for the schema to load, 1) the test database configuration needs to be setup and 2) can be connected to. This creates some issues for users if either of both conditions aren't met. (For 1. it can be argued that it's a user error, but for 2., it's likely that a production environment can't connect to the test database). ### Details I dug to understand why loading a specific schema requires purging the test database (whereas loading all schema doesn't), but couldn't really see anything obvious, so I concluded that it may just be some overly defensive code. ### Solution Whenever we run a test, Rails maintains all test databases schema, and if a schema changes, the equivalent of a purge will be executed on all test databases. https://github.com/rails/rails/blob/b132744e22de64eb530d6ac407fa4b6b4a926143/railties/lib/rails/testing/maintain_test_schema.rb#L5 I couldn't really think of a negative behaviour difference between purging the test database eagerly when we load the schema and lazily when we run the tests, so I simply removed the `purge` rake task dependency on `schema:load:<name>`.
1 parent b132744 commit ef1ccde

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

activerecord/lib/active_record/railties/databases.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ db_namespace = namespace :db do
474474
namespace :load do
475475
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
476476
desc "Load a database schema file (either db/schema.rb or db/structure.sql, depending on configuration) into the #{name} database"
477-
task name => "db:test:purge:#{name}" do
477+
task name => [:load_config, :check_protected_environments] do
478478
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
479479
db_config = pool.db_config
480480
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, ENV["SCHEMA_FORMAT"] || db_config.schema_format)

railties/test/application/rake/multi_dbs_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,45 @@ def generate_models_for_animals
510510
end
511511
end
512512

513+
test "db:schema:load:name doesn't purge the test database. The test schema is maintained when running tests" do
514+
require "#{app_path}/config/environment"
515+
Dir.chdir(app_path) do
516+
generate_models_for_animals
517+
518+
File.open("test/models/dog_test.rb", "w") do |file|
519+
file.write(<<~EOS)
520+
require "test_helper"
521+
522+
class DogTest < ActiveSupport::TestCase
523+
test "Dog name type" do
524+
puts Dog.type_for_attribute(:name).type
525+
end
526+
end
527+
EOS
528+
end
529+
530+
rails("db:migrate:primary", "db:migrate:animals")
531+
532+
development_runner_output = rails("runner", "puts Dog.type_for_attribute(:name).type")
533+
assert_match(/string/, development_runner_output)
534+
535+
test_output = rails("test", "test/models/dog_test.rb")
536+
assert_match(/string/, test_output)
537+
538+
# Simulate a schema change
539+
content = File.read("db/animals_schema.rb")
540+
content.gsub!(/t\.string "name"/, "t.text \"name\"")
541+
File.write("db/animals_schema.rb", content)
542+
543+
rails("db:schema:load:animals")
544+
development_runner_output = rails("runner", "puts Dog.type_for_attribute(:name).type")
545+
assert_match(/text/, development_runner_output)
546+
547+
test_output = rails("test", "test/models/dog_test.rb")
548+
assert_match(/text/, test_output)
549+
end
550+
end
551+
513552
test "db:migrate respects timestamp ordering across databases" do
514553
require "#{app_path}/config/environment"
515554
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION

0 commit comments

Comments
 (0)