history.rb 1.4 KB

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