Last active
August 29, 2015 13:58
-
-
Save robhurring/9995758 to your computer and use it in GitHub Desktop.
Rake task to sync heroku database locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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