20230509091602_add_history_source.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class AddHistorySource < ActiveRecord::Migration[6.1]
  3. def up
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. migrate_table
  7. migrate_time_trigger_history_entries
  8. end
  9. private
  10. def migrate_table
  11. change_table :histories do |t|
  12. t.references :sourceable, polymorphic: true, null: true
  13. t.string :sourceable_name, limit: 500
  14. end
  15. History.reset_column_information
  16. end
  17. def migrate_time_trigger_history_entries
  18. old_history_entries.in_batches.each_record do |elem|
  19. update_record(elem)
  20. end
  21. end
  22. def update_record(record)
  23. record.update_columns( # rubocop:disable Rails/SkipsModelValidations
  24. sourceable_type: 'Trigger',
  25. sourceable_id: record.related_o_id,
  26. sourceable_name: record.value_to,
  27. related_history_object_id: nil,
  28. related_o_id: nil,
  29. value_to: nil
  30. )
  31. record.cache_delete
  32. end
  33. def old_history_entries
  34. History.where(
  35. history_type_id: History.type_lookup('time_trigger_performed').id,
  36. )
  37. end
  38. end