Skip to content

Commit 54dce68

Browse files
committed
Improve log messages for #insert_all / #upsert_all / #insert / #upsert etc. methods
In rails#35077, `#insert_all` / `#upsert_all` / `#insert` / `#upsert` etc. methods are added. But Active Record logs only “Bulk Insert” log messages when they are invoked. This commit improves the log messages to use collect words for how invoked them.
1 parent 57c7cbb commit 54dce68

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

activerecord/lib/active_record/insert_all.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ def initialize(model, inserts, on_duplicate:, returning: nil, unique_by: nil)
2121
end
2222

2323
def execute
24-
connection.exec_query to_sql, "Bulk Insert"
24+
message = "#{model} "
25+
message += "Bulk " if inserts.many?
26+
message += (on_duplicate == :update ? "Upsert" : "Insert")
27+
connection.exec_query to_sql, message
2528
end
2629

2730
def updatable_columns

activerecord/test/cases/insert_all_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,42 @@ def test_insert_all_and_upsert_all_raises_when_index_is_missing
143143
end
144144
end
145145

146+
def test_insert_logs_message_including_model_name
147+
skip unless supports_insert_conflict_target?
148+
149+
capture_log_output do |output|
150+
Book.insert(name: "Rework", author_id: 1)
151+
assert_match "Book Insert", output.string
152+
end
153+
end
154+
155+
def test_insert_all_logs_message_including_model_name
156+
skip unless supports_insert_conflict_target?
157+
158+
capture_log_output do |output|
159+
Book.insert_all [{ name: "Remote", author_id: 1 }, { name: "Renote", author_id: 1 }]
160+
assert_match "Book Bulk Insert", output.string
161+
end
162+
end
163+
164+
def test_upsert_logs_message_including_model_name
165+
skip unless supports_insert_on_duplicate_update?
166+
167+
capture_log_output do |output|
168+
Book.upsert(name: "Remote", author_id: 1)
169+
assert_match "Book Upsert", output.string
170+
end
171+
end
172+
173+
def test_upsert_all_logs_message_including_model_name
174+
skip unless supports_insert_on_duplicate_update?
175+
176+
capture_log_output do |output|
177+
Book.upsert_all [{ name: "Remote", author_id: 1 }, { name: "Renote", author_id: 1 }]
178+
assert_match "Book Bulk Upsert", output.string
179+
end
180+
end
181+
146182
def test_upsert_all_updates_existing_records
147183
skip unless supports_insert_on_duplicate_update?
148184

@@ -186,4 +222,17 @@ def test_insert_all_raises_on_unknown_attribute
186222
Book.insert_all! [{ unknown_attribute: "Test" }]
187223
end
188224
end
225+
226+
private
227+
228+
def capture_log_output
229+
output = StringIO.new
230+
old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(output)
231+
232+
begin
233+
yield output
234+
ensure
235+
ActiveRecord::Base.logger = old_logger
236+
end
237+
end
189238
end

0 commit comments

Comments
 (0)