How to reset the postgres database on heroku

So I’m sure your not like me and you never have to rename your models in your database, because you always plan enough ahead that you name everything perfectly on the first migration.  In case you don’t you may end up renaming tables, adding/removing database columns etc.

My issue came from a join model between Students and Courses that I called Registrations.  Which seemed to be one of the rare occasions where I actually found a word for the join model that made perfect sense in the context and perfect sense in English.  However the issue arose when I then started to use the Devise gem for user authentication.  Confusion and chaos ensued due to the fact that devise uses ‘registrations’ to describe users signing up for your site.  These two uses of ‘registration’ caused overlap in routes and other errors.  So in short, I unfortunately needed to rename the full Registrations scaffold, along with its views etc.  Basically I went through my whole app manually in Sublime Text and replaced all the instances of the registration (except the devise ones of course) with my new model name – attendance.  Of course you also have to rename all the files too.  This is tedious and error prone, and if anyone knows of a better way to do it, please let me know.  Maybe I’ll make one in my free time someday.  This Stack Overflow question goes into a bit more detail of exactly how to rename a full scaffold.

But my point here is heroku.  After going through all the work of renaming the full scaffold and all its files, I ran rake db:reset to reset the database, and then ran the migrations again.  Worked great.  However for some strange reason, this does not work on heroku.  I ran heroku run rake db:reset and kept getting this strange error:

FATAL: permission denied for database "postgres" DETAIL: User does not have CONNECT privilege.

which really confused me as of course I’ve been changing the heroku database all along, why would I suddenly not have privileges?  My first thought was to try running sudo, but after Googling, I came across this SO question with the answer:  On heroku you must run heroku pg:reset – notice the omission of run rake.

Hope this saves you some time!

Rails Naming Conventions

Short and sweet:

Scaffolds and Models are singular, controllers are plural.

rails g model User

rails g controller Users

rails g scaffold User