Class: OnlineMigrations::BackgroundSchemaMigrations::MigrationStatusValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/online_migrations/background_schema_migrations/migration_status_validator.rb

Constant Summary collapse

VALID_STATUS_TRANSITIONS =
{
  # pending -> running occurs when the migration starts performing.
  "pending" => ["running", "cancelled"],
  # running -> succeeded occurs when the migration completes successfully.
  # running -> errored occurs when the migration raised an error during the last run.
  # running -> failed occurs when the migration raises an error when running and retry attempts exceeded.
  "running" => ["succeeded", "errored", "failed", "cancelled"],
  # errored -> running occurs when previously errored migration starts running
  # errored -> failed occurs when the migration raises an error when running and retry attempts exceeded.
  "errored" => ["running", "failed", "cancelled"],
  # failed -> pending occurs when the failed migration is enqueued to be retried.
  # failed -> running occurs when the failed migration is retried.
  "failed" => ["pending", "running", "cancelled"],
  # delayed -> pending occurs when the delayed migration was approved by the user to start running.
  # delayed -> cancelled occurs when the delayed migration was cancelled.
  "delayed" => ["pending", "cancelled"],
}

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/online_migrations/background_schema_migrations/migration_status_validator.rb', line 25

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 schema migration from status #{previous_status} to #{new_status}"
    )
  end
end