Module: Azuki::Helpers::AzukiPostgresql

Extended by:
Azuki::Helpers, AzukiPostgresql
Included in:
Command::Addons, Command::Pg, Command::Pgbackups, AzukiPostgresql
Defined in:
lib/azuki/helpers/azuki_postgresql.rb

Defined Under Namespace

Classes: Attachment

Instance Method Summary collapse

Methods included from Azuki::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, 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/azuki/helpers/azuki_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/azuki/helpers/azuki_postgresql.rb', line 43

def app_config_vars
  @app_config_vars ||= api.get_config_vars(app).body
end

#find_database_url_real_attachmentObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 88

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



82
83
84
85
86
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 82

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/azuki/helpers/azuki_postgresql.rb', line 35

def hpg_addon_name
  if ENV['SHOGUN']
    "shogun-#{ENV['SHOGUN']}"
  else
    ENV['AZUKI_POSTGRESQL_ADDON_NAME'] || 'azuki-postgresql'
  end
end

#hpg_databasesObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/azuki/helpers/azuki_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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 111

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 134

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



105
106
107
108
109
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 105

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



78
79
80
# File 'lib/azuki/helpers/azuki_postgresql.rb', line 78

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