Skip to content

Commit 11edd88

Browse files
authored
Merge pull request rails#54853 from Edouard-chin/ec-avoid-purge
Avoid purging the test database when loading a schema:
2 parents 39da021 + ef1ccde commit 11edd88

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
@@ -486,7 +486,7 @@ db_namespace = namespace :db do
486486
namespace :load do
487487
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
488488
desc "Load a database schema file (either db/schema.rb or db/structure.sql, depending on configuration) into the #{name} database"
489-
task name => "db:test:purge:#{name}" do
489+
task name => [:load_config, :check_protected_environments] do
490490
ActiveRecord::Tasks::DatabaseTasks.with_temporary_pool_for_each(name: name) do |pool|
491491
db_config = pool.db_config
492492
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)