module Heroku::Helpers::HerokuPostgresql

Public Instance Methods

app_attachments() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 47
def app_attachments
  @app_attachments ||= api.get_attachments(app).body.map { |raw| Attachment.new(raw) }
end
app_config_vars() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 43
def app_config_vars
  @app_config_vars ||= api.get_config_vars(app).body
end
find_database_url_real_attachment() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 87
def find_database_url_real_attachment
  primary_db_url = app_config_vars['DATABASE_URL']
  return unless primary_db_url

  real_config = app_config_vars.detect {|k,v| k != 'DATABASE_URL' && v == primary_db_url }
  if real_config
    real = hpg_databases[real_config.first]
    real.primary_attachment! if real
    return real
  else
    return nil
  end
end
forget_config!() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 82
def forget_config!
  @hpg_databases   = nil
  @app_config_vars = nil
end
hpg_addon_name() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 35
def hpg_addon_name
  if ENV['SHOGUN']
    "shogun-#{ENV['SHOGUN']}"
  else
    ENV['HEROKU_POSTGRESQL_ADDON_NAME'] || 'heroku-postgresql'
  end
end
hpg_databases() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 51
def hpg_databases
  return @hpg_databases if @hpg_databases
  pairs = app_attachments.select {|att|
      att.addon == hpg_addon_name
    }.map { |att|
      [att.config_var, att]
    }
  @hpg_databases = Hash[ pairs ]

  if find_database_url_real_attachment
    @hpg_databases['DATABASE_URL'] = find_database_url_real_attachment
  end

  if app_config_vars['SHARED_DATABASE_URL']
    @hpg_databases['SHARED_DATABASE'] = Attachment.new({
      'config_var' => 'SHARED_DATABASE',
      'resource' => {
          'name'  => 'SHARED_DATABASE',
          'value' => app_config_vars['SHARED_DATABASE_URL'],
          'type'  => 'shared:database'
        }
      })
  end

  return @hpg_databases
end
hpg_resolve(name, default=nil) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 107
def hpg_resolve(name, default=nil)
  name = '' if name.nil?
  name = 'DATABASE_URL' if name == 'DATABASE'

  if hpg_databases.empty?
    error("Your app has no databases.")
  end

  found_attachment = nil
  canidates = match_attachments_by_name(name)
  if default && name.empty? && app_config_vars[default]
    found_attachment = hpg_databases[default]
  elsif canidates.size == 1
    found_attachment = hpg_databases[canidates.first]
  end

  if found_attachment.nil?
    error("Unknown database#{': ' + name unless name.empty?}. Valid options are: #{hpg_databases.keys.sort.join(", ")}")
  end

  return found_attachment
end
hpg_translate_fork_and_follow(addon, config) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 130
def hpg_translate_fork_and_follow(addon, config)
  if addon =~ /^#{hpg_addon_name}/
    %w[fork follow].each do |opt|
      if val = config[opt]
        unless val.is_a?(String)
          error("--#{opt} requires a database argument")
        end

        uri = URI.parse(val) rescue nil
        if uri && uri.scheme
          argument_url = uri.to_s
        else
          attachment = hpg_resolve(val)
          argument_url = attachment.url
        end

        config[opt] = argument_url
      end
    end
  end
end
match_attachments_by_name(name) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 101
def match_attachments_by_name(name)
   return [] if name.empty?
   return [name] if hpg_databases[name]
   hpg_databases.keys.grep(%r{#{ name }})
end
resource_url(resource) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 78
def resource_url(resource)
  api.get_resource(resource).body['value']
end

Private Instance Methods

hpg_promote(url) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 154
def hpg_promote(url)
  api.put_config_vars(app, "DATABASE_URL" => url)
end