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

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

require_relative '../popen'

# The purpose of this code is to set the migrations path
# for the Geo tracking database and the embedding database.
module Gitlab
  module Patch
    module DatabaseConfig
      extend ActiveSupport::Concern

      CommandExecutionError = Class.new(StandardError)

      def database_configuration
        super.to_h do |env, configs|
          parsed_config = parse_extra_config(configs)

          if Gitlab.ee?
            ee_databases = %w[embedding geo]

            ee_databases.each do |ee_db_name|
              next unless parsed_config.key?(ee_db_name)

              migrations_paths = Array(parsed_config[ee_db_name]['migrations_paths'])
              migrations_paths << File.join('ee', 'db', ee_db_name, 'migrate') if migrations_paths.empty?
              migrations_paths << File.join('ee', 'db', ee_db_name, 'post_migrate') unless ENV['SKIP_POST_DEPLOYMENT_MIGRATIONS']

              parsed_config[ee_db_name]['migrations_paths'] = migrations_paths.uniq
              parsed_config[ee_db_name]['schema_migrations_path'] = File.join('ee', 'db', ee_db_name, 'schema_migrations') if parsed_config[ee_db_name]['schema_migrations_path'].blank?
            end
          end

          [env, parsed_config]
        end
      end

      private

      def parse_extra_config(configs)
        command = configs.delete('config_command')
        return configs unless command.present?

        config_from_command = extra_config_from_command(command)
        return configs unless config_from_command.present?

        configs.deep_merge(config_from_command)
      end

      def extra_config_from_command(command)
        cmd = command.split(" ")
        output, exit_status = Gitlab::Popen.popen(cmd)

        if exit_status != 0
          raise CommandExecutionError,
            "database.yml: Execution of `#{command}` failed with exit code #{exit_status}. Output: #{output}"
        end

        YAML.safe_load(output).deep_stringify_keys
      rescue Psych::SyntaxError => e
        error_message = <<~MSG
          database.yml: Execution of `#{command}` generated invalid yaml.
          Error: #{e.problem} #{e.context} at line #{e.line} column #{e.column}
        MSG

        raise CommandExecutionError, error_message
      end
    end
  end
end