20171023000001_fixed_store_upgrade_ror_45.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class FixedStoreUpgradeRor45 < ActiveRecord::Migration[5.0]
  2. def up
  3. # return if it's a new setup
  4. return if !Setting.find_by(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. begin
  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. end
  15. Channel.all.each do |channel|
  16. next if channel.options.blank?
  17. channel.options.each do |key, value|
  18. channel.options[key] = cleanup(value)
  19. end
  20. channel.save!
  21. end
  22. User.with_permissions('ticket.agent').each do |user|
  23. next if user.preferences.blank?
  24. user.preferences.each do |key, value|
  25. user.preferences[key] = cleanup(value)
  26. end
  27. user.save!
  28. end
  29. end
  30. def cleanup(value)
  31. if value.class == ActionController::Parameters
  32. value = value.permit!.to_h
  33. end
  34. return value if value.class != ActiveSupport::HashWithIndifferentAccess && value.class != Hash
  35. value.each do |local_key, local_value|
  36. value[local_key] = cleanup(local_value)
  37. end
  38. value
  39. end
  40. end