Skip to content

Commit ec7deca

Browse files
committed
Refactor InsertAll not to permanently lease a connection
Extracted from: rails#50793
1 parent 2bf0d68 commit ec7deca

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

activerecord/lib/active_record/insert_all.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@ class InsertAll # :nodoc:
77
attr_reader :model, :connection, :inserts, :keys
88
attr_reader :on_duplicate, :update_only, :returning, :unique_by, :update_sql
99

10-
def initialize(model, inserts, on_duplicate:, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
11-
@model, @connection, @inserts = model, model.lease_connection, inserts.map(&:stringify_keys)
10+
class << self
11+
def execute(model, ...)
12+
model.with_connection do |c|
13+
new(model, c, ...).execute
14+
end
15+
end
16+
end
17+
18+
def initialize(model, connection, inserts, on_duplicate:, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
19+
@model, @connection, @inserts = model, connection, inserts.map(&:stringify_keys)
1220
@on_duplicate, @update_only, @returning, @unique_by = on_duplicate, update_only, returning, unique_by
1321
@record_timestamps = record_timestamps.nil? ? model.record_timestamps : record_timestamps
1422

activerecord/lib/active_record/persistence.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def insert(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
173173
# { id: 2, title: "Eloquent Ruby" }
174174
# ])
175175
def insert_all(attributes, returning: nil, unique_by: nil, record_timestamps: nil)
176-
InsertAll.new(self, attributes, on_duplicate: :skip, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps).execute
176+
InsertAll.execute(self, attributes, on_duplicate: :skip, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps)
177177
end
178178

179179
# Inserts a single record into the database in a single SQL INSERT
@@ -240,7 +240,7 @@ def insert!(attributes, returning: nil, record_timestamps: nil)
240240
# { id: 1, title: "Eloquent Ruby", author: "Russ" }
241241
# ])
242242
def insert_all!(attributes, returning: nil, record_timestamps: nil)
243-
InsertAll.new(self, attributes, on_duplicate: :raise, returning: returning, record_timestamps: record_timestamps).execute
243+
InsertAll.execute(self, attributes, on_duplicate: :raise, returning: returning, record_timestamps: record_timestamps)
244244
end
245245

246246
# Updates or inserts (upserts) a single record into the database in a
@@ -360,7 +360,7 @@ def upsert(attributes, **kwargs)
360360
#
361361
# Book.find_by(isbn: "1").title # => "Eloquent Ruby"
362362
def upsert_all(attributes, on_duplicate: :update, update_only: nil, returning: nil, unique_by: nil, record_timestamps: nil)
363-
InsertAll.new(self, attributes, on_duplicate: on_duplicate, update_only: update_only, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps).execute
363+
InsertAll.execute(self, attributes, on_duplicate: on_duplicate, update_only: update_only, returning: returning, unique_by: unique_by, record_timestamps: record_timestamps)
364364
end
365365

366366
# Given an attributes hash, +instantiate+ returns a new instance of

0 commit comments

Comments
 (0)