class HerokuCLI::PG

manage postgresql databases

Public Instance Methods

create_follower(database, options = {}) click to toggle source

create a follower database

# File lib/heroku_cli/pg.rb, line 23
def create_follower(database, options = {})
  plan = options.delete(:plan) || database.plan
  heroku "addons:create heroku-postgresql:#{plan} --follow #{database.resource_name}"
end
destroy(database) click to toggle source
# File lib/heroku_cli/pg.rb, line 44
def destroy(database)
  raise "Cannot destroy #{application.name} main database" if database.main?
  heroku "addons:destroy #{database.url_name} -c #{application.name}"
end
followers() click to toggle source

Returns an array of allow follower databases

# File lib/heroku_cli/pg.rb, line 60
def followers
  info.find_all(&:follower?)
end
forks() click to toggle source

Returns an array of allow forks databases

# File lib/heroku_cli/pg.rb, line 55
def forks
  info.find_all(&:fork?)
end
info() click to toggle source

show database information

# File lib/heroku_cli/pg.rb, line 7
def info
  @info ||= begin
    heroku('pg:info').split("===").reject(&:empty?).map do |stdout|
      next if stdout.nil? || stdout.length.zero?
      stdout = stdout.split("\n")
      stdout[0] = "===#{stdout[0]}"
      Database.new(stdout, self)
    end
  end
end
main() click to toggle source

Return the main database

# File lib/heroku_cli/pg.rb, line 50
def main
  info.find(&:main?)
end
promote(database, wait: false) click to toggle source

sets DATABASE as your DATABASE_URL

# File lib/heroku_cli/pg.rb, line 37
def promote(database, wait: false)
  raise "Database already main #{database.name}" if database.main?

  un_follow(database, wait: wait) if database.follower?
  heroku "pg:promote #{database.resource_name}"
end
reload() click to toggle source
# File lib/heroku_cli/pg.rb, line 18
def reload
  @info = nil
end
un_follow(database, wait: false) click to toggle source

Remove the following of a database and put DB into write mode

# File lib/heroku_cli/pg.rb, line 29
def un_follow(database, wait: false)
  raise "Not a following database #{database.name}" unless database.follower?

  heroku "pg:unfollow #{database.url_name} -c #{application.name}"
  wait_for_follow_fork_transformation(database) if wait
end
wait() click to toggle source

blocks until database is available

# File lib/heroku_cli/pg.rb, line 65
def wait
  heroku 'pg:wait'
end
wait_for_follow_fork_transformation(database) click to toggle source
# File lib/heroku_cli/pg.rb, line 69
def wait_for_follow_fork_transformation(database)
  while database.follower? do
    puts "...wait 10 seconds for DB to change from follower to fork"
    sleep 10
    database.reload
    puts database
  end
end