Skip to content

Clone a Production DB to stage environment

Morgan Ludtke edited this page Nov 21, 2022 · 3 revisions

The best way to properly test a change before a production release is to test in an environment as close to production as possible. One way to do this is to clone a production DB with the latest data and use it in the staging environment. The following are steps that can be done to accomplish this in the Housing Bay Area Heroku setup

Create a cloned version of the production database

  1. Login to Heroku and go to the housingbayarea -> hba-0-3-alameda-prod heroku instance
  2. Go to the "Resources" tab and click on the "Heroku Postgre" that is "Attached as DATABASE". There might be one that is attached as "HEROKU_POSTGRESQL_" so make sure you select the one called "DATABASE" image
  3. Once in the postgres view click on "Settings" tab and click the button that says "Fork Database..". Make sure the same plan that that DB uses is selected and then click "Fork Database" image
  4. The provisioning can take a while and the ui doesn't show how far in the provisioning it is so you might want to check in on it sporadically using the heroku cli. Make sure you have the heroku cli installed locally and run this command: heroku pg:info --app hba-0-3-alameda-prod. It will output the status of all postgres DBs associated to that app, the one getting provisioned will have its status/provisioning percentage.

Attach the DB to stage environment

  1. Once the DB is fully provisioned you should be able to click on the link called Attached as HEROKU_POSTGRES_<color>. From there there should be a "Manage Attachments" button.

image

  1. In the drawer you can now attach the database to the stage environment. It will assign a different color to staging.

image

Promote the new DB in staging

  1. After the above steps you should be able to traverse to the staging app and find the new postgres listed in the resources tab. image
  2. In your local terminal using the heroku cli you can do the pg:promote command. For the below sample replace <color> with the appropriate color, in the case of the above screenshot it should be CHARCOAL
heroku pg:promote HEROKU_POSTGRESQL_<color>_URL --app hba-0-3-alameda-staging
  1. You will know that it worked if you refresh the page and DATABASE is listed in the attachments dropdown image

Run migrations - potentially optional

  1. If there have been new migrations added that aren't in production yet you will need to run the migrations. If you open up papertrail for staging it will have an error if there are migrations that need to be run.
  2. Go to the "More" button on the top right of Heroku and click "Run Console".

image

  1. In the Console that opens up run this command: yarn db:migration:run. It will output the progress of the migrations so make note that it can fail and will output the errors.