20171023000001_fixed_store_upgrade_ror_45.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FixedStoreUpgradeRor45 < ActiveRecord::Migration[5.0]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. Rails.cache.clear
  7. [Macro, Taskbar, Calendar, Trigger, Channel, Job, PostmasterFilter, Report::Profile, Setting, Sla, Template].each do |class_name|
  8. class_name.all.each do |record|
  9. record.save!
  10. rescue => e
  11. Rails.logger.error "Unable to save/update #{class_name}.find(#{record.id}): #{e.message}"
  12. end
  13. end
  14. Channel.all.each do |channel|
  15. next if channel.options.blank?
  16. channel.options.each do |key, value|
  17. channel.options[key] = cleanup(value)
  18. end
  19. channel.save!
  20. end
  21. User.with_permissions('ticket.agent').each do |user|
  22. next if user.preferences.blank?
  23. user.preferences.each do |key, value|
  24. user.preferences[key] = cleanup(value)
  25. end
  26. user.save!
  27. end
  28. end
  29. def cleanup(value)
  30. if value.instance_of?(ActionController::Parameters)
  31. value = value.permit!.to_h
  32. end
  33. return value if value.class != ActiveSupport::HashWithIndifferentAccess && value.class != Hash
  34. value.each do |local_key, local_value|
  35. value[local_key] = cleanup(local_value)
  36. end
  37. value
  38. end
  39. end