Welcome to mirror list, hosted at ThFree Co, Russian Federation.

database.rb « schema_validation « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dfc845f0b44c39ee84d246046cfbfc83ca588319 (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
# frozen_string_literal: true

module Gitlab
  module Database
    module SchemaValidation
      class Database
        def initialize(connection)
          @connection = connection
        end

        def fetch_index_by_name(index_name)
          index_map[index_name]
        end

        def indexes
          index_map.values
        end

        private

        def index_map
          @index_map ||=
            fetch_indexes.transform_values! do |index_stmt|
              Index.new(PgQuery.parse(index_stmt).tree.stmts.first.stmt.index_stmt)
            end
        end

        attr_reader :connection

        def fetch_indexes
          sql = <<~SQL
            SELECT indexname, indexdef
            FROM pg_indexes
            WHERE indexname NOT LIKE '%_pkey' AND schemaname IN ('public', 'gitlab_partitions_static');
          SQL

          @fetch_indexes ||= connection.exec_query(sql).rows.to_h
        end
      end
    end
  end
end