20171023000001_fixed_store_upgrade_ror_45.rb 1.2 KB

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