Skip to content

Instantly share code, notes, and snippets.

@knoxjeffrey
Created October 23, 2018 23:09
Show Gist options
  • Save knoxjeffrey/f6c0e5aa1ebda3a97ccafc4897ee4e5f to your computer and use it in GitHub Desktop.
Save knoxjeffrey/f6c0e5aa1ebda3a97ccafc4897ee4e5f to your computer and use it in GitHub Desktop.
Pull specified db to development
# rake db:pull[<environment to pull from>,<your local db username>]
# eg. rake db:pull[staging,postgres]
namespace :db do
desc 'Pull specified db to development'
task :pull, :environment, :dev_db_user do |_t, args|
environment = args[:environment].to_sym
dev_db_user = args[:dev_db_user]
dev = Rails.application.config.database_configuration['development']
dumpfile = "#{Rails.root}/tmp/latest.dump"
ssh = Rails.application.credentials[environment][:ssh]
ssh_port = Rails.application.credentials[environment][:ssh_port]
db_user = Rails.application.credentials[environment][:db_user]
db_password = Rails.application.credentials[environment][:db_password]
db_name = Rails.application.credentials[environment][:db_name]
db_host = Rails.application.credentials[environment][:db_host]
pg_command = Rails.application.credentials[:pg_command]
puts 'PG_DUMP on specified database...'
system "ssh #{ssh} -p #{ssh_port} 'PGPASSWORD=\"#{db_password}\" #{pg_command}/pg_dump -U #{db_user} #{db_name} -h #{db_host} -F t' > #{dumpfile}"
puts 'Pulled!'
puts 'PG_RESTORE on development database...'
system "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{dev_db_user} -d #{dev['database']} #{dumpfile}"
puts 'Done!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment