Skip to content

Instantly share code, notes, and snippets.

@katlandreth
Created September 15, 2016 21:10
Show Gist options
  • Save katlandreth/33817a947382ab58e6d3f321f5fb5a5f to your computer and use it in GitHub Desktop.
Save katlandreth/33817a947382ab58e6d3f321f5fb5a5f to your computer and use it in GitHub Desktop.
How to drop and rebuild a database on Heroku.

##Problem: While trying to deploy an app to heroku, a table was chronically not found, preventing asset precompile.

Migrating the db on heroku would seem successfull, but trying to push to heroku would result in this error:

remote:        Running: rake assets:precompile
remote:        rake aborted!
remote:        ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "********" does not exist
.
.
.
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed
remote: Verifying deploy...
remote:
remote: !      	Push rejected to ********.
remote:
To *****heroku url*****
 ! [remote rejected] staging -> master (pre-receive hook declined)
error: failed to push some refs to '*****heroku url*****'

##Solution

I don't know why, but the table seemed to be actually missing, and migrating wasn't creating it. I had to drop the database and rebuild it.

heroku pg:reset DATABASE --app appname where appname is the name of the app on heroku.

This is not ideal, as it destroys the database. This should not be used in production since data will be lost.

@katlandreth
Copy link
Author

AHHHHH! I know what's happening now - I renamed some tables (probably the wrong way) and instead of renaming the existing tables in my local app, new ones were made. The old ones are still in schema.rb, but there are no corresponding migration files so heroku rake db:migrate can't add the tables that are referenced in the schema.rb file. I need to drop the offending tables and remove them from schema.rb.

@katlandreth
Copy link
Author

Well, I guess that didn't work. Still having the same problem, only now the missing table is one that actually should be there.

Still, it's good to not have unused tables floating around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment