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

dictionary.rb « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d963d6f288e32bcaf09728e53dc6734cf7baf6bc (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

module Gitlab
  module Database
    class Dictionary
      ALL_SCOPES = ['', 'views', 'deleted_tables'].freeze

      def self.entries(scope = '')
        @entries ||= {}
        @entries[scope] ||= Dir.glob(dictionary_path_globs(scope)).map do |file_path|
          dictionary = Entry.new(file_path)
          dictionary.validate!
          dictionary
        end
      end

      def self.any_entry(name)
        ALL_SCOPES.each do |scope|
          e = entry(name, scope)
          return e if e
        end

        nil
      end

      def self.entry(name, scope = '')
        entries(scope).find do |entry|
          entry.key_name == name
        end
      end

      private_class_method def self.dictionary_path_globs(scope)
        dictionary_paths.map { |path| Rails.root.join(path, scope, '*.yml') }
      end

      private_class_method def self.dictionary_paths
        ::Gitlab::Database.all_database_connections
          .values.map(&:db_docs_dir).uniq
      end

      class Entry
        def initialize(file_path)
          @file_path = file_path
          @data = YAML.load_file(file_path)
        end

        def name_and_schema
          [key_name, gitlab_schema.to_sym]
        end

        def table_name
          data['table_name']
        end

        def feature_categories
          data['feature_categories']
        end

        def view_name
          data['view_name']
        end

        def milestone
          data['milestone']
        end

        def gitlab_schema
          data['gitlab_schema']
        end

        def sharding_key
          data['sharding_key']
        end

        def desired_sharding_key
          data['desired_sharding_key']
        end

        def classes
          data['classes']
        end

        def allow_cross_to_schemas(type)
          data["allow_cross_#{type}"].to_a.map(&:to_sym)
        end

        def schema?(schema_name)
          gitlab_schema == schema_name.to_s
        end

        def key_name
          table_name || view_name
        end

        def validate!
          return true unless gitlab_schema.nil?

          raise(
            GitlabSchema::UnknownSchemaError,
            "#{file_path} must specify a valid gitlab_schema for #{key_name}. " \
            "See #{help_page_url}"
          )
        end

        private

        attr_reader :file_path, :data

        def help_page_url
          # rubocop:disable Gitlab/DocUrl -- link directly to docs.gitlab.com, always
          'https://docs.gitlab.com/ee/development/database/database_dictionary.html'
          # rubocop:enable Gitlab/DocUrl
        end
      end
    end
  end
end