Skip to content

Instantly share code, notes, and snippets.

@robhurring robhurring/db.rake
Last active Aug 29, 2015

Embed
What would you like to do?
Rake task to sync heroku database locally
namespace :db do
desc 'sync with prod database'
task :sync, :backup_id do |t, args|
dump_file = Rails.root.join('tmp', 'latest.dump')
db_configs = YAML.load_file(Rails.root.join('config', 'database.yml'))
db_config = db_configs[Rails.env]
commands = [
"heroku pgbackups:capture --expire",
"curl -o #{dump_file} \`heroku pgbackups:url #{args[:backup_id]}\`",
"pg_restore --verbose --clean --no-acl --no-owner -U #{db_config['username']} -d #{db_config['database']} #{dump_file}",
"rm -f #{dump_file}"
]
puts "\033[36mDropping current db\033[0m"
Rake::Task["db:drop"].invoke
Rake::Task["db:create"].invoke
puts "\033[36mImporting latest DB from heroku\033[0m"
commands.each do |command|
STDERR.puts command
`#{command}`
end
puts "\033[36mMigrating...\033[0m"
Rake::Task["db:migrate"].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.