Module: HerokuDbSync::TaskHelper

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

Class Method Summary collapse

Class Method Details

.databaseObject



49
50
51
# File 'lib/heroku-db-sync/task_helper.rb', line 49

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

.db_configObject



53
54
55
# File 'lib/heroku-db-sync/task_helper.rb', line 53

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

.hostObject



41
42
43
# File 'lib/heroku-db-sync/task_helper.rb', line 41

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

.perform_sync(arguments) ⇒ Object



4
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
# File 'lib/heroku-db-sync/task_helper.rb', line 4

def self.perform_sync arguments
  @arguments = arguments
  from_remote = arguments[:from_remote] || 'production'
  to_remote = arguments[:to_remote]

  puts "(1/4) capturing #{from_remote} database snapshot..."
  run "heroku pgbackups:capture --expire --remote #{from_remote}"

  if to_remote
    puts "(2/4) fetching backup url from remote #{from_remote}"
    backup_url = runr("heroku pgbackups:url --remote #{from_remote}")
    print "(3/4) backing up #{to_remote} and restoring it to snapshot from #{from_remote} ... "
    run "heroku pgbackups:capture --expire --remote #{to_remote}"
    puts "captured. restoring .."
    restore_cmd = "heroku pgbackups: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 pgbackups:url --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

.userObject



45
46
47
# File 'lib/heroku-db-sync/task_helper.rb', line 45

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