Class: OnlineMigrations::BackgroundMigrations::BackgroundMigrationClassValidator

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

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/online_migrations/background_migrations/background_migration_class_validator.rb', line 7

def validate(record)
  relation = record.migration_relation
  migration_name = record.migration_name

  if !relation.is_a?(ActiveRecord::Relation)
    record.errors.add(
      :migration_name,
      "#{migration_name}#relation must return an ActiveRecord::Relation object"
    )
    return
  end

  if relation.arel.orders.present? || relation.arel.taken.present?
    record.errors.add(
      :migration_name,
      "#{migration_name}#relation cannot use ORDER BY or LIMIT due to the way how iteration with a cursor is designed. " \
      "You can use other ways to limit the number of rows, e.g. a WHERE condition on the primary key column."
    )
  end
end