20170905140038_cti_log_preferences_migration.rb 1.9 KB

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