[英]Heroku Review Apps: copy DB to review app
Trying to fully automate Heroku's Review Apps (beta) for an app.尝试为应用程序完全自动化 Heroku 的评论应用程序(测试版)。 Heroku wants us to use db/seeds.rb
to seed the recently spun up instance's DB. Heroku 希望我们使用db/seeds.rb
来播种最近启动的实例的数据库。
We don't have a db/seeds.rb
with this app.我们没有这个应用程序的db/seeds.rb
。 We'd like to set up a script to copy the existing DB from the current parent (staging) and use that as the DB for the new app under review.我们想设置一个脚本来从当前父级(暂存)复制现有数据库,并将其用作正在审查的新应用程序的数据库。
This I can do manually:这我可以手动完成:
heroku pg:copy myapp::DATABASE_URL DATABASE_URL --app myapp-pr-1384 --confirm myapp-pr-1384
But I can't get figure out how to get the app name that Heroku creates into the postdeploy script.但是我不知道如何将 Heroku 创建的应用程序名称添加到部署后脚本中。
Anyone tried this and know how it might be automated?任何人都试过这个并知道它是如何自动化的?
I ran into this same issue and here is how I solved it. 我遇到了同样的问题,这就是我如何解决它。
Set up the database url you want to copy from as an environment variable on the base app for the pipeline. 将要复制的数据库URL设置为管道的基础应用程序上的环境变量。 In my case this is STAGING_DATABASE_URL
. 在我的情况下,这是STAGING_DATABASE_URL
。 The url format is postgresql://username:password@host:port/db_name
. url格式为postgresql://username:password@host:port/db_name
。
In your app.json
file make sure to copy that variable over. 在app.json
文件中,确保复制该变量。
In your app.json
provision a new database which will set the DATABASE_URL
environment variable. 在app.json
配置一个新数据库,该数据库将设置DATABASE_URL
环境变量。
Use the following script to copy over the database pg_dump $STAGING_DATABASE_URL | psql $DATABASE_URL
使用以下脚本复制数据库pg_dump $STAGING_DATABASE_URL | psql $DATABASE_URL
pg_dump $STAGING_DATABASE_URL | psql $DATABASE_URL
Here is my app.json
file for reference: 这是我的app.json
文件供参考:
{
"name": "app-name",
"scripts": {
"postdeploy": "pg_dump $STAGING_DATABASE_URL | psql $DATABASE_URL && bundle exec rake db:migrate"
},
"env": {
"STAGING_DATABASE_URL": {
"required": true
},
"HEROKU_APP_NAME": {
"required": true
}
},
"formation": {
"web": {
"quantity": 1,
"size": "hobby"
},
"resque": {
"quantity": 1,
"size": "hobby"
},
"scheduler": {
"quantity": 1,
"size": "hobby"
}
},
"addons": [
"heroku-postgresql:hobby-basic",
"papertrail",
"rediscloud"
],
"buildpacks": [
{
"url": "heroku/ruby"
}
]
}
An alternative is to share the database between review apps. 另一种方法是在审核应用之间共享数据库。 You can inherit DATABASE_URL
in your app.json
file. 您可以在app.json
文件中继承DATABASE_URL
。
PS: This is enough for my case which is a small team, keep in mind that maybe is not enough for yours. PS:这对我的情况来说足够了,这是一个小团队,请记住,对你来说可能还不够。 And, I keep my production and test (or staging, or dev, whatever you called it) data separated. 并且,我保持我的生产和测试(或暂存,或开发,无论你怎么称呼)数据分开。
Alternatively: Another solution using pg_restore, thanks to https://gist.github.com/Kalagan/1adf39ffa15ae7a125d02e86ede04b6f 或者:使用pg_restore的另一个解决方案,感谢https://gist.github.com/Kalagan/1adf39ffa15ae7a125d02e86ede04b6f
{
"scripts": {
"postdeploy": "pg_dump -Fc $DATABASE_URL_TO_COPY | pg_restore --clean --no-owner -n public -d $DATABASE_URL && bundle exec rails db:migrate"
}
}
I ran into problem after problem trying to get this to work. 我试图让这个问题解决后遇到问题。 This postdeploy script finally worked for me: 这个postdeploy脚本终于为我工作了:
pg_dump -cOx $STAGING_DATABASE_URL | psql $DATABASE_URL && bundle exec rails db:migrate
I see && bundle exec rails db:migrate
as part of the postdeploy
step in a lot of these responses.我在许多这些响应中看到&& bundle exec rails db:migrate
作为postdeploy
步骤的一部分。
Should that actually just be bundle exec rails db:migrate
in the release
section of app.json
?这实际上应该只是app.json
的release
部分中的bundle exec rails db:migrate
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.