Commit d2a38b3e authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'ab/fix-reindexing-statement-timeout-bug' into 'master'

Fix reindexing bug when statement_timeout set

See merge request gitlab-org/gitlab!42692
parents 2980804d 516fda9f
......@@ -28,6 +28,8 @@ development:
username: postgres
password: "secure password"
host: localhost
variables:
statement_timeout: 15s
#
# Staging specific
......@@ -51,3 +53,5 @@ test: &test
password:
host: localhost
prepared_statements: false
variables:
statement_timeout: 15s
......@@ -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,
......
......@@ -514,6 +514,7 @@ RSpec.describe Gitlab::Database::PartitioningMigrationHelpers::TableManagementHe
allow(migration).to receive(:table_exists?).with(partitioned_table).and_return(true)
allow(migration).to receive(:copy_missed_records)
allow(migration).to receive(:execute).with(/VACUUM/)
allow(migration).to receive(:execute).with(/^(RE)?SET/)
end
it 'finishes remaining jobs for the correct table' do
......@@ -567,6 +568,7 @@ RSpec.describe Gitlab::Database::PartitioningMigrationHelpers::TableManagementHe
allow(Gitlab::BackgroundMigration).to receive(:steal)
allow(migration).to receive(:execute).with(/VACUUM/)
allow(migration).to receive(:execute).with(/^(RE)?SET/)
end
it 'idempotently cleans up after failed background migrations' do
......
......@@ -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