2021-01-03 14:25:43 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Gitlab::Database::Reindexing do
|
|
|
|
include ExclusiveLeaseHelpers
|
|
|
|
|
|
|
|
describe '.perform' do
|
2021-02-22 17:27:13 +05:30
|
|
|
subject { described_class.perform(candidate_indexes) }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
let(:coordinator) { instance_double(Gitlab::Database::Reindexing::Coordinator) }
|
2021-02-22 17:27:13 +05:30
|
|
|
let(:index_selection) { instance_double(Gitlab::Database::Reindexing::IndexSelection) }
|
|
|
|
let(:candidate_indexes) { double }
|
2021-03-08 18:12:59 +05:30
|
|
|
let(:indexes) { [double, double] }
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
it 'delegates to Coordinator' do
|
2021-02-22 17:27:13 +05:30
|
|
|
expect(Gitlab::Database::Reindexing::IndexSelection).to receive(:new).with(candidate_indexes).and_return(index_selection)
|
|
|
|
expect(index_selection).to receive(:take).with(2).and_return(indexes)
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
indexes.each do |index|
|
|
|
|
expect(Gitlab::Database::Reindexing::Coordinator).to receive(:new).with(index).and_return(coordinator)
|
|
|
|
expect(coordinator).to receive(:perform)
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
describe '.cleanup_leftovers!' do
|
|
|
|
subject { described_class.cleanup_leftovers! }
|
|
|
|
|
|
|
|
before do
|
|
|
|
ApplicationRecord.connection.execute(<<~SQL)
|
|
|
|
CREATE INDEX foobar_ccnew ON users (id);
|
|
|
|
CREATE INDEX foobar_ccnew1 ON users (id);
|
|
|
|
SQL
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
it 'drops both leftover indexes' do
|
|
|
|
expect_query("SET lock_timeout TO '60000ms'")
|
|
|
|
expect_query("DROP INDEX CONCURRENTLY IF EXISTS \"public\".\"foobar_ccnew\"")
|
|
|
|
expect_query("RESET idle_in_transaction_session_timeout; RESET lock_timeout")
|
|
|
|
expect_query("SET lock_timeout TO '60000ms'")
|
|
|
|
expect_query("DROP INDEX CONCURRENTLY IF EXISTS \"public\".\"foobar_ccnew1\"")
|
|
|
|
expect_query("RESET idle_in_transaction_session_timeout; RESET lock_timeout")
|
2021-01-03 14:25:43 +05:30
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
def expect_query(sql)
|
|
|
|
expect(ApplicationRecord.connection).to receive(:execute).ordered.with(sql).and_wrap_original do |method, sql|
|
|
|
|
method.call(sql.sub(/CONCURRENTLY/, ''))
|
|
|
|
end
|
2021-01-03 14:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|