Class: OnlineMigrations::BackgroundMigrations::MigrationJobStatusValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/online_migrations/background_migrations/migration_job_status_validator.rb

Constant Summary collapse

VALID_STATUS_TRANSITIONS =
{
  "enqueued" => ["running", "cancelled"],
  "running" => ["succeeded", "failed", "cancelled"],
  "failed" => ["enqueued", "running", "cancelled"],
}

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/online_migrations/background_migrations/migration_job_status_validator.rb', line 13

def validate(record)
  return if !record.status_changed?

  previous_status, new_status = record.status_change
  valid_new_statuses = VALID_STATUS_TRANSITIONS.fetch(previous_status, [])

  if !valid_new_statuses.include?(new_status)
    record.errors.add(
      :status,
      "cannot transition background migration job from status #{previous_status} to #{new_status}"
    )
  end
end