aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/database_configurations.rb
blob: 09aef62753df434f8469d3ac8edef95405f0b6c6 (plain) (blame)
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
# frozen_string_literal: true

module ActiveRecord
  module DatabaseConfigurations # :nodoc:
    class DatabaseConfig
      attr_reader :env_name, :spec_name, :config

      def initialize(env_name, spec_name, config)
        @env_name = env_name
        @spec_name = spec_name
        @config = config
      end
    end

    # Selects the config for the specified environment and specification name
    #
    # For example if passed :development, and :animals it will select the database
    # under the :development and :animals configuration level
    def self.config_for_env_and_spec(environment, specification_name, configs = ActiveRecord::Base.configurations) # :nodoc:
      configs_for(environment, configs).find do |db_config|
        db_config.spec_name == specification_name
      end
    end

    # Collects the configs for the environment passed in.
    #
    # If a block is given returns the specification name and configuration
    # otherwise returns an array of DatabaseConfig structs for the environment.
    def self.configs_for(env, configs = ActiveRecord::Base.configurations, &blk) # :nodoc:
      env_with_configs = db_configs(configs).select do |db_config|
        db_config.env_name == env
      end

      if block_given?
        env_with_configs.each do |env_with_config|
          yield env_with_config.spec_name, env_with_config.config
        end
      else
        env_with_configs
      end
    end

    # Given an env, spec and config creates DatabaseConfig structs with
    # each attribute set.
    def self.walk_configs(env_name, spec_name, config) # :nodoc:
      if config["database"] || config["url"] || env_name == "default"
        DatabaseConfig.new(env_name, spec_name, config)
      else
        config.each_pair.map do |sub_spec_name, sub_config|
          walk_configs(env_name, sub_spec_name, sub_config)
        end
      end
    end

    # Walks all the configs passed in and returns an array
    # of DatabaseConfig structs for each configuration.
    def self.db_configs(configs = ActiveRecord::Base.configurations) # :nodoc:
      configs.each_pair.flat_map do |env_name, config|
        walk_configs(env_name, "primary", config)
      end
    end
  end
end