Class: HerokuCLI::PG

Inherits:
Base
  • Object
show all
Defined in:
lib/heroku_cli/pg.rb,
lib/heroku_cli/pg/database.rb

Overview

manage postgresql databases

Defined Under Namespace

Classes: Database

Instance Attribute Summary

Attributes inherited from Base

#application

Instance Method Summary collapse

Methods inherited from Base

#heroku, #initialize

Constructor Details

This class inherits a constructor from HerokuCLI::Base

Instance Method Details

#connection_url(database) ⇒ Object

Get a remote connection url for a database



51
52
53
54
# File 'lib/heroku_cli/pg.rb', line 51

def connection_url(database)
  url_name = database.url_name
  (heroku "config:get #{url_name}").strip
end

#create_follower(database, options = {}) ⇒ Object

create a follower database



29
30
31
32
# File 'lib/heroku_cli/pg.rb', line 29

def create_follower(database, options = {})
  plan = options.delete(:plan) || database.plan
  heroku "addons:create heroku-postgresql:#{plan}", "--follow #{database.resource_name}"
end

#create_fork(database, options = {}) ⇒ Object

Create a fork database



23
24
25
26
# File 'lib/heroku_cli/pg.rb', line 23

def create_fork(database, options = {})
  plan = options.delete(:plan) || database.plan
  heroku "addons:create heroku-postgresql:#{plan}", "--fork #{database.resource_name}"
end

#destroy(database) ⇒ Object



56
57
58
59
# File 'lib/heroku_cli/pg.rb', line 56

def destroy(database)
  raise "Cannot destroy #{application.name} main database" if database.main?
  heroku "addons:destroy #{database.url_name} -c #{application.name}"
end

#followersObject

Returns an array of allow follower databases



72
73
74
# File 'lib/heroku_cli/pg.rb', line 72

def followers
  info.find_all(&:follower?)
end

#forksObject

Returns an array of allow forks databases



67
68
69
# File 'lib/heroku_cli/pg.rb', line 67

def forks
  info.find_all(&:fork?)
end

#infoObject

show database information



7
8
9
10
11
12
13
14
15
16
# 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

#mainObject

Return the main database



62
63
64
# File 'lib/heroku_cli/pg.rb', line 62

def main
  info.find(&:main?)
end

#promote(database, wait: false) ⇒ Object

sets DATABASE as your DATABASE_URL



43
44
45
46
47
48
# File 'lib/heroku_cli/pg.rb', line 43

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

#reloadObject



18
19
20
# File 'lib/heroku_cli/pg.rb', line 18

def reload
  @info = nil
end

#un_follow(database, wait: false) ⇒ Object

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



35
36
37
38
39
40
# File 'lib/heroku_cli/pg.rb', line 35

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

#waitObject

blocks until database is available



77
78
79
# File 'lib/heroku_cli/pg.rb', line 77

def wait
  heroku 'pg:wait'
end

#wait_for(database, wait_time = 10) ⇒ Object

blocks until database is available but waits until pg:info actually says it is ready



82
83
84
85
86
87
88
89
90
91
# File 'lib/heroku_cli/pg.rb', line 82

def wait_for(database, wait_time = 10)
  until database.available?
    puts "...wait #{wait_time} seconds for DB to change status to available"
    sleep wait_time
    database.reload
    puts database
  end

  puts 'Database available!'
end

#wait_for_follow_fork_transformation(database) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/heroku_cli/pg.rb', line 93

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