提交 c9e44d1a 编写于 作者: E Edouard CHIN

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
上级 0097b24b
......@@ -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 @@ def generate_models_for_animals
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"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册