Class: Tol::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/tol/database.rb

Instance Method Summary collapse

Instance Method Details

#download(heroku_app) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/tol/database.rb', line 36

def download(heroku_app)
  puts Rainbow("Downloading database for #{Rainbow(heroku_app).underline}").foreground(:green)

  puts Rainbow("Step 1. Detecting local database settings.").foreground(:yellow)
  @settings = Tol::RailsApp.new.database_settings["development"]

  choose do |menu|
    puts Rainbow("Step 2. Which version of the database should I download?").foreground(:green)
    
    menu.prompt = "Please pick up database version?"
    
    local_file = "/tmp/#{heroku_app}.dump"
    if File.exists?(local_file)
      downloaded_at = File.mtime(local_file).strftime("%b %e, %l:%M %p")
      menu.choice "Local File (Fastest) - downloaded at #{downloaded_at}"
    end

    menu.choice "Most Recent Snapshot (Fast)" do
      puts Rainbow("... Downloading. Please wait.").foreground(:yellow)
      url = ""
      Bundler.with_clean_env do
        url = `heroku pg:backups public-url --app #{heroku_app}`
      end
      download = `curl -o /tmp/#{heroku_app}.dump '#{url}' > /dev/null 2>&1`
    end

    menu.choice "New Snapshot (Slowest)" do
      puts Rainbow("... Capturing database on Heroku. Please wait").foreground(:yellow)
      db = ""
      Bundler.with_clean_env do
        db = `heroku pg:backups capture --app #{heroku_app}`
      end

      puts Rainbow("... Downloading. Please wait.").foreground(:yellow)
      url = ""
      Bundler.with_clean_env do
        url = `heroku pg:backups public-url --app #{heroku_app}`
      end
      
      download = `curl -o /tmp/#{heroku_app}.dump '#{url}' > /dev/null 2>&1`
    end
  end
  
  puts Rainbow("Step 3. Importing Database.").foreground(:yellow)
  puts "-> drop old database"
  dropdb           = "dropdb"
  dropdb          += " -h #{@settings['host']}"      if @settings["host"]
  dropdb          += " -U #{@settings['username']}"  if @settings["username"]
  dropdb           = "PGPASSWORD=#{@settings['password']} " +
                     dropdb                          if @settings["password"]
  dropdb          += " #{@settings['database']}"
  drop             = `/bin/bash -c '#{dropdb}'`

  puts "-> recreate old database"
  createdb         = "createdb"
  createdb        += " -h #{@settings['host']}"      if @settings["host"]
  createdb        += " -U #{@settings['username']}"  if @settings["username"]
  createdb         = "PGPASSWORD=#{@settings['password']} " +
                     createdb                        if @settings["password"]
  createdb        += " #{@settings['database']}"
  create           = `/bin/bash -c '#{createdb}'`

  puts "-> restore from file"
  restore_command  = "pg_restore --verbose --clean --no-acl --no-owner"
  restore_command += " -d #{@settings['database']}"
  restore_command += " -h #{@settings['host']}"      if @settings["host"]
  restore_command += " -U #{@settings['username']}"  if @settings["username"]
  restore_command  = "PGPASSWORD=#{@settings['password']} " +
                     restore_command                 if @settings["password"]
  restore_command += " /tmp/#{heroku_app}.dump > /dev/null 2>&1"
  restore          = `/bin/bash -c '#{restore_command}'`

  puts Rainbow("DONE").foreground(:green)
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tol/database.rb', line 10

def run
  puts "Identifying Heroku application"
  apps = Tol::Heroku.new.list_of_applications
  
  if apps.length == 0
    puts Rainbow("No Heroku apps found").foreground(:red)
    puts "Add your remotes to .git/config"
    # TODO: Automatically add remotes
  elsif apps.length == 1
    download(apps[0])
  else
    puts Rainbow("Multiple Heroku apps found").foreground(:green)

    choose do |menu|
      menu.prompt = "Which database should I download?"
      apps.each do |app|
        menu.choice app do
          download(app)
        end
      end

      menu.choice "None"
    end
  end
end