history.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. # rubocop:disable Style/ClassVars
  3. module Import
  4. module OTRS
  5. class History
  6. include Import::Helper
  7. def initialize(history)
  8. init_callback(history)
  9. ensure_history_attribute
  10. add
  11. end
  12. def init_callback(_)
  13. raise 'No init callback defined for this history!'
  14. end
  15. private
  16. def add
  17. ::History.add(@history_attributes)
  18. reset_primary_key_sequence('histories')
  19. end
  20. # make sure that no other thread is importing just the same
  21. # history attribute which causes a ActiveRecord::RecordNotUnique
  22. # exception we (currently) can't handle otherwise
  23. def ensure_history_attribute
  24. history_attribute = @history_attributes[:history_attribute]
  25. return if !history_attribute
  26. return if history_attribute_exists?(history_attribute)
  27. @@created_history_attributes[history_attribute] = true
  28. ::History.attribute_lookup(history_attribute)
  29. end
  30. def history_attribute_exists?(name)
  31. @@created_history_attributes ||= {}
  32. return false if !@@created_history_attributes[name]
  33. # make sure the history attribute is added before we
  34. # we perform further import
  35. # otherwise the following import logic (add) will
  36. # try to add the history attribute, too
  37. sleep 1 until ::History::Attribute.exists?(name: name)
  38. true
  39. end
  40. end
  41. end
  42. end