Commit e053a3e1 authored by Andreas Brandl's avatar Andreas Brandl

Move into proper scopes

This moves the idea of "candidate indexes" into a proper scope on the
model
parent f8de35f7
......@@ -19,7 +19,12 @@ module Gitlab
end
# Indexes with reindexing support
scope :reindexing_support, -> { where(partitioned: false, exclusion: false, expression: false, type: Gitlab::Database::Reindexing::SUPPORTED_TYPES) }
scope :reindexing_support, -> do
where(partitioned: false, exclusion: false, expression: false, type: Gitlab::Database::Reindexing::SUPPORTED_TYPES)
.not_match("#{Gitlab::Database::Reindexing::ReindexConcurrently::TEMPORARY_INDEX_PATTERN}$")
end
scope :reindexing_leftovers, -> { match("#{Gitlab::Database::Reindexing::ReindexConcurrently::TEMPORARY_INDEX_PATTERN}$") }
scope :not_match, ->(regex) { where("name !~ ?", regex) }
......
......@@ -14,12 +14,6 @@ module Gitlab
Coordinator.new(index).perform
end
end
def self.candidate_indexes
Gitlab::Database::PostgresIndex
.not_match("#{ReindexConcurrently::TEMPORARY_INDEX_PATTERN}$")
.reindexing_support
end
end
end
end
......@@ -161,7 +161,7 @@ namespace :gitlab do
exit
end
indexes = Gitlab::Database::Reindexing.candidate_indexes
indexes = Gitlab::Database::PostgresIndex.reindexing_support
if identifier = args[:index_name]
raise ArgumentError, "Index name is not fully qualified with a schema: #{identifier}" unless identifier =~ /^\w+\.\w+$/
......
......@@ -40,6 +40,37 @@ RSpec.describe Gitlab::Database::PostgresIndex do
expect(types & %w(btree gist)).to eq(types)
end
context 'with leftover indexes' do
before do
ActiveRecord::Base.connection.execute(<<~SQL)
CREATE INDEX foobar_ccnew ON users (id);
CREATE INDEX foobar_ccnew1 ON users (id);
SQL
end
subject { described_class.reindexing_support.map(&:name) }
it 'excludes temporary indexes from reindexing' do
expect(subject).not_to include('foobar_ccnew')
expect(subject).not_to include('foobar_ccnew1')
end
end
end
describe '.reindexing_leftovers' do
subject { described_class.reindexing_leftovers }
before do
ActiveRecord::Base.connection.execute(<<~SQL)
CREATE INDEX foobar_ccnew ON users (id);
CREATE INDEX foobar_ccnew1 ON users (id);
SQL
end
it 'retrieves leftover indexes matching the /_ccnew[0-9]*$/ pattern' do
expect(subject.map(&:name)).to eq(%w(foobar_ccnew foobar_ccnew1))
end
end
describe '.not_match' do
......
......@@ -25,15 +25,4 @@ RSpec.describe Gitlab::Database::Reindexing do
subject
end
end
describe '.candidate_indexes' do
subject { described_class.candidate_indexes }
it 'retrieves regular indexes that are no left-overs from previous runs' do
result = double
expect(Gitlab::Database::PostgresIndex).to receive_message_chain('not_match.reindexing_support').with('\_ccnew[0-9]*$').with(no_args).and_return(result)
expect(subject).to eq(result)
end
end
end
......@@ -203,7 +203,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
context 'when no index_name is given' do
it 'uses all candidate indexes' do
expect(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes)
expect(Gitlab::Database::PostgresIndex).to receive(:reindexing_support).and_return(indexes)
expect(Gitlab::Database::Reindexing).to receive(:perform).with(indexes)
run_rake_task('gitlab:db:reindex')
......@@ -214,7 +214,7 @@ RSpec.describe 'gitlab:db namespace rake task', :silence_stdout do
let(:index) { double('index') }
before do
allow(Gitlab::Database::Reindexing).to receive(:candidate_indexes).and_return(indexes)
allow(Gitlab::Database::PostgresIndex).to receive(:reindexing_support).and_return(indexes)
end
it 'calls the index rebuilder with the proper arguments' do
......
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