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

Reset the ActiveRecord::Base connection after rails db:migrate:name

This commit is contained in:
Kyle Thompson 2020-02-27 13:10:54 -05:00
parent 3269e4e7b0
commit 0366cbd742
No known key found for this signature in database
GPG key ID: 8ACC76081E6BCF84
3 changed files with 28 additions and 0 deletions

View file

@ -1,3 +1,9 @@
* Reset the `ActiveRecord::Base` connection after `rails db:migrate:name`
When `rails db:migrate` has finished, it ensures the `ActiveRecord::Base` connection is reset to its original configuration. Going forward, `rails db:migrate:name` will have the same behavior.
*Kyle Thompson*
* Disallow calling `connected_to` on subclasses of `ActiveRecord::Base`.
Behavior has not changed here but the previous API could be misleading to people who thought it would switch connections for only that class. `connected_to` switches the context from which we are getting connections, not the connections themselves.

View file

@ -110,9 +110,12 @@ db_namespace = namespace :db do
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
desc "Migrate #{name} database for current environment"
task name => :load_config do
original_db_config = ActiveRecord::Base.connection_db_config
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: name)
ActiveRecord::Base.establish_connection(db_config)
ActiveRecord::Tasks::DatabaseTasks.migrate
ensure
ActiveRecord::Base.establish_connection(original_db_config)
end
end

View file

@ -325,6 +325,25 @@ module ApplicationTests
end
end
test "db:migrate:name sets the connection back 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
rails("db:migrate:primary")
assert_nothing_raised do
rails("db:migrate:animals", "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"