20200716124141_issue_3123_external_sync_ticket_merge.rb 886 B

12345678910111213141516171819202122232425262728293031
  1. class Issue3123ExternalSyncTicketMerge < ActiveRecord::Migration[5.2]
  2. def change
  3. # return if it's a new setup
  4. return if !Setting.exists?(name: 'system_init_done')
  5. merged_ticket_ids_with_external_sync.each do |id_from|
  6. id_to = merged_ticket_ids_map[id_from]
  7. ExternalSync.migrate('Ticket', id_from, id_to)
  8. end
  9. end
  10. private
  11. # reduce to the ones with an ExternalSync entry
  12. def merged_ticket_ids_with_external_sync
  13. @merged_ticket_ids_with_external_sync ||= ExternalSync.where(
  14. object: 'Ticket',
  15. o_id: merged_ticket_ids_map.keys,
  16. ).pluck(:o_id).uniq
  17. end
  18. # get all merged tickets
  19. def merged_ticket_ids_map
  20. @merged_ticket_ids_map ||= History.where(
  21. history_type_id: History.type_lookup('merged_into').id,
  22. history_object_id: History.object_lookup('Ticket').id,
  23. ).pluck(:id_from, :id_to).to_h
  24. end
  25. end