From e0b70cb24f09980d574df83b57296f91fa78cf82 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Tue, 29 Nov 2016 12:56:24 +0100 Subject: [PATCH] Follow up: Fixed race condtion while importing history types for the first time. --- lib/import/otrs/history.rb | 15 +++++++++++++-- spec/import/otrs/history_examples.rb | 3 ++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/import/otrs/history.rb b/lib/import/otrs/history.rb index 18d8d86fe..d1ef84cef 100644 --- a/lib/import/otrs/history.rb +++ b/lib/import/otrs/history.rb @@ -25,11 +25,22 @@ module Import def ensure_history_attribute history_attribute = @history_attributes[:history_attribute] return if !history_attribute - @@created_history_attributes ||= {} - return if @@created_history_attributes[history_attribute] + return if history_attribute_exists?(history_attribute) @@created_history_attributes[history_attribute] = true ::History.attribute_lookup(history_attribute) end + + 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 + sleep 1 until ::History::Attribute.find_by(name: name) + true + end end end end diff --git a/spec/import/otrs/history_examples.rb b/spec/import/otrs/history_examples.rb index 1b66f0b04..1f988d559 100644 --- a/spec/import/otrs/history_examples.rb +++ b/spec/import/otrs/history_examples.rb @@ -12,7 +12,8 @@ end RSpec.shared_examples 'Import::OTRS::History' do it 'responds to init_callback' do - expect(History).to receive(:add) + expect(::History).to receive(:add) + allow(::History::Attribute).to receive(:find_by).and_return(true) blank_instance = described_class.new({}) expect(blank_instance).to respond_to('init_callback') end