Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku Review Apps: copy DB to review app

Trying to fully automate Heroku's Review Apps (beta) for an app. Heroku wants us to use db/seeds.rb to seed the recently spun up instance's DB.

We don't have a db/seeds.rb with this app. 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.

Anyone tried this and know how it might be automated?

like image 785
Meltemi Avatar asked Oct 23 '15 00:10

Meltemi


People also ask

What is Heroku_app_name?

FYI, HEROKU_APP_NAME is just the name. ie my-app not my-app.herokuapp.com so you'll have to manually append the rest . herokuapp.com to get a usable URL.


3 Answers

I ran into this same issue and here is how I solved it.

  1. Set up the database url you want to copy from as an environment variable on the base app for the pipeline. In my case this is STAGING_DATABASE_URL. The url format is postgresql://username:password@host:port/db_name.

  2. In your app.json file make sure to copy that variable over.

  3. In your app.json provision a new database which will set the DATABASE_URL environment variable.

  4. Use the following script to copy over the database pg_dump $STAGING_DATABASE_URL | psql $DATABASE_URL

Here is my app.json file for reference:

{
  "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"
    }
  ]
}
like image 50
EpiphanyMachine Avatar answered Oct 22 '22 16:10

EpiphanyMachine


An alternative is to share the database between review apps. You can inherit DATABASE_URL in your app.json file.

PS: This is enough for my case which is a small team, keep in mind that maybe is not enough for yours. And, I keep my production and test (or staging, or dev, whatever you called it) data separated.

like image 44
sad parrot Avatar answered Oct 22 '22 15:10

sad parrot


Alternatively: Another solution using pg_restore, thanks to 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"
  }
}
like image 3
Jonathan Haar Avatar answered Oct 22 '22 15:10

Jonathan Haar