Skip to content

Commit dea7899

Browse files
authored
Eliminate uses of lease_connection (#1157)
1 parent 9e2117b commit dea7899

File tree

8 files changed

+56
-44
lines changed

8 files changed

+56
-44
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ module AttributeMethods
1010
private
1111

1212
def attributes_for_update(attribute_names)
13-
return super unless self.class.lease_connection.adapter_name == "SQLServer"
13+
self.class.with_connection do |connection|
14+
return super(attribute_names) unless connection.sqlserver?
1415

15-
super.reject do |name|
16-
column = self.class.columns_hash[name]
17-
column && column.respond_to?(:is_identity?) && column.is_identity?
16+
super(attribute_names).reject do |name|
17+
column = self.class.columns_hash[name]
18+
column && column.respond_to?(:is_identity?) && column.is_identity?
19+
end
1820
end
1921
end
2022
end

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ module SQLServer
99
module CoreExt
1010
module Calculations
1111
def calculate(operation, column_name)
12-
if klass.lease_connection.sqlserver?
13-
_calculate(operation, column_name)
14-
else
15-
super
12+
klass.with_connection do |connection|
13+
if connection.sqlserver?
14+
_calculate(operation, column_name)
15+
else
16+
super
17+
end
1618
end
1719
end
1820

@@ -54,9 +56,10 @@ def _calculate(operation, column_name)
5456
end
5557

5658
def build_count_subquery(relation, column_name, distinct)
57-
return super unless klass.lease_connection.adapter_name == "SQLServer"
58-
59-
super(relation.unscope(:order), column_name, distinct)
59+
klass.with_connection do |connection|
60+
relation = relation.unscope(:order) if connection.sqlserver?
61+
super(relation, column_name, distinct)
62+
end
6063
end
6164
end
6265
end

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ module Explain
99
SQLSERVER_STATEMENT_REGEXP = /N'(.+)', N'(.+)', (.+)/
1010

1111
def exec_explain(queries, options = [])
12-
return super unless lease_connection.adapter_name == "SQLServer"
12+
with_connection do |connection|
13+
return super(queries, options) unless connection.sqlserver?
1314

14-
unprepared_queries = queries.map do |(sql, binds)|
15-
[unprepare_sqlserver_statement(sql, binds), binds]
15+
unprepared_queries = queries.map do |(sql, binds)|
16+
[unprepare_sqlserver_statement(sql, binds), binds]
17+
end
18+
19+
super(unprepared_queries, options)
1620
end
17-
super(unprepared_queries, options)
1821
end
1922

2023
private

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ module FinderMethods
1111
private
1212

1313
def construct_relation_for_exists(conditions)
14-
if klass.lease_connection.sqlserver?
15-
_construct_relation_for_exists(conditions)
16-
else
17-
super
14+
klass.with_connection do |connection|
15+
if connection.sqlserver?
16+
_construct_relation_for_exists(conditions)
17+
else
18+
super
19+
end
1820
end
1921
end
2022

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

lib/active_record/connection_adapters/sqlserver/database_statements.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def raw_execute(sql, name, async: false, allow_retry: false, materialize_transac
3030
result
3131
end
3232

33-
def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
33+
def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false)
3434
result = nil
3535
sql = transform_query(sql)
3636

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -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
@@ -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}/

0 commit comments

Comments
 (0)