20170905140038_cti_log_preferences_migration.rb 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. # Rails dropped the class
  3. # ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::MysqlDateTime
  4. # via: https://github.com/rails/rails/commit/f1a0fa9e19b7e4ccaea191fc6cf0613880222ee7
  5. # ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer
  6. # via: https://github.com/rails/rails/commit/aafee233fb3b4211ee0bfb1fca776c159bd1067e
  7. # which we use in stored Cti::Log instance preferences.
  8. # Since we don't need the instances but just an Hash we have to:
  9. # - create a dummy class
  10. # - loop over all instances
  11. # - deserialize them in the preferences
  12. # - replace them in the preferences with the Hash version
  13. # create a dummy class
  14. module ActiveRecord
  15. module ConnectionAdapters
  16. class AbstractMysqlAdapter
  17. class MysqlDateTime < Type::DateTime
  18. end
  19. end
  20. end
  21. end
  22. module ActiveRecord
  23. module ConnectionAdapters
  24. module PostgreSQL
  25. module OID
  26. class Integer < Type::Integer
  27. end
  28. end
  29. end
  30. end
  31. end
  32. class CtiLogPreferencesMigration < ActiveRecord::Migration[5.0]
  33. def change
  34. # correct all entries
  35. directions = %w[from to]
  36. Cti::Log.all.pluck(:id).each do |item_id|
  37. item = Cti::Log.find(item_id)
  38. next if !item.preferences
  39. next if item.preferences.blank?
  40. # check from and to keys which hold the instances
  41. preferences = {}
  42. directions.each do |direction|
  43. next if item.preferences[direction].blank?
  44. # loop over all instances and covert them
  45. # to an Hash via .attributes
  46. updated = item.preferences[direction].each_with_object([]) do |caller_id, new_direction|
  47. next if !caller_id.respond_to?(:attributes)
  48. new_direction.push(caller_id.attributes)
  49. end
  50. # overwrite the old key with the converted data
  51. preferences[direction] = updated
  52. end
  53. # update entry
  54. item.update_column(:preferences, preferences) # rubocop:disable Rails/SkipsModelValidations
  55. end
  56. end
  57. end