我試圖創建一個Rake任務,以便我可以簡單地鍵入「rake db:sync」以更新我的本地數據庫以匹配生產。Heroku Rails Rake任務同步生產和本地數據庫
該解決方案利用由Heroku的團隊在這裏提供的代碼: Importing and Exporting Heroku Postgres Databases with PG Backups
當我使用捲曲--output /tmp/latest.dump#{URL}我得到我的latest.dump以下錯誤文件:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AuthorizationQueryParametersError</Code><Message>Query-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.</Message><RequestId>421FEFF763870123</RequestId><HostId>vlVr/ihmQiDgYIpdFFkuCgEP8Smvr2ks0wRkf89fJ8NfHfsBb92EVv40Q0NZuQIC</HostId></Error>
這是我正在使用的代碼。
#lib/tasks/db_sync.rake
namespace :db do
desc 'Pull production db to development'
task :sync => [:backup, :dump, :restore]
task :backup do
Bundler.with_clean_env {
puts 'Backup started...'
system "heroku pg:backups capture --app YOUR_APP_NAME"
puts 'Backup complete!'
}
end
task :dump do
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'Fetching url and file...'
Bundler.with_clean_env {
url = `heroku pg:backups public-url --app YOUR_APP_NAME | cat`
system "curl --output #{dumpfile} #{url}"
}
puts 'Fetching complete!'
end
task :restore do
dev = Rails.application.config.database_configuration['development']
dumpfile = "#{Rails.root}/tmp/latest.dump"
puts 'PG_RESTORE on development database...'
system "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{dev['username']} -d #{dev['database']} #{dumpfile}"
puts 'PG_RESTORE Complete!'
end
end
我打算試一試。學習如何在heroku中設置臨時環境是我的良好習慣,看起來比我預期的要容易。我仍然希望看到這個簡單問題的「耙」解決方案。 – woodenfox
工作就像一個魅力。儘管該解決方案確實需要我設置臨時環境。 – woodenfox
因此,如果您遵循代碼,它會在幾個類和幾個文件之間跳轉,但它的核心基本上只是將兩個命令拼合在一起以便恢復 - (1)查找公共URL並(2)將其恢復到任何環境。你可以將它們拼湊起來,並將它們粘在一個耙子任務中,雖然寶石是一個方便的軟件包,所以不確定這種方法的優點。祝你好運! – user2490003