20180220171219_check_for_object_attributes.rb 1.4 KB

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