20170905140038_cti_log_preferences_migration.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. directions = %w[from to]
  35. Cti::Log.all.pluck(:id).each do |item_id|
  36. item = Cti::Log.find(item_id)
  37. next if !item.preferences
  38. next if item.preferences.blank?
  39. # check from and to keys which hold the instances
  40. preferences = {}
  41. directions.each do |direction|
  42. next if item.preferences[direction].blank?
  43. # loop over all instances and covert them
  44. # to an Hash via .attributes
  45. updated = item.preferences[direction].each_with_object([]) do |caller_id, new_direction|
  46. next if !caller_id.respond_to?(:attributes)
  47. new_direction.push(caller_id.attributes)
  48. end
  49. # overwrite the old key with the converted data
  50. preferences[direction] = updated
  51. end
  52. # update entry
  53. item.update_column(:preferences, preferences) # rubocop:disable Rails/SkipsModelValidations
  54. end
  55. end
  56. end