summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/database/schema_validation/indexes_spec.rb
blob: 4351031a4b4456ce8166398b4b15b210d8b41533 (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
51
52
53
54
55
56
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Database::SchemaValidation::Indexes, feature_category: :database do
  let(:structure_file_path) { Rails.root.join('spec/fixtures/structure.sql') }
  let(:database_indexes) do
    [
      ['wrong_index', 'CREATE UNIQUE INDEX wrong_index ON public.table_name (column_name)'],
      ['extra_index', 'CREATE INDEX extra_index ON public.table_name (column_name)'],
      ['index', 'CREATE UNIQUE INDEX "index" ON public.achievements USING btree (namespace_id, lower(name))']
    ]
  end

  let(:database_name) { 'main' }

  let(:database_model) { Gitlab::Database.database_base_models[database_name] }

  let(:connection) { database_model.connection }

  let(:query_result) { instance_double('ActiveRecord::Result', rows: database_indexes) }

  let(:database) { Gitlab::Database::SchemaValidation::Database.new(connection) }
  let(:structure_file) { Gitlab::Database::SchemaValidation::StructureSql.new(structure_file_path) }

  subject(:schema_validation) { described_class.new(structure_file, database) }

  before do
    allow(connection).to receive(:exec_query).and_return(query_result)
  end

  describe '#missing_indexes' do
    it 'returns missing indexes' do
      missing_indexes = %w[
        missing_index
        index_namespaces_public_groups_name_id
        index_on_deploy_keys_id_and_type_and_public
        index_users_on_public_email_excluding_null_and_empty
      ]

      expect(schema_validation.missing_indexes).to match_array(missing_indexes)
    end
  end

  describe '#extra_indexes' do
    it 'returns extra indexes' do
      expect(schema_validation.extra_indexes).to match_array(['extra_index'])
    end
  end

  describe '#wrong_indexes' do
    it 'returns wrong indexes' do
      expect(schema_validation.wrong_indexes).to match_array(['wrong_index'])
    end
  end
end