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 =
{
  # enqueued -> running occurs when the migration starts performing.
  "enqueued" => ["running", "cancelled"],
  # running -> succeeded occurs when the migration completes successfully.
  # running -> failed occurs when the migration raises an exception when running and retry attempts exceeded.
  "running" => ["succeeded", "failed", "cancelled"],
  # failed -> enqueued occurs when the failed migration is enqueued to be retried.
  # failed -> running occurs when the failed migration is retried.
  "failed" => ["enqueued", "running", "cancelled"],
}

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/online_migrations/background_schema_migrations/migration_status_validator.rb', line 18

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