Commit 516fda9f authored by Andreas Brandl's avatar Andreas Brandl

Fix reindexing bug

When a `statement_timeout` is set, this leads to trouble with the
inclusion of the migration helper.

There is basically a name clash between the reindexing class with
`#execute` and the migration helper for the statement timeout expecting
to be able to execute SQL queries through `#execute`.

This has been fixed with renaming the method on the reindexing class.

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/251084
parent 8e462453
......@@ -19,7 +19,7 @@ module Gitlab
@logger = logger
end
def execute
def perform
raise ReindexError, "index #{index_name} does not exist" unless index_exists?
raise ReindexError, 'UNIQUE indexes are currently not supported' if index_unique?
......@@ -51,6 +51,7 @@ module Gitlab
private
delegate :execute, to: :connection
def connection
@connection ||= ActiveRecord::Base.connection
end
......
......@@ -176,7 +176,7 @@ namespace :gitlab do
raise ArgumentError, 'must give the index name to reindex' unless args[:index_name]
Gitlab::Database::ConcurrentReindex.new(args[:index_name], logger: Logger.new(STDOUT)).execute
Gitlab::Database::ConcurrentReindex.new(args[:index_name], logger: Logger.new(STDOUT)).perform
end
end
end
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
RSpec.describe Gitlab::Database::ConcurrentReindex, '#perform' do
subject { described_class.new(index_name, logger: logger) }
let(:table_name) { '_test_reindex_table' }
......@@ -29,7 +29,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
end
it 'raises an error' do
expect { subject.execute }.to raise_error(described_class::ReindexError, /does not exist/)
expect { subject.perform }.to raise_error(described_class::ReindexError, /does not exist/)
end
end
......@@ -43,7 +43,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
it 'raises an error' do
expect do
subject.execute
subject.perform
end.to raise_error(described_class::ReindexError, /UNIQUE indexes are currently not supported/)
end
end
......@@ -57,6 +57,17 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
let!(:original_index) { find_index_create_statement }
it 'integration test: executing full index replacement without mocks' do
allow(connection).to receive(:execute).and_wrap_original do |method, sql|
method.call(sql.sub(/CONCURRENTLY/, ''))
end
subject.perform
check_index_exists
end
context 'mocked specs' do
before do
allow(subject).to receive(:connection).and_return(connection)
allow(subject).to receive(:disable_statement_timeout).and_yield
......@@ -78,7 +89,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
subject.execute
subject.perform
check_index_exists
end
......@@ -104,7 +115,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
subject.execute
subject.perform
check_index_exists
end
......@@ -119,7 +130,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
expect { subject.execute }.to raise_error(described_class::ReindexError, /connect timeout/)
expect { subject.perform }.to raise_error(described_class::ReindexError, /connect timeout/)
check_index_exists
end
......@@ -134,7 +145,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
expect { subject.execute }.to raise_error(described_class::ReindexError, /replacement index was created as INVALID/)
expect { subject.perform }.to raise_error(described_class::ReindexError, /replacement index was created as INVALID/)
check_index_exists
end
......@@ -155,7 +166,7 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
expect { subject.execute }.to raise_error(described_class::ReindexError, /connect timeout/)
expect { subject.perform }.to raise_error(described_class::ReindexError, /connect timeout/)
check_index_exists
end
......@@ -173,12 +184,13 @@ RSpec.describe Gitlab::Database::ConcurrentReindex, '#execute' do
expect_to_execute_concurrently_in_order(drop_index)
expect { subject.execute }.to raise_error(described_class::ReindexError, /exhausted/)
expect { subject.perform }.to raise_error(described_class::ReindexError, /exhausted/)
check_index_exists
end
end
end
end
def expect_to_execute_concurrently_in_order(sql)
# Indexes cannot be created CONCURRENTLY in a transaction. Since the tests are wrapped in transactions,
......
......@@ -180,7 +180,7 @@ RSpec.describe 'gitlab:db namespace rake task' do
.with('some_index_name', logger: instance_of(Logger))
.and_return(reindex)
expect(reindex).to receive(:execute)
expect(reindex).to receive(:perform)
run_rake_task('gitlab:db:reindex', '[some_index_name]')
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment