8

I'm trying to copy my production database on Heroku across to my staging server.

My staging server has been ahead of my production server with a migration that creates an extra table. I now want to dry-run the migration to add that table on the production database and so would like to revert my staging's database in order to do so.

The problem is that each time I restore the staging database to the production one the table sticks around. I was expecting the database to be destroyed and repopulated but it only seems to be being repopulated.

heroku pgbackups:url b104 --app production
# 'http://s3.amazonaws.com/...'

heroku pgbackups:restore DATABASE 'http://s3.amazonaws.com/...' --app staging

Every time I do this I find that the "newer" table is still remaining.

Does pgbackups:restore actually restore table structure or simply data - what's going on here?

2 Answers 2

11

A restore is a simple Postgres restore, so essentially a scripted version of your data.

In order to do this, you need to nuke your database first, and then rebuild it from the database script:

heroku pg:reset
heroku pgbackups:restore blah...
3
  • This is correct. It currently only drops the relations that the backup knows about before restoring the backup.
    – Will
    Feb 5, 2012 at 21:01
  • Brilliant, that worked a treat. I was doing heroku db:reset before and that was I think simply truncating tables Feb 5, 2012 at 21:13
  • 1
    "The rake db:reset task is not supported. Heroku apps do not have permission to drop and create databases. Use the heroku pg:reset command instead." ref: devcenter.heroku.com/articles/rake
    – Magne
    Mar 12, 2012 at 13:28
4

as neil said, the pgbackups:restore only moves the data and does not change the actual db structure.

for complete db restore (change app-production, app-staging and migration_number to fit your app):

first, get the current migration from your production.

heroku run rake db:version --app app-production

reset staging db (naturally, back it up if needed)

heroku pg:reset DATABASE_URL --confirm app-staging

run the migrations until migration_number to fit the production db structure

heroku run rake db:migrate VERSION=current_migration_number --app app-staging

capture the production app

heroku pgbackups:capture --app app-production

push the data to the staging app

EDIT - in case you're using the heroku toolbelt, the syntax pattern has changed from pgbackups:action_name to pg:backups action_name

heroku pgbackups:restore DATABASE "heroku pgbackups:url --app app-production" --app app-staging

the staging db should now match the production data and structure

heroku run rake db:migrate --app app-staging

hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.