diff options
author | Ryo Hashimoto <ryohashimoto@gmail.com> | 2019-04-08 12:22:54 +0900 |
---|---|---|
committer | Ryo Hashimoto <ryohashimoto@gmail.com> | 2019-04-08 23:20:14 +0900 |
commit | 54dce6870de23110cdf251d8f71a8e1a57dea7bb (patch) | |
tree | 0f9bb56ed181ba7c37e9183c0c831555224f1d90 | |
parent | 57c7cbb1623c0e8befc58988a34bbb9896fd226a (diff) | |
download | rails-54dce6870de23110cdf251d8f71a8e1a57dea7bb.tar.gz rails-54dce6870de23110cdf251d8f71a8e1a57dea7bb.tar.bz2 rails-54dce6870de23110cdf251d8f71a8e1a57dea7bb.zip |
Improve log messages for #insert_all` / `#upsert_all` / `#insert` / `#upsert etc. methods
In #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.
-rw-r--r-- | activerecord/lib/active_record/insert_all.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/insert_all_test.rb | 49 |
2 files changed, 53 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/insert_all.rb b/activerecord/lib/active_record/insert_all.rb index 4b02d40aa0..7e28b66663 100644 --- a/activerecord/lib/active_record/insert_all.rb +++ b/activerecord/lib/active_record/insert_all.rb @@ -21,7 +21,10 @@ module ActiveRecord end def execute - connection.exec_query to_sql, "Bulk Insert" + message = "#{model} " + message += "Bulk " if inserts.many? + message += (on_duplicate == :update ? "Upsert" : "Insert") + connection.exec_query to_sql, message end def updatable_columns diff --git a/activerecord/test/cases/insert_all_test.rb b/activerecord/test/cases/insert_all_test.rb index fc25701c80..9aee1e90b8 100644 --- a/activerecord/test/cases/insert_all_test.rb +++ b/activerecord/test/cases/insert_all_test.rb @@ -143,6 +143,42 @@ class InsertAllTest < ActiveRecord::TestCase end end + def test_insert_logs_message_including_model_name + skip unless supports_insert_conflict_target? + + capture_log_output do |output| + Book.insert(name: "Rework", author_id: 1) + assert_match "Book Insert", output.string + end + end + + def test_insert_all_logs_message_including_model_name + skip unless supports_insert_conflict_target? + + capture_log_output do |output| + Book.insert_all [{ name: "Remote", author_id: 1 }, { name: "Renote", author_id: 1 }] + assert_match "Book Bulk Insert", output.string + end + end + + def test_upsert_logs_message_including_model_name + skip unless supports_insert_on_duplicate_update? + + capture_log_output do |output| + Book.upsert(name: "Remote", author_id: 1) + assert_match "Book Upsert", output.string + end + end + + def test_upsert_all_logs_message_including_model_name + skip unless supports_insert_on_duplicate_update? + + capture_log_output do |output| + Book.upsert_all [{ name: "Remote", author_id: 1 }, { name: "Renote", author_id: 1 }] + assert_match "Book Bulk Upsert", output.string + end + end + def test_upsert_all_updates_existing_records skip unless supports_insert_on_duplicate_update? @@ -186,4 +222,17 @@ class InsertAllTest < ActiveRecord::TestCase Book.insert_all! [{ unknown_attribute: "Test" }] end end + + private + + def capture_log_output + output = StringIO.new + old_logger, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ActiveSupport::Logger.new(output) + + begin + yield output + ensure + ActiveRecord::Base.logger = old_logger + end + end end |