summaryrefslogtreecommitdiff
path: root/lib/tasks/database.rake
blob: f61e56f7399946c5678bca7ab1f7ab377fde3912 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
namespace :db do
  task :restore do
    config = Rails.configuration.database_configuration
    destination_db = config[Rails.env]["database"]

    newest_backup = Dir['db/backups/*.dump'].max_by { |file| File.mtime(file) }
    sh "PGPASSWORD=#{config[Rails.env]["password"]} pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{config[Rails.env]["username"]} -d #{destination_db} #{newest_backup}"
  end

  task :backup do
    sh "cap #{ENV['RAILS_ENV']} postgresql:backup"
  end

  task :restore_to_staging => :backup do
    sh "cap staging postgresql:restore"
  end
end