Skip to content

Commit 56b7357

Browse files
authored
Merge pull request rails#49793 from fatkodima/multiple-databases-rake-dump-specific
Dump schema only for a specific db for rollback/up/down tasks for multiple dbs
2 parents 2b09327 + 6aed3f7 commit 56b7357

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

activerecord/lib/active_record/railties/databases.rake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ db_namespace = namespace :db do
204204
conn.migration_context.run(:up, ActiveRecord::Tasks::DatabaseTasks.target_version)
205205
end
206206

207-
db_namespace["_dump"].invoke
207+
db_namespace["_dump:#{name}"].invoke
208208
end
209209
end
210210
end
@@ -235,7 +235,7 @@ db_namespace = namespace :db do
235235
conn.migration_context.run(:down, ActiveRecord::Tasks::DatabaseTasks.target_version)
236236
end
237237

238-
db_namespace["_dump"].invoke
238+
db_namespace["_dump:#{name}"].invoke
239239
end
240240
end
241241
end
@@ -269,7 +269,7 @@ db_namespace = namespace :db do
269269
conn.migration_context.rollback(step)
270270
end
271271

272-
db_namespace["_dump"].invoke
272+
db_namespace["_dump:#{name}"].invoke
273273
end
274274
end
275275
end

railties/test/application/rake/multi_dbs_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,28 @@ class TwoMigration < ActiveRecord::Migration::Current
692692
db_up_and_down "02", "animals"
693693
end
694694

695+
test "db:migrate:down:namespace and db:migrate:up:namespace dumps schema only for specific database" do
696+
require "#{app_path}/config/environment"
697+
698+
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
699+
class OneMigration < ActiveRecord::Migration::Current
700+
end
701+
MIGRATION
702+
703+
app_file "db/animals_migrate/02_two_migration.rb", <<-MIGRATION
704+
class TwoMigration < ActiveRecord::Migration::Current
705+
end
706+
MIGRATION
707+
708+
Dir.chdir(app_path) do
709+
rails("db:migrate:up:primary", "VERSION=01")
710+
rails("db:migrate:down:primary", "VERSION=01")
711+
712+
assert File.exist?("db/schema.rb"), "should dump schema for primary database"
713+
assert_not File.exist?("db/animals_schema.rb"), "should not dump schema for animals database"
714+
end
715+
end
716+
695717
test "db:migrate:redo raises in a multi-db application" do
696718
require "#{app_path}/config/environment"
697719
db_migrate_redo
@@ -742,6 +764,20 @@ class TwoMigration < ActiveRecord::Migration::Current
742764
db_migrate_and_rollback "animals"
743765
end
744766

767+
test "db:rollback:namespace dumps schema only for specific database" do
768+
Dir.chdir(app_path) do
769+
rails "generate", "model", "book", "title:string"
770+
rails "generate", "model", "dog", "name:string", "--database animals"
771+
rails "db:migrate"
772+
File.delete("db/animals_schema.rb")
773+
774+
rails "db:rollback:primary"
775+
776+
assert File.exist?("db/schema.rb"), "should dump schema for primary database"
777+
assert_not File.exist?("db/animals_schema.rb"), "should not dump schema for animals database"
778+
end
779+
end
780+
745781
test "db:migrate:status works on all databases" do
746782
require "#{app_path}/config/environment"
747783
db_migrate_and_migrate_status

0 commit comments

Comments
 (0)