From b375aed543964bf1babfc4eadcaa502da58339ef Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Wed, 15 Feb 2023 22:48:04 +0100 Subject: [PATCH] Bulk insert TestrunMessages With Sentry, we identified that all TestrunMessages are created one-by-one (besides passing them all in an array to #create!). Therefore, we now do the validations on our own first and then store all once, dramatically reducing the required time. Fixes CODEOCEAN-HP --- app/models/testrun_message.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/models/testrun_message.rb b/app/models/testrun_message.rb index 5b2e163a..a6d708a5 100644 --- a/app/models/testrun_message.rb +++ b/app/models/testrun_message.rb @@ -56,7 +56,7 @@ class TestrunMessage < ApplicationRecord filtered_messages = filter_messages_by_size testrun, messages # An array with hashes is passed, all are stored - TestrunMessage.create!(filtered_messages) + validate_and_store!(filtered_messages) end def self.filter_messages_by_size(testrun, messages) @@ -83,6 +83,20 @@ class TestrunMessage < ApplicationRecord end filtered_messages.select(&:present?) end + private_class_method :filter_messages_by_size + + def self.validate_and_store!(messages) + validated_messages = messages.map do |message| + testrun_message = TestrunMessage.new(message) + testrun_message.validate! + # We serialize the message without the ID, created_at and updated_at, as they are generated by the database. + testrun_message.serializable_hash(except: %w[id created_at updated_at]) + end + + # Now, we store all messages and skip validations (they are already done) + TestrunMessage.insert_all!(validated_messages) # rubocop:disable Rails/SkipsModelValidations + end + private_class_method :validate_and_store! def either_data_or_log if [data, log].count(&:present?) > 1