Skip to content

Commit 1c0b912

Browse files
authored
Merge pull request rails#52700 from ngan/fix-db-prepare-schema-dump
Dump schema only once after database prepare
2 parents 8c7754d + 175bf82 commit 1c0b912

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def create_current(environment = env, name = nil)
175175

176176
def prepare_all
177177
seed = false
178+
dump_db_configs = []
178179

179180
each_current_configuration(env) do |db_config|
180181
with_temporary_pool(db_config) do
@@ -197,15 +198,23 @@ def prepare_all
197198

198199
each_current_environment(env) do |environment|
199200
db_configs_with_versions(environment).sort.each do |version, db_configs|
201+
dump_db_configs |= db_configs
202+
200203
db_configs.each do |db_config|
201204
with_temporary_pool(db_config) do
202205
migrate(version)
203-
dump_schema(db_config) if ActiveRecord.dump_schema_after_migration
204206
end
205207
end
206208
end
207209
end
208210

211+
# Dump schema for databases that were migrated.
212+
if ActiveRecord.dump_schema_after_migration
213+
dump_db_configs.each do |db_config|
214+
dump_schema(db_config)
215+
end
216+
end
217+
209218
load_seed if seed
210219
end
211220

railties/test/application/rake/multi_dbs_test.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ class ThreeMigration < ActiveRecord::Migration::Current
556556
end
557557
MIGRATION
558558

559-
560559
Dir.chdir(app_path) do
561560
rails "db:migrate:up:primary", "VERSION=01_one_migration.rb"
562561
rails "db:migrate:up:primary", "VERSION=03_three_migration.rb"
@@ -595,6 +594,46 @@ class ThreeMigration < ActiveRecord::Migration::Current
595594
end
596595
end
597596

597+
test "db:prepare only dumps schema for migrated databases" do
598+
require "#{app_path}/config/environment"
599+
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
600+
class OneMigration < ActiveRecord::Migration::Current
601+
end
602+
MIGRATION
603+
604+
app_file "db/animals_migrate/02_two_migration.rb", <<-MIGRATION
605+
class TwoMigration < ActiveRecord::Migration::Current
606+
end
607+
MIGRATION
608+
609+
primary_mtime = nil
610+
animals_mtime = nil
611+
612+
Dir.chdir(app_path) do
613+
# Run the first two migrations to get the schema files.
614+
rails "db:prepare"
615+
616+
assert File.exist?("db/schema.rb")
617+
assert File.exist?("db/animals_schema.rb")
618+
619+
primary_mtime = File.mtime("db/schema.rb")
620+
animals_mtime = File.mtime("db/animals_schema.rb")
621+
end
622+
623+
app_file "db/animals_migrate/03_three_migration.rb", <<-MIGRATION
624+
class ThreeMigration < ActiveRecord::Migration::Current
625+
end
626+
MIGRATION
627+
628+
Dir.chdir(app_path) do
629+
# Run the new migration and assert that only the animals schema was updated.
630+
rails "db:prepare"
631+
632+
assert_equal primary_mtime, File.mtime("db/schema.rb")
633+
assert_not_equal animals_mtime, File.mtime("db/animals_schema.rb")
634+
end
635+
end
636+
598637
test "migrations in different directories can have the same timestamp" do
599638
require "#{app_path}/config/environment"
600639
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION

0 commit comments

Comments
 (0)