2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2016-11-29 11:04:09 +00:00
|
|
|
# rubocop:disable Style/ClassVars
|
2016-11-25 16:10:37 +00:00
|
|
|
module Import
|
|
|
|
module OTRS
|
|
|
|
class History
|
2016-12-06 15:26:57 +00:00
|
|
|
include Import::Helper
|
2016-11-25 16:10:37 +00:00
|
|
|
|
|
|
|
def initialize(history)
|
|
|
|
init_callback(history)
|
2016-11-29 11:04:09 +00:00
|
|
|
ensure_history_attribute
|
2016-11-25 16:10:37 +00:00
|
|
|
add
|
|
|
|
end
|
|
|
|
|
|
|
|
def init_callback(_)
|
|
|
|
raise 'No init callback defined for this history!'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def add
|
|
|
|
::History.add(@history_attributes)
|
2016-12-06 15:26:57 +00:00
|
|
|
reset_primary_key_sequence('histories')
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
2016-11-29 11:04:09 +00:00
|
|
|
|
|
|
|
# make sure that no other thread is importing just the same
|
|
|
|
# history attribute which causes a ActiveRecord::RecordNotUnique
|
|
|
|
# exception we (currently) can't handle otherwise
|
|
|
|
def ensure_history_attribute
|
|
|
|
history_attribute = @history_attributes[:history_attribute]
|
|
|
|
return if !history_attribute
|
2016-11-29 11:56:24 +00:00
|
|
|
return if history_attribute_exists?(history_attribute)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-11-29 11:04:09 +00:00
|
|
|
@@created_history_attributes[history_attribute] = true
|
|
|
|
::History.attribute_lookup(history_attribute)
|
|
|
|
end
|
2016-11-29 11:56:24 +00:00
|
|
|
|
|
|
|
def history_attribute_exists?(name)
|
|
|
|
@@created_history_attributes ||= {}
|
|
|
|
return false if !@@created_history_attributes[name]
|
|
|
|
|
|
|
|
# make sure the history attribute is added before we
|
|
|
|
# we perform further import
|
|
|
|
# otherwise the following import logic (add) will
|
|
|
|
# try to add the history attribute, too
|
2020-08-03 08:35:43 +00:00
|
|
|
sleep 1 until ::History::Attribute.exists?(name: name)
|
2016-11-29 11:56:24 +00:00
|
|
|
true
|
|
|
|
end
|
2016-11-25 16:10:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|