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

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