Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51083 from Shopify/activerecord-with-connection
Add `ActiveRecord::Base.with_connection` as a shortcut
- Loading branch information
Showing
6 changed files
with
98 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
|
||
module ActiveRecord | ||
class ConnectionHandlingTest < ActiveRecord::TestCase | ||
unless in_memory_db? | ||
test "#with_connection lease the connection for the duration of the block" do | ||
ActiveRecord::Base.connection_pool.release_connection | ||
assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
|
||
ActiveRecord::Base.with_connection do |connection| | ||
assert_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
assert_same connection, ActiveRecord::Base.connection | ||
end | ||
|
||
assert_not_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
end | ||
|
||
test "#with_connection use the already leased connection if available" do | ||
leased_connection = ActiveRecord::Base.connection | ||
assert_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
|
||
ActiveRecord::Base.with_connection do |connection| | ||
assert_same leased_connection, connection | ||
assert_same ActiveRecord::Base.connection, connection | ||
end | ||
|
||
assert_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
assert_same ActiveRecord::Base.connection, leased_connection | ||
end | ||
|
||
test "#with_connection is reentrant" do | ||
leased_connection = ActiveRecord::Base.connection | ||
assert_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
|
||
ActiveRecord::Base.with_connection do |connection| | ||
assert_same leased_connection, connection | ||
assert_same ActiveRecord::Base.connection, connection | ||
|
||
ActiveRecord::Base.with_connection do |connection2| | ||
assert_same leased_connection, connection | ||
assert_same ActiveRecord::Base.connection, connection | ||
end | ||
end | ||
|
||
assert_predicate ActiveRecord::Base.connection_pool, :active_connection? | ||
assert_same ActiveRecord::Base.connection, leased_connection | ||
end | ||
end | ||
end | ||
end |