Module: HerokuDbSync::TaskHelper

Defined in:
lib/heroku-db-sync/task_helper.rb

Class Method Summary collapse

Class Method Details

.databaseObject



51
52
53
# File 'lib/heroku-db-sync/task_helper.rb', line 51

def self.database
  @arguments[:database] || db_config[:database]  || raise("no database configured")
end

.db_configObject



55
56
57
# File 'lib/heroku-db-sync/task_helper.rb', line 55

def self.db_config
  @_db_config ||= Rails.configuration.database_configuration[Rails.env].with_indifferent_access
end

.hostObject



43
44
45
# File 'lib/heroku-db-sync/task_helper.rb', line 43

def self.host
  @arguments[:host] || db_config[:host] || raise("no host configured")
end

.perform_sync(arguments) ⇒ Object



5
6
7
8
9
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
35
36
37
38
39
40
41
# File 'lib/heroku-db-sync/task_helper.rb', line 5

def self.perform_sync arguments
  Bundler.with_clean_env do
    @arguments = arguments
    from_remote = arguments[:from_remote] || 'production'
    to_remote = arguments[:to_remote]
    puts "(1/4) capturing #{from_remote} database snapshot..."
    run "heroku pg:backups capture --remote #{from_remote}"

    if to_remote
      puts "(2/4) fetching backup url from remote #{from_remote}"
      backup_url = runr("heroku pg:backups public-url -q --remote #{from_remote}")
      print "(3/4) backing up #{to_remote} and restoring it from snapshot from #{from_remote} ... "
      run "heroku pg:backups capture --remote #{to_remote}"
      puts "captured. restoring .."
      restore_cmd = "heroku pg:backups restore DATABASE '#{backup_url}' --remote #{to_remote}"
      puts restore_cmd
      run restore_cmd   # use 'system' as heroku prompts for confirmation of db restore.
      puts "(4/4) restarting remote #{to_remote}"
      run "heroku restart --remote #{to_remote}"
    else
      dumpfile = 'latest.dump'
      puts "(2/4) downloading snapshot..."
      run "curl -o #{dumpfile} \`heroku pg:backups public-url -q --remote #{from_remote}\`"
      puts "(3/4) restoring snapshot to #{host} database #{database} ... "
      `pg_restore --verbose --clean --no-acl --no-owner -h #{host} -U #{user} -d #{database} #{dumpfile}`
      keep = arguments[:keepdump] || false
      unless keep
        puts "(4/4) cleaning up..."
        `rm #{dumpfile}`
      else
        puts "(4/4) keeping #{dumpfile}"
      end
    end

puts "Success! Ah, that was exhausting, pop a beer!"
  end
end

.userObject



47
48
49
# File 'lib/heroku-db-sync/task_helper.rb', line 47

def self.user
  @arguments[:username] || db_config[:username] || raise("no user configured")
end