20180220171219_check_for_object_attributes.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class CheckForObjectAttributes < ActiveRecord::Migration[5.1]
  2. def change
  3. return if !Setting.exists?(name: 'system_init_done')
  4. attributes.each do |attribute|
  5. fix_nil_data_option(attribute)
  6. fix_options(attribute)
  7. fix_relation(attribute)
  8. fix_interger_missing_min_max(attribute)
  9. next if !attribute.changed?
  10. attribute.save!
  11. end
  12. end
  13. private
  14. def attributes
  15. ObjectManager::Attribute.all
  16. end
  17. def fix_nil_data_option(attribute)
  18. return if attribute[:data_option].is_a?(Hash) || attribute[:data_option][:options].is_a?(Array)
  19. attribute[:data_option] = {}
  20. end
  21. def fix_options(attribute)
  22. return if attribute[:data_option][:options].is_a?(Hash)
  23. return if attribute[:data_option][:options].is_a?(Array)
  24. attribute[:data_option][:options] = {}
  25. end
  26. def fix_relation(attribute)
  27. return if attribute[:data_option][:relation].is_a?(String)
  28. attribute[:data_option][:relation] = ''
  29. end
  30. # fixes issue #2318 - Upgrade to Zammad 2.7 was not possible (migration 20180220171219 CheckForObjectAttributes failed)
  31. def fix_interger_missing_min_max(attribute)
  32. return if attribute[:data_type] != 'integer'
  33. attribute[:data_option][:min] = 0 if !attribute[:data_option][:min]
  34. attribute[:data_option][:max] = 1_000_000 if !attribute[:data_option][:max]
  35. end
  36. end