Commit 2ddfd434 authored by Andreas Brandl's avatar Andreas Brandl

Add PostgresIndex.not_match scope

parent 4d761683
...@@ -21,6 +21,8 @@ module Gitlab ...@@ -21,6 +21,8 @@ module Gitlab
limit(how_many).order(Arel.sql('RANDOM()')) limit(how_many).order(Arel.sql('RANDOM()'))
end end
scope :not_match, ->(regex) { where("name !~ ?", regex)}
def to_s def to_s
name name
end end
......
...@@ -30,7 +30,7 @@ RSpec.describe Gitlab::Database::PostgresIndex do ...@@ -30,7 +30,7 @@ RSpec.describe Gitlab::Database::PostgresIndex do
end end
end end
describe '#regular' do describe '.regular' do
it 'only non-unique indexes' do it 'only non-unique indexes' do
expect(described_class.regular).to all(have_attributes(unique: false)) expect(described_class.regular).to all(have_attributes(unique: false))
end end
...@@ -44,7 +44,17 @@ RSpec.describe Gitlab::Database::PostgresIndex do ...@@ -44,7 +44,17 @@ RSpec.describe Gitlab::Database::PostgresIndex do
end end
end end
describe '#random_few' do describe '.not_match' do
it 'excludes indexes matching the given regex' do
expect(described_class.not_match('^bar_k').map(&:name)).to all(match(/^(?!bar_k).*/))
end
it 'matches indexes without this prefix regex' do
expect(described_class.not_match('^bar_k')).not_to be_empty
end
end
describe '.random_few' do
it 'limits to two records by default' do it 'limits to two records by default' do
expect(described_class.random_few(2).size).to eq(2) expect(described_class.random_few(2).size).to eq(2)
end 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