Skip to content

Commit ae62d08

Browse files
committed
Fix more with_connection offences inside Active Record
1 parent 23729ce commit ae62d08

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

activerecord/lib/active_record/persistence.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,16 @@ def _insert_record(connection, values, returning) # :nodoc:
248248

249249
im = Arel::InsertManager.new(arel_table)
250250

251-
with_connection do |c|
252-
if values.empty?
253-
im.insert(connection.empty_insert_statement_value(primary_key))
254-
else
255-
im.insert(values.transform_keys { |name| arel_table[name] })
256-
end
257-
258-
connection.insert(
259-
im, "#{self} Create", primary_key || false, primary_key_value,
260-
returning: returning
261-
)
251+
if values.empty?
252+
im.insert(connection.empty_insert_statement_value(primary_key))
253+
else
254+
im.insert(values.transform_keys { |name| arel_table[name] })
262255
end
256+
257+
connection.insert(
258+
im, "#{self} Create", primary_key || false, primary_key_value,
259+
returning: returning
260+
)
263261
end
264262

265263
def _update_record(values, constraints) # :nodoc:

activerecord/lib/active_record/sanitization.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ def sanitize_sql_for_order(condition)
105105
# sanitize_sql_hash_for_assignment({ status: nil, group_id: 1 }, "posts")
106106
# # => "`posts`.`status` = NULL, `posts`.`group_id` = 1"
107107
def sanitize_sql_hash_for_assignment(attrs, table)
108-
c = connection
109-
attrs.map do |attr, value|
110-
type = type_for_attribute(attr)
111-
value = type.serialize(type.cast(value))
112-
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
113-
end.join(", ")
108+
with_connection do |c|
109+
attrs.map do |attr, value|
110+
type = type_for_attribute(attr)
111+
value = type.serialize(type.cast(value))
112+
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
113+
end.join(", ")
114+
end
114115
end
115116

116117
# Sanitizes a +string+ so that it is safe to use within an SQL

activerecord/lib/active_record/schema_migration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def delete_version(version)
3434
end
3535

3636
def delete_all_versions
37-
@pool.with_connection do |connection|
37+
# Eagerly check in connection to avoid checking in/out many times in the called method.
38+
@pool.with_connection do
3839
versions.each do |version|
3940
delete_version(version)
4041
end

0 commit comments

Comments
 (0)