Class: Azuki::Command::Pg

Inherits:
Base
  • Object
show all
Includes:
Helpers::AzukiPostgresql
Defined in:
lib/azuki/command/pg.rb

Overview

manage azuki-postgresql databases

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods included from Helpers::AzukiPostgresql

#app_attachments, #app_config_vars, #find_database_url_real_attachment, #forget_config!, #hpg_addon_name, #hpg_databases, #hpg_resolve, #hpg_translate_fork_and_follow, #match_attachments_by_name, #resource_url

Methods included from 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

Methods inherited from Base

#api, #app, #azuki, #initialize, namespace

Constructor Details

This class inherits a constructor from Azuki::Command::Base

Instance Method Details

#credentialsObject

pg:credentials DATABASE

Display the DATABASE credentials.

--reset  # Reset credentials on the specified database.


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/azuki/command/pg.rb', line 161

def credentials
  unless db = shift_argument
    error("Usage: azuki pg:credentials DATABASE\nMust specify DATABASE to display credentials.")
  end
  validate_arguments!

  attachment = hpg_resolve(db)

  if options[:reset]
    action "Resetting credentials for #{attachment.display_name}" do
      hpg_client(attachment).rotate_credentials
    end
    if attachment.primary_attachment?
      forget_config!
      attachment = hpg_resolve(db)
      action "Promoting #{attachment.display_name}" do
        hpg_promote(attachment.url)
      end
    end
  else
    uri = URI.parse( attachment.url )
    display "Connection info string:"
    display "   \"dbname=#{uri.path[1..-1]} host=#{uri.host} port=#{uri.port || 5432} user=#{uri.user} password=#{uri.password} sslmode=require\""
    display "Connection URL:"
    display "    " + attachment.url

  end
end

#indexObject

pg

List databases for an app



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/azuki/command/pg.rb', line 15

def index
  validate_arguments!

  if hpg_databases_with_info.empty?
    display("#{app} has no azuki-postgresql databases.")
  else
    hpg_databases_with_info.keys.sort.each do |name|
      display_db name, hpg_databases_with_info[name]
    end
  end
end

#infoObject

pg:info [DATABASE]

-x, --extended  # Show extended information

Display database information

If DATABASE is not specified, displays all databases



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/azuki/command/pg.rb', line 35

def info
  db = shift_argument
  validate_arguments!

  if db
    attachment = hpg_resolve(db)
    display_db attachment.display_name, hpg_info(attachment, options[:extended])
  else
    index
  end
end

#promoteObject

pg:promote DATABASE

Sets DATABASE as your DATABASE_URL



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/azuki/command/pg.rb', line 51

def promote
  unless db = shift_argument
    error("Usage: azuki pg:promote DATABASE\nMust specify DATABASE to promote.")
  end
  validate_arguments!

  attachment = hpg_resolve(db)

  action "Promoting #{attachment.display_name} to DATABASE_URL" do
    hpg_promote(attachment.url)
  end
end

#psqlObject

pg:psql [DATABASE]

Open a psql shell to the database

defaults to DATABASE_URL databases if no DATABASE is specified



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/azuki/command/pg.rb', line 70

def psql
  attachment = hpg_resolve(shift_argument, "DATABASE_URL")
  validate_arguments!

  uri = URI.parse( attachment.url )
  begin
    ENV["PGPASSWORD"] = uri.password
    ENV["PGSSLMODE"]  = 'require'
    exec "psql -U #{uri.user} -h #{uri.host} -p #{uri.port || 5432} #{uri.path[1..-1]}"
  rescue Errno::ENOENT
    output_with_bang "The local psql command could not be located"
    output_with_bang "For help installing psql, see http://devcenter.azukiapp.com/articles/local-postgresql"
    abort
  end
end

#resetObject

pg:reset DATABASE

Delete all data in DATABASE



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/azuki/command/pg.rb', line 90

def reset
  unless db = shift_argument
    error("Usage: azuki pg:reset DATABASE\nMust specify DATABASE to reset.")
  end
  validate_arguments!

  attachment = hpg_resolve(db) unless db == "SHARED_DATABASE"
  return unless confirm_command

  if db == "SHARED_DATABASE"
    action("Resetting SHARED_DATABASE") { azuki.database_reset(app) }
  else
    action("Resetting #{attachment.display_name}") do
      hpg_client(attachment).reset
    end
  end
end

#unfollowObject

pg:unfollow REPLICA

stop a replica from following and make it a read/write database



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

def unfollow
  unless db = shift_argument
    error("Usage: azuki pg:unfollow REPLICA\nMust specify REPLICA to unfollow.")
  end
  validate_arguments!

  replica = hpg_resolve(db)
  replica_info = hpg_info(replica)

  unless replica_info[:following]
    error("#{replica.display_name} is not following another database.")
  end
  origin_url = replica_info[:following]
  origin_name = database_name_from_url(origin_url)

  output_with_bang "#{replica.display_name} will become writable and no longer"
  output_with_bang "follow #{origin_name}. This cannot be undone."
  return unless confirm_command

  action "Unfollowing #{replica.display_name}" do
    hpg_client(replica).unfollow
  end
end

#waitObject

pg:wait [DATABASE]

monitor database creation, exit when complete

defaults to all databases if no DATABASE is specified



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/azuki/command/pg.rb', line 142

def wait
  db = shift_argument
  validate_arguments!

  if db
    wait_for hpg_resolve(db)
  else
    hpg_databases.values.each do |attach|
      wait_for(attach)
    end
  end
end