1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #37601 from Edouard-chin/ec-reset-connection-when-migrating

Reestablish connection to previous database after migrating:
This commit is contained in:
Eileen M. Uchitelle 2019-11-01 14:06:58 -04:00 committed by GitHub
commit cfd4bff376
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

@ -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

View file

@ -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"