Heroku Rails Rake任务同步生产&本地数据库 [英] Heroku Rails Rake Task to Sync Production & Local DB

查看:182
本文介绍了Heroku Rails Rake任务同步生产&本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个rake任务,以便我可以简单地键入rake db:sync以更新我的本地数据库以匹配产品。



此解决方案利用Heroku团队提供的代码:
导入和导出Heroku具有PG备份的Postgres数据库



当我使用curl时--output /tmp/latest.dump#{url}我在我的最新的.dump文件:

 <?xml version =1.0encoding =UTF-8?> 
< Error>< Code> AuthorizationQueryParametersError< / Code>< Message>查询字符串认证版本4需要X-Amz算法,X-Amz-Credential,X-Amz-签名,X-Amz < / Message>< RequestId> 421FEFF763870123< / RequestId>< HostId> vIVr / ihmQiDgYIpdFFkuCgEP8Smvr2ks0wRkf89fJ8NfHfsBb92EVv40Q0NZuQIC< / HostId>< / Error>

以下是我使用的代码。

 #lib / tasks / db_sync.rake 
命名空间:db do
desc'将生产数据库拉到开发'
任务:同步=> [:backup,:dump,:restore]

任务:backup do
Bundler.with_clean_env {
puts'Backup started ...'
systemheroku pg :备份捕获--app YOUR_APP_NAME
puts'备份完成!'
}
结束

任务:dump do
dumpfile =#{Rails .root} /tmp/latest.dump
puts'获取网址和文件...'
Bundler.with_clean_env {$ b $ url =`heroku pg:backups public-url --app YOUR_APP_NAME | cat`
systemcurl --output#{dumpfile}#{url}
}
puts'Fetching complete!'
结束

任务:restore
dev = Rails.application.config.database_configuration ['development']
dumpfile =#{Rails.root} /tmp/latest.dump
将'PG_RESTORE开启数据库...'
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 Rails任务 -


  1. 备份数据库

  2. 恢复数据库

  3. 运行 rails控制台

  4. 尾部日志

  5. 运行迁移

  6. 部署

您当然主要在寻找前两项。



安装完成后,它希望您有两个 git remote 值设置为 staging 生产 development 不需要,因为它假定为您的本地计算机。



您可以从Heroku仪表板 - >(您的应用程序) - >设置 - >信息获取其他两种环境的git url

完成设置后,它就像

一样简单

 生产备份
开发恢复生产

代码非常简单,所以我建议您阅读它。但它基本上是通过获取公共URL并恢复它来完成您的代码尝试执行的操作。

I'm trying to create a rake task so that I can simply type "rake db:sync" in order to update my local DB to match production.

This solution leverages code provided by the Heroku team here: Importing and Exporting Heroku Postgres Databases with PG Backups

When I use curl --output /tmp/latest.dump #{url} I'm getting the following error in my latest.dump file:

<?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>

Here is the code I'm using.

    #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

解决方案

Check out the Parity gem. It offers several commands to do the following Heroku Rails tasks easily -

  1. Backup DB's
  2. Restore DB's
  3. Run rails console
  4. Tail logs
  5. Run migrations
  6. Deploy

You're of course primarily looking for the first two.

After installation, it expects that you have two git remote values set named staging and production. development isn't needed as it is assumed to be your local machine.

You can get the git url for the other two environments from your Heroku dashboard -> (your app) -> Settings -> Info

After you have that set up, it's as simple as

production backup
development restore production

The code is pretty simple, so I encourage you to read it. But it's essentially doing exactly what your rake code attempts to do by getting a public URL and restoring it.

这篇关于Heroku Rails Rake任务同步生产&amp;本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆