Created
June 6, 2018 13:51
-
-
Save cameronbarker/7be4c64a76f16024330e89075a201f4e to your computer and use it in GitHub Desktop.
Some helpful heroku database tasks
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
require "uri" | |
namespace :db do | |
desc "reset but from the migrations not the schema" | |
task restart: :environment do | |
Rake::Task["db:drop"].invoke | |
Rake::Task["db:create"].invoke | |
Rake::Task["db:migrate"].invoke | |
Rake::Task["db:seed"].invoke | |
end | |
task clear: :environment do | |
Rake::Task["db:drop"].invoke | |
Rake::Task["db:create"].invoke | |
Rake::Task["db:migrate"].invoke | |
end | |
desc "Dump Heroku production DB and restore it locally." | |
task import_from_heroku: [ :environment, :create ] do | |
c = Rails.configuration.database_configuration[Rails.env] | |
Bundler.with_clean_env do | |
puts "[1/4] Fetching DB password from Heroku" | |
db_url = `heroku config:get DATABASE_URL` | |
db = URI.parse(db_url.strip) | |
file = "tmp/heroku_db.dump" | |
puts "[2/4] Dumping DB" | |
`PGPASSWORD=#{db.password} pg_dump -h #{db.host} -U #{db.user} -d #{db.path[1..-1]} -F c -b -v -f #{file}` | |
puts "[3/4] Restoring dump on local database" | |
`pg_restore --clean --verbose --no-acl --no-owner -h #{c["host"] || 'localhost'} -d #{c["database"]} #{file}` | |
puts "[4/4] Removing local backup" | |
`rm #{file}` | |
puts "Done." | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment