20140505201716_migrate_agents_to_liquid_templating.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. require 'liquid_migrator'
  2. class MigrateAgentsToLiquidTemplating < ActiveRecord::Migration[4.2]
  3. class Agent < ActiveRecord::Base
  4. include JSONSerializedField
  5. json_serialize :options, :memory
  6. end
  7. class Agents::HipchatAgent < Agent
  8. end
  9. class Agents::EventFormattingAgent < Agent
  10. end
  11. class Agents::PushbulletAgent < Agent
  12. end
  13. class Agents::JabberAgent < Agent
  14. end
  15. class Agents::DataOutputAgent < Agent
  16. end
  17. class Agents::TranslationAgent < Agent
  18. end
  19. class Agents::TwitterPublishAgent < Agent
  20. end
  21. class Agents::TriggerAgent < Agent
  22. end
  23. class Agents::PeakDetectorAgent < Agent
  24. end
  25. class Agents::HumanTaskAgent < Agent
  26. end
  27. def up
  28. Agent.where(:type => 'Agents::HipchatAgent').each do |agent|
  29. LiquidMigrator.convert_all_agent_options(agent)
  30. end
  31. Agent.where(:type => 'Agents::EventFormattingAgent').each do |agent|
  32. agent.options['instructions'] = LiquidMigrator.convert_hash(agent.options['instructions'], {:merge_path_attributes => true, :leading_dollarsign_is_jsonpath => true})
  33. agent.save
  34. end
  35. Agent.where(:type => 'Agents::PushbulletAgent').each do |agent|
  36. LiquidMigrator.convert_all_agent_options(agent)
  37. end
  38. Agent.where(:type => 'Agents::JabberAgent').each do |agent|
  39. LiquidMigrator.convert_all_agent_options(agent)
  40. end
  41. Agent.where(:type => 'Agents::DataOutputAgent').each do |agent|
  42. LiquidMigrator.convert_all_agent_options(agent)
  43. end
  44. Agent.where(:type => 'Agents::TranslationAgent').each do |agent|
  45. agent.options['content'] = LiquidMigrator.convert_hash(agent.options['content'], {:merge_path_attributes => true, :leading_dollarsign_is_jsonpath => true})
  46. agent.save
  47. end
  48. Agent.where(:type => 'Agents::TwitterPublishAgent').each do |agent|
  49. if (message = agent.options.delete('message_path')).present?
  50. agent.options['message'] = "{{#{message}}}"
  51. agent.save
  52. end
  53. end
  54. Agent.where(:type => 'Agents::TriggerAgent').each do |agent|
  55. agent.options['message'] = LiquidMigrator.convert_make_message(agent.options['message'])
  56. agent.save
  57. end
  58. Agent.where(:type => 'Agents::PeakDetectorAgent').each do |agent|
  59. agent.options['message'] = LiquidMigrator.convert_make_message(agent.options['message'])
  60. agent.save
  61. end
  62. Agent.where(:type => 'Agents::HumanTaskAgent').each do |agent|
  63. LiquidMigrator.convert_all_agent_options(agent)
  64. end
  65. end
  66. def down
  67. raise ActiveRecord::IrreversibleMigration, "Cannot revert migration to Liquid templating"
  68. end
  69. end