Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid loading all tasks to validate a Run#task_name #866

Merged
merged 1 commit into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/models/maintenance_tasks/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class Run < ApplicationRecord

enum status: STATUSES.to_h { |status| [status, status.to_s] }

validates :task_name, on: :create, inclusion: {
in: ->(_) { Task.available_tasks.map(&:to_s) },
}
validate :task_name_belongs_to_a_valid_task, on: :create
validate :csv_attachment_presence, on: :create
validate :csv_content_type, on: :create
validate :validate_task_arguments, on: :create
Expand Down Expand Up @@ -339,6 +337,15 @@ def stuck?
cancelling? && updated_at <= STUCK_TASK_TIMEOUT.ago
end

# Performs validation on the task_name attribute.
# A Run must be associated with a valid Task to be valid.
# In order to confirm that, the Task is looked up by name.
def task_name_belongs_to_a_valid_task
Task.named(task_name)
rescue Task::NotFoundError
errors.add(:task_name, "must be the name of an existing Task.")
end

# Performs validation on the presence of a :csv_file attachment.
# A Run for a Task that uses CsvCollection must have an attached :csv_file
# to be valid. Conversely, a Run for a Task that doesn't use CsvCollection
Expand Down
2 changes: 1 addition & 1 deletion test/models/maintenance_tasks/runner_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class RunnerTest < ActiveSupport::TestCase
end

assert_equal(
"Validation failed: Task name is not included in the list",
"Validation failed: Task name must be the name of an existing Task.",
error.message,
)
end
Expand Down