Skip to content

Commit 1557674

Browse files
committed
Lease connections and renamed assert_queries to assert_queries_count
See rails/rails#50373
1 parent bb8ae0a commit 1557674

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

lib/active_record/connection_adapters/sqlserver/core_ext/preloader.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ module SQLServer
88
module CoreExt
99
module LoaderQuery
1010
def load_records_for_keys(keys, &block)
11-
return super unless scope.connection.sqlserver?
11+
scope.with_connection do |connection|
12+
return super unless connection.sqlserver?
1213

13-
return [] if keys.empty?
14+
return [] if keys.empty?
1415

15-
if association_key_name.is_a?(Array)
16-
query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }
16+
if association_key_name.is_a?(Array)
17+
query_constraints = Hash.new { |hsh, key| hsh[key] = Set.new }
1718

18-
keys.each_with_object(query_constraints) do |values_set, constraints|
19-
association_key_name.zip(values_set).each do |key_name, value|
20-
constraints[key_name] << value
19+
keys.each_with_object(query_constraints) do |values_set, constraints|
20+
association_key_name.zip(values_set).each do |key_name, value|
21+
constraints[key_name] << value
22+
end
2123
end
22-
end
2324

24-
scope.where(query_constraints).load(&block)
25-
else
26-
keys.each_slice(in_clause_length).flat_map do |slice|
27-
scope.where(association_key_name => slice).load(&block).records
25+
scope.where(query_constraints).load(&block)
26+
else
27+
keys.each_slice(in_clause_length).flat_map do |slice|
28+
scope.where(association_key_name => slice).load(&block).records
29+
end
2830
end
2931
end
3032
end

test/cases/adapter_test_sqlserver.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ class AdapterTestSQLServer < ActiveRecord::TestCase
6161
end
6262

6363
it "test table existence across database schemas" do
64-
arunit_connection = Topic.connection
65-
arunit2_connection = College.connection
64+
arunit_connection = Topic.lease_connection
65+
arunit2_connection = College.lease_connection
6666

6767
arunit_database = arunit_connection.pool.db_config.database
6868
arunit2_database = arunit2_connection.pool.db_config.database

test/cases/coerced_tests.rb

+19-19
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def test_partial_index_coerced
5656

5757
t = Topic.create!(title: "abc")
5858
t.author_name = "John"
59-
assert_queries(1) do
59+
assert_queries_count(1) do
6060
t.valid?
6161
end
6262
end
@@ -234,7 +234,7 @@ def test_update_date_time_attributes_with_default_timezone_local
234234
coerce_tests! %r{an empty transaction does not raise if preventing writes}
235235
test "an empty transaction does not raise if preventing writes coerced" do
236236
ActiveRecord::Base.while_preventing_writes do
237-
assert_queries(1, ignore_none: true) do
237+
assert_queries_count(1, ignore_none: true) do
238238
Bird.transaction do
239239
ActiveRecord::Base.lease_connection.materialize_transactions
240240
end
@@ -490,8 +490,8 @@ def test_limit_with_offset_is_kept_coerced
490490
coerce_tests! :test_distinct_count_all_with_custom_select_and_order
491491
def test_distinct_count_all_with_custom_select_and_order_coerced
492492
accounts = Account.distinct.select("credit_limit % 10 AS the_limit").order(Arel.sql("credit_limit % 10"))
493-
assert_queries(1) { assert_equal 3, accounts.count(:all) }
494-
assert_queries(1) { assert_equal 3, accounts.load.size }
493+
assert_queries_count(1) { assert_equal 3, accounts.count(:all) }
494+
assert_queries_count(1) { assert_equal 3, accounts.load.size }
495495
end
496496

497497
# Leave it up to users to format selects/functions so HAVING works correctly.
@@ -1018,7 +1018,7 @@ def test_implicit_order_column_is_configurable_coerced
10181018
assert_equal topics(:fifth), Topic.first
10191019
assert_equal topics(:third), Topic.last
10201020

1021-
c = Topic.connection
1021+
c = Topic.lease_connection
10221022
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("topics.title"))} DESC, #{Regexp.escape(c.quote_table_name("topics.id"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
10231023
Topic.last
10241024
}
@@ -1032,7 +1032,7 @@ def test_implicit_order_set_to_primary_key_coerced
10321032
old_implicit_order_column = Topic.implicit_order_column
10331033
Topic.implicit_order_column = "id"
10341034

1035-
c = Topic.connection
1035+
c = Topic.lease_connection
10361036
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("topics.id"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
10371037
Topic.last
10381038
}
@@ -1046,7 +1046,7 @@ def test_implicit_order_for_model_without_primary_key_coerced
10461046
old_implicit_order_column = NonPrimaryKey.implicit_order_column
10471047
NonPrimaryKey.implicit_order_column = "created_at"
10481048

1049-
c = NonPrimaryKey.connection
1049+
c = NonPrimaryKey.lease_connection
10501050

10511051
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("non_primary_keys.created_at"))} DESC OFFSET 0 ROWS FETCH NEXT @0 ROWS ONLY.*@0 = 1/i) {
10521052
NonPrimaryKey.last
@@ -1067,7 +1067,7 @@ def test_member_on_unloaded_relation_with_composite_primary_key_coerced
10671067
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
10681068
coerce_tests! :test_implicit_order_column_prepends_query_constraints
10691069
def test_implicit_order_column_prepends_query_constraints_coerced
1070-
c = ClothingItem.connection
1070+
c = ClothingItem.lease_connection
10711071
ClothingItem.implicit_order_column = "description"
10721072
quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type"))
10731073
quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color"))
@@ -1083,7 +1083,7 @@ def test_implicit_order_column_prepends_query_constraints_coerced
10831083
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
10841084
coerce_tests! %r{#last for a model with composite query constraints}
10851085
test "#last for a model with composite query constraints coerced" do
1086-
c = ClothingItem.connection
1086+
c = ClothingItem.lease_connection
10871087
quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type"))
10881088
quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color"))
10891089

@@ -1095,7 +1095,7 @@ def test_implicit_order_column_prepends_query_constraints_coerced
10951095
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
10961096
coerce_tests! %r{#first for a model with composite query constraints}
10971097
test "#first for a model with composite query constraints coerced" do
1098-
c = ClothingItem.connection
1098+
c = ClothingItem.lease_connection
10991099
quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type"))
11001100
quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color"))
11011101

@@ -1107,7 +1107,7 @@ def test_implicit_order_column_prepends_query_constraints_coerced
11071107
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
11081108
coerce_tests! :test_implicit_order_column_reorders_query_constraints
11091109
def test_implicit_order_column_reorders_query_constraints_coerced
1110-
c = ClothingItem.connection
1110+
c = ClothingItem.lease_connection
11111111
ClothingItem.implicit_order_column = "color"
11121112
quoted_type = Regexp.escape(c.quote_table_name("clothing_items.clothing_type"))
11131113
quoted_color = Regexp.escape(c.quote_table_name("clothing_items.color"))
@@ -1131,7 +1131,7 @@ def test_include_on_unloaded_relation_with_composite_primary_key_coerced
11311131
# Check for `FETCH NEXT x ROWS` rather then `LIMIT`.
11321132
coerce_tests! :test_nth_to_last_with_order_uses_limit
11331133
def test_nth_to_last_with_order_uses_limit_coerced
1134-
c = Topic.connection
1134+
c = Topic.lease_connection
11351135
assert_sql(/ORDER BY #{Regexp.escape(c.quote_table_name("topics.id"))} DESC OFFSET @(\d) ROWS FETCH NEXT @(\d) ROWS ONLY.*@\1 = 1.*@\2 = 1/i) do
11361136
Topic.second_to_last
11371137
end
@@ -1324,7 +1324,7 @@ def test_create_without_primary_key_no_extra_query_coerced
13241324
self.table_name = "dashboards"
13251325
end
13261326
klass.create! # warmup schema cache
1327-
assert_queries(2, ignore_none: true) { klass.create! }
1327+
assert_queries_count(2, ignore_none: true) { klass.create! }
13281328
end
13291329
end
13301330

@@ -1354,7 +1354,7 @@ def test_query_cached_even_when_types_are_reset_coerced
13541354
Task.initialize_find_by_cache
13551355
Task.define_attribute_methods
13561356

1357-
assert_queries(1, ignore_none: true) do
1357+
assert_queries_count(1, ignore_none: true) do
13581358
Task.find(1)
13591359
end
13601360

@@ -1454,11 +1454,11 @@ def test_relations_dont_load_all_records_in_pretty_print_coerced
14541454
def test_empty_complex_chained_relations_coerced
14551455
posts = Post.select("comments_count").where("id is not null").group("author_id", "id").where("legacy_comments_count > 0")
14561456

1457-
assert_queries(1) { assert_equal false, posts.empty? }
1457+
assert_queries_count(1) { assert_equal false, posts.empty? }
14581458
assert_not_predicate posts, :loaded?
14591459

14601460
no_posts = posts.where(title: "")
1461-
assert_queries(1) { assert_equal true, no_posts.empty? }
1461+
assert_queries_count(1) { assert_equal true, no_posts.empty? }
14621462
assert_not_predicate no_posts, :loaded?
14631463
end
14641464

@@ -2338,7 +2338,7 @@ def test_preloads_has_many_on_model_with_a_composite_primary_key_through_id_attr
23382338
assert_equal 2, sql.size
23392339
preload_sql = sql.last
23402340

2341-
c = Cpk::OrderAgreement.connection
2341+
c = Cpk::OrderAgreement.lease_connection
23422342
order_id_column = Regexp.escape(c.quote_table_name("cpk_order_agreements.order_id"))
23432343
order_id_constraint = /#{order_id_column} = @0.*@0 = \d+$/
23442344
expectation = /SELECT.*WHERE.* #{order_id_constraint}/
@@ -2362,7 +2362,7 @@ def test_preloads_belongs_to_a_composite_primary_key_model_through_id_attribute_
23622362
assert_equal 2, sql.size
23632363
preload_sql = sql.last
23642364

2365-
c = Cpk::Order.connection
2365+
c = Cpk::Order.lease_connection
23662366
order_id = Regexp.escape(c.quote_table_name("cpk_orders.id"))
23672367
order_constraint = /#{order_id} = @0.*@0 = \d+$/
23682368
expectation = /SELECT.*WHERE.* #{order_constraint}/
@@ -2377,7 +2377,7 @@ class BasePreventWritesTest < ActiveRecord::TestCase
23772377
coerce_tests! %r{an empty transaction does not raise if preventing writes}
23782378
test "an empty transaction does not raise if preventing writes coerced" do
23792379
ActiveRecord::Base.while_preventing_writes do
2380-
assert_queries(1, ignore_none: true) do
2380+
assert_queries_count(1, ignore_none: true) do
23812381
Bird.transaction do
23822382
ActiveRecord::Base.lease_connection.materialize_transactions
23832383
end

test/cases/eager_load_too_many_ids_test_sqlserver.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def test_batch_preloading_too_many_ids
1111
# We Monkey patch Preloader to work with batches of 10_000 records.
1212
# Expect: N Books queries + Citation query
1313
expected_query_count = (Citation.count / in_clause_length.to_f).ceil + 1
14-
assert_queries(expected_query_count) do
14+
assert_queries_count(expected_query_count) do
1515
Citation.preload(:reference_of).to_a.size
1616
end
1717
end

0 commit comments

Comments
 (0)