class Object

Public Instance Methods

database_config_defaults() click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 172
def database_config_defaults
  { 'host' => 'localhost' }
end
env_variables_loader_code(env) click to toggle source

Requires necessary gems (Dotenv, Figaro, …) if present and loads environment variables for configurations

# File lib/capistrano3/tasks/postgres.rb, line 221
  def env_variables_loader_code(env)
    <<-RUBY.strip
      begin
        require 'dotenv'
        Dotenv.load(File.expand_path('.env.#{env}'), File.expand_path('.env'))
      rescue LoadError
      end

      begin
        require 'figaro'
        config = File.expand_path('../config/application.yml', __FILE__)

        Figaro.application = Figaro::Application.new(environment: '#{env}', path: config)
        Figaro.load
      rescue LoadError
      end
    RUBY
  end
grab_local_database_config() click to toggle source

Grabs local database config before importing dump

# File lib/capistrano3/tasks/postgres.rb, line 140
def grab_local_database_config
  return if fetch(:postgres_local_database_config)
  on roles(fetch(:postgres_role)) do |role|
    run_locally do
      env = 'development'
      preload_env_variables(env)
      yaml_content = ERB.new(capture 'cat config/database.yml').result
      set_postgres_database_config(yaml_content, env, :postgres_local_database_config)
    end
  end
end
grab_remote_database_config() click to toggle source

Grabs remote database config before creating dump

# File lib/capistrano3/tasks/postgres.rb, line 153
  def grab_remote_database_config
    return if fetch(:postgres_remote_database_config)
    on roles(fetch(:postgres_role)) do |role|
      within release_path do
        env = fetch(:postgres_env).to_s.downcase
        filename = "#{deploy_to}/current/config/database.yml"
        eval_yaml_with_erb = <<-RUBY.strip
          #{env_variables_loader_code(env)}
          require 'erb'
          puts ERB.new(File.read('#{filename}')).result
        RUBY

        capture_config_cmd = "ruby -e \"#{eval_yaml_with_erb}\""
        yaml_content = test('ruby -v') ? capture(capture_config_cmd) : capture(:bundle, :exec, capture_config_cmd)
        set_postgres_database_config(yaml_content, env, :postgres_remote_database_config)
      end
    end
  end
load_env_variables_with_dotenv(env) click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 194
def load_env_variables_with_dotenv(env)
  Dotenv.load(
    File.expand_path('.env.local'),
    File.expand_path(".env.#{env}"),
    File.expand_path('.env')
  )
end
load_env_variables_with_figaro(env) click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 202
def load_env_variables_with_figaro(env)
  config = 'config/application.yml'

  Figaro.application = Figaro::Application.new(environment: env, path: config)
  Figaro.load
end
preload_env_variables(env) click to toggle source

Load environment variables for configurations. Useful for such gems as Dotenv, Figaro, etc.

# File lib/capistrano3/tasks/postgres.rb, line 184
def preload_env_variables(env)
  safely_require_gems('dotenv', 'figaro')

  if defined?(Dotenv)
    load_env_variables_with_dotenv(env)
  elsif defined?(Figaro)
    load_env_variables_with_figaro(env)
  end
end
safely_require_gems(*gem_names) click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 209
def safely_require_gems(*gem_names)
  gem_names.each do |name|
    begin
      require name
    rescue LoadError
      # Ignore if gem doesn't exist
    end
  end
end
set_postgres_database_config(yaml_content, env, key) click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 176
def set_postgres_database_config(yaml_content, env, key)
  database_config = YAML::load(yaml_content)[env]
  database_config = database_config[fetch(:postgres_database)] if fetch(:postgres_database)
  set key, database_config_defaults.merge(database_config)
end
user_option(config) click to toggle source
# File lib/capistrano3/tasks/postgres.rb, line 131
def user_option(config)
  if config['user'] || config['username']
    "-U #{config['user'] || config['username']}"
  else
    '' # assume ident auth is being used
  end
end