Module: Heroku::Helpers::HerokuPostgresql

Extended by:
Heroku::Helpers, HerokuPostgresql
Included in:
Command::Addons, Command::Pg, Command::Pgbackups, HerokuPostgresql
Defined in:
lib/heroku/helpers/heroku_postgresql.rb

Defined Under Namespace

Classes: Attachment

Instance Method Summary collapse

Methods included from Heroku::Helpers

action, ask, confirm, confirm_billing, confirm_command, create_git_remote, deprecate, display, display_header, display_object, display_row, display_table, error, error_with_failure, error_with_failure=, extended, extended_into, fail, format_bytes, format_date, format_error, format_with_bang, get_terminal_environment, git, has_git?, home_directory, host_name, hprint, hputs, included, included_into, json_decode, json_encode, launchy, line_formatter, longest, output_with_bang, quantify, redisplay, retry_on_exception, run_command, running_on_a_mac?, running_on_windows?, set_buffer, shell, spinner, status, string_distance, styled_array, styled_error, styled_hash, styled_header, suggestion, time_ago, truncate, with_tty

Instance Method Details

#app_attachmentsObject



47
48
49
# 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_varsObject



43
44
45
# 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_attachmentObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 77

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_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!Object



71
72
73
74
75
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 71

def forget_config!
  @hpg_databases   = nil
  @app_config_vars = nil
  @app_attachments = nil
end

#hpg_addon_nameObject



35
36
37
38
39
40
41
# 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_databasesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

  return @hpg_databases
end

#hpg_resolve(name, default = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 100

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

#hpg_translate_fork_and_follow(addon, config) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 123

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)
          if attachment.starter_plan?
            error("#{opt.tr 'f', 'F'} is only available on production databases.")
          end
          argument_url = attachment.url
        end

        config[opt] = argument_url
      end
    end
  end
end

#match_attachments_by_name(name) ⇒ Object



94
95
96
97
98
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 94

def match_attachments_by_name(name)
   return [] if name.empty?
   return [name] if hpg_databases[name]
   hpg_databases.keys.grep(%r{#{ name }}i)
end

#resource_url(resource) ⇒ Object



67
68
69
# File 'lib/heroku/helpers/heroku_postgresql.rb', line 67

def resource_url(resource)
  api.get_resource(resource).body['value']
end