From 85d6d78eea1a0e645dd3d9829c1711f20819a0af Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 30 Jan 2019 19:50:37 +0800 Subject: [PATCH] Fix test failures caused by faulty id generator in ChecksImport examples --- .../application_model/checks_import_examples.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/models/application_model/checks_import_examples.rb b/spec/models/application_model/checks_import_examples.rb index 4ef072e07..973a027fb 100644 --- a/spec/models/application_model/checks_import_examples.rb +++ b/spec/models/application_model/checks_import_examples.rb @@ -1,7 +1,19 @@ RSpec.shared_examples 'ApplicationModel::ChecksImport' do describe '#id (for referential integrity during OTRS/Zendesk import)' do - subject { build(described_class.name.underscore, id: unused_id) } - let(:unused_id) { (described_class.pluck(:id).max || 1) * 2 } + subject { build(described_class.name.underscore, id: next_id + 1) } + + let(:next_id) do + case ActiveRecord::Base.connection_config[:adapter] + when 'mysql2' + ActiveRecord::Base.connection.execute(<<~QUERY).first.first + SELECT max(auto_increment) FROM information_schema.tables WHERE table_name='#{described_class.table_name}' + QUERY + when 'postgresql' + ActiveRecord::Base.connection.execute(<<~QUERY).first['last_value'].next + SELECT last_value FROM #{described_class.table_name}_id_seq + QUERY + end + end context 'when Setting.get("system_init_done") is false (regardless of import_mode)' do before { Setting.set('system_init_done', false) }