class Heroku::Helpers::HerokuPostgresql::Resolver

Attributes

api[R]
app_name[R]

Public Class Methods

new(app_name, api) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 88
def initialize(app_name, api)
  @app_name = app_name
  @api = api
end

Public Instance Methods

all_databases() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 103
def all_databases
  hpg_databases
end
app_config_vars() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 125
def app_config_vars
  protect_missing_app
  @app_config_vars ||= api.get_config_vars(app_name).body
end
database_name_from_url(url) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 107
def database_name_from_url(url)
  vars = app_config_vars.reject {|key,value| key == 'DATABASE_URL'}
  if var = vars.invert[url]
    var.gsub(/_URL$/, '')
  else
    uri = URI.parse(url)
    "Database on #{uri.host}:#{uri.port || 5432}#{uri.path}"
  end
end
hpg_addon_name() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 117
def hpg_addon_name
  if ENV['SHOGUN']
    "shogun-#{ENV['SHOGUN']}"
  else
    ENV['HEROKU_POSTGRESQL_ADDON_NAME'] || 'heroku-postgresql'
  end
end
resolve(identifier, default=nil) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 93
def resolve(identifier, default=nil)
  if identifier =~ /::/
    @app_name, db_name = identifier.split('::')
  else
    db_name = identifier
  end

  hpg_resolve(db_name, default)
end

Private Instance Methods

app_attachments() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 139
def app_attachments
  protect_missing_app
  @app_attachments ||= api.get_attachments(app_name).body.map { |raw| Attachment.new(raw) }
end
find_database_url_real_attachment() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 171
def find_database_url_real_attachment
  raw_primary_db_url = app_config_vars['DATABASE_URL']
  return unless raw_primary_db_url

  primary_db_url = raw_primary_db_url.split("?").first
  return unless primary_db_url && !primary_db_url.empty?

  real_config = app_attachments.map { |a|
    [a.config_var, a.url]
  }.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 165
def forget_config!
  @hpg_databases   = nil
  @app_config_vars = nil
  @app_attachments = nil
end
hpg_databases() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 144
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 ]

  # TODO: don't bother doing this if DATABASE_URL is already present in hash!
  if !@hpg_databases.key?('DATABASE_URL') && find_database_url_real_attachment
    @hpg_databases['DATABASE_URL'] = find_database_url_real_attachment
  end

  return @hpg_databases
end
hpg_resolve(name, default=nil) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 198
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
  candidates = match_attachments_by_name(name)
  if default && name.empty? && app_config_vars[default]
    found_attachment = hpg_databases[default]
  elsif candidates.size == 1
    found_attachment = hpg_databases[candidates.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
match_attachments_by_name(name) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 192
def match_attachments_by_name(name)
   return [] if name.empty?
   return [name] if hpg_databases[name]
   hpg_databases.keys.grep(%r{#{ name }}i)
end
protect_missing_app() click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 132
def protect_missing_app
  # in the case where --app was left out, AND app::db shorthand was not used, AND no app autodetect
  unless app_name
    error("No app specified.\nRun this command from an app folder or specify which app to use with --app APP.")
  end
end
resource_url(resource) click to toggle source
# File lib/heroku/helpers/heroku_postgresql.rb, line 161
def resource_url(resource)
  api.get_resource(resource).body['value']
end