20201202080338_issue3270_selector_update.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. class Issue3270SelectorUpdate < ActiveRecord::Migration[5.2]
  2. def change
  3. # return if it's a new setup
  4. return if !Setting.exists?(name: 'system_init_done')
  5. Overview.find_each do |overview|
  6. fix_selector(overview)
  7. end
  8. Trigger.find_each do |trigger|
  9. fix_selector(trigger)
  10. end
  11. Job.find_each do |job|
  12. fix_selector(job)
  13. end
  14. end
  15. def fix_selector(object)
  16. fixed = false
  17. object.condition.each do |_attribute, attribute_condition|
  18. next if attribute_condition['operator'] != 'within next (relative)' && attribute_condition['operator'] != 'within last (relative)'
  19. attribute_condition['operator'] = if attribute_condition['operator'] == 'within next (relative)'
  20. 'before (relative)'
  21. else
  22. 'before (after)'
  23. end
  24. fixed = true
  25. end
  26. return if !fixed
  27. save(object)
  28. end
  29. def save(object)
  30. object.save
  31. rescue => e
  32. Rails.logger.error "Migration Issue3270SelectorUpdate failed: #{object.class} - #{object.id} - #{e.inspect}."
  33. end
  34. end