我们有2个Heroku应用程序 - 第一个是生产,第二个是分期。我想从生产应用程序中获取一个表中的数据(它的表用户包含所有用户的数据)并将其推送到登台数据库。
经过一番研究后,我发现了一个名为 pgbackups 的插件 - 我只有几个问题: 此插件是否也允许仅从一个表获取数据,而不是从整个数据库获取数据?
第二件事就是这样 - 让我们说生产中的用户ID为 1 到 300 。 ID为 1 至 10 的登台版用户如何将300个用户从生产版本转移到临时版本的方式,这300个用户将从ID 11 计算(我们希望将我们的登台用户保留在登台数据库中)
谢谢
答案 0 :(得分:1)
有很多方法可以在直接SQL中执行此操作。如果你对此感到满意,那就去吧。这种方式适用于Rails中的开发人员 - 因此我们使用JSON提取数据,并使用该JSON在新数据库中创建具有新ID的用户。
由于您只提取了1个表,并且您想将ID重置为新数据库,我建议:
File.open('yourfile.json', 'wb') {|file| file << User.all.to_json }
然后:
users_json = JSON.parse(File.read('yourfile.json'))
users_json.each do |json|
json.delete("id")
User.create(json)
end
答案 1 :(得分:1)
此脚本将数据从Heroku数据库中提取到本地postgres数据库中。您需要安装pgbackups
插件。从Heroku应用程序的根目录执行它。
#!/bin/bash -ex
# This script asks Heroku to backup the current production data, then downloads that dump and imports it into the local db.
# It is at least 3 times quicker than heroku db:pull, and we can only do it because we are dumping from postgres to postgres.
heroku pgbackups:capture --expire
curl -o tmp/latest.dump `heroku pgbackups:url`
pg_restore --verbose --clean --no-acl --no-owner -d your_db_name tmp/latest.dump
答案 2 :(得分:0)
您可以查看my answer in this thread。您可以使用名为forceps的库来完全按照您的要求进行操作。