summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/reindexing/index_selection_spec.rb
blob: a5e2f368f406c63599d3d096d5d67c8361a1f85e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::Reindexing::IndexSelection do
  include DatabaseHelpers

  subject { described_class.new(Gitlab::Database::PostgresIndex.all).to_a }

  before do
    swapout_view_for_table(:postgres_index_bloat_estimates)
    swapout_view_for_table(:postgres_indexes)
  end

  def execute(sql)
    ActiveRecord::Base.connection.execute(sql)
  end

  it 'orders by highest bloat first' do
    create_list(:postgres_index, 10).each_with_index do |index, i|
      create(:postgres_index_bloat_estimate, index: index, bloat_size_bytes: 1.megabyte * i)
    end

    expected = Gitlab::Database::PostgresIndexBloatEstimate.order(bloat_size_bytes: :desc).map(&:index)

    expect(subject).to eq(expected)
  end

  context 'with time frozen' do
    around do |example|
      freeze_time { example.run }
    end

    it 'does not return indexes with reindex action in the last 7 days' do
      not_recently_reindexed = create_list(:postgres_index, 2).each_with_index do |index, i|
        create(:postgres_index_bloat_estimate, index: index, bloat_size_bytes: 1.megabyte * i)
        create(:reindex_action, index: index, action_end: Time.zone.now - 7.days - 1.minute)
      end

      create_list(:postgres_index, 2).each_with_index do |index, i|
        create(:postgres_index_bloat_estimate, index: index, bloat_size_bytes: 1.megabyte * i)
        create(:reindex_action, index: index, action_end: Time.zone.now)
      end

      expected = Gitlab::Database::PostgresIndexBloatEstimate.where(identifier: not_recently_reindexed.map(&:identifier)).map(&:index).map(&:identifier).sort

      expect(subject.map(&:identifier).sort).to eq(expected)
    end
  end
end