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