Files
codeocean/db/migrate/20181129093207_drop_errors.rb
2021-05-14 22:03:06 +02:00

43 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class DropErrors < ActiveRecord::Migration[5.2]
# define old CodeOcean::Error module so that the migration works
module CodeOcean
class Error < ApplicationRecord
belongs_to :execution_environment
scope :for_execution_environment, ->(execution_environment) { where(execution_environment_id: execution_environment.id) }
scope :grouped_by_message, -> { select('MAX(created_at) AS created_at, MAX(id) AS id, message, COUNT(id) AS count').group(:message).order('count DESC') }
validates :execution_environment_id, presence: true
validates :message, presence: true
def self.nested_resource?
true
end
delegate :to_s, to: :id
end
end
def change
puts 'Migrating CodeOcean::Errors to StructuredErrors using RegEx. This might take a (long) while but will return.'
submissions_controller = SubmissionsController.new
# Iterate only over those Errors containing a message and submission_id
CodeOcean::Error.where.not(message: [nil, '']).where.not(submission_id: [nil, '']).each do |error|
raw_output = error.message
submission = Submission.find_by(id: error.submission_id)
# Validate that we have everything we need: the output, the submission and the execution environment
next if submission.blank? || submission.exercise.execution_environment.blank?
submissions_controller.instance_variable_set(:@raw_output, raw_output)
submissions_controller.instance_variable_set(:@submission, submission)
submissions_controller.extract_errors
end
drop_table :errors
end
end