mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Reestablish connection to previous database after migrating:
- The migrate task iterates and establish a connection over each db resulting in the last one to be used by subsequent rake tasks. We should reestablish a connection to the connection that was established before the migrate tasks was run - Fix #37578
This commit is contained in:
parent
0097b24bb2
commit
c9e44d1a06
2 changed files with 20 additions and 0 deletions
|
@ -80,11 +80,14 @@ db_namespace = namespace :db do
|
|||
|
||||
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
|
||||
task migrate: :load_config do
|
||||
original_config = ActiveRecord::Base.connection_config
|
||||
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
||||
ActiveRecord::Base.establish_connection(db_config)
|
||||
ActiveRecord::Tasks::DatabaseTasks.migrate
|
||||
end
|
||||
db_namespace["_dump"].invoke
|
||||
ensure
|
||||
ActiveRecord::Base.establish_connection(original_config)
|
||||
end
|
||||
|
||||
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
|
||||
|
|
|
@ -231,6 +231,23 @@ module ApplicationTests
|
|||
end
|
||||
end
|
||||
|
||||
test "db:migrate set back connection to its original state" do
|
||||
Dir.chdir(app_path) do
|
||||
dummy_task = <<~RUBY
|
||||
task foo: :environment do
|
||||
Book.first
|
||||
end
|
||||
RUBY
|
||||
app_file('Rakefile', dummy_task, 'a+')
|
||||
|
||||
generate_models_for_animals
|
||||
|
||||
assert_nothing_raised do
|
||||
rails("db:migrate", "foo")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "db:migrate and db:schema:dump and db:schema:load works on all databases" do
|
||||
require "#{app_path}/config/environment"
|
||||
db_migrate_and_schema_dump_and_load "schema"
|
||||
|
|
Loading…
Reference in a new issue